Wednesday, January 16, 2019

Light-Chasing Self-Driving Car (2): What is A/D Conversion?

To read analog sensor values with Raspberry Pi, we use the A/D converter from last time.

If you're used to Arduino, you might be wondering why.
I certainly was.

Arduino UNO's analog input pins let you connect analog sensors without an external A/D converter.
The reason: the ATmega328P chip inside Arduino UNO has an ADC (analog-to-digital converter) built right in,
so connecting a sensor to an analog input pin gives you a converted digital value immediately.

Raspberry Pi has no built-in ADC, which is why an external converter is needed.

While A/D conversion is second nature to hardware engineers, software people often have a fuzzier understanding of it.
Even the difference between "analog" and "digital" isn't always clear.
(I wasn't entirely sure myself.)

So this post is all about A/D conversion.
  1. Gather parts
  2. What is A/D conversion? (this article)
  3. Reading values from the converter via SPI
  4. Pseudo-analog output with Raspberry Pi PWM
  5. Motor control with a motor driver
  6. Connecting light sensor input to motor control

Analog Values, Digital Values, and A/D Conversion

A common misconception: "floating-point = analog." Not quite.
Once a value is accessible inside a program, it is by definition a digital value.

Another one: "analog input gives you continuous values." Also not quite.
The moment an analog value is A/D converted, it becomes a discrete quantized value.

And a subtle trap: "the raw A/D output is an engineering unit." It is not.
Digital value 20 (0x14) is not 20 degrees Celsius — it's just a number.

Here's a rough sketch of the full picture:
(1) The sensor's role
Most analog sensors change their resistance in response to environmental changes.
The relationship between the physical quantity and resistance is defined by the sensor's characteristics — and it's usually non-linear.
For example, doubling the brightness doesn't double (or halve) the resistance.
That's what the sensor does.

(2) The A/D converter's role
The sensor's changing resistance means a changing voltage across its terminals.
The A/D converter quantizes that voltage into a fixed number of bits.
For example, an 8-bit A/D converter with a 5V reference divides the 0–5V range into 256 steps (roughly).
The actual process is more sophisticated, but that's the basic idea.

(3) Engineering unit conversion
If you want the value in Celsius or another physical unit, you need to convert the digital value back.
Since the sensor's characteristic is non-linear, this conversion is also non-linear.
And because the A/D conversion discretized the signal, the resulting engineering values are also discrete.

In most cases, this conversion from raw A/D value to engineering unit has to be done in software — and building that conversion function can be tricky.
Some packaged analog sensor modules (like the DHT11 temperature/humidity sensor) come with manufacturer libraries that handle steps (1)–(3) automatically.

Explaining This to a 4th Grader

I tried explaining A/D conversion to my 4th-grade son.
How much of it landed — unclear.



That turned into a longer explanation than expected, so the actual A/D converter usage comes next time.
Next: reading values from the A/D converter via SPI communication.

No comments:

Post a Comment