Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 ยท Sensors and Actuators

Analog-to-Digital Conversion

Understand how an ADC turns voltages into numbers: resolution, reference voltage, sampling rate, Nyquist, aliasing and voltage dividers, with simulations.

The real world is analog: temperatures, light levels and sound change smoothly. A microcontroller only understands numbers. The bridge is the analog-to-digital converter (ADC), a circuit that measures a voltage and reports it as an integer. Understanding the ADC helps you interpret raw values, estimate error and avoid surprising mistakes such as reading garbage from a floating pin.

How an ADC measures

An ADC compares the input voltage with a reference voltage (Vref) and returns a fraction of full scale as an integer. An N-bit converter has 2^N levels:

  • 8-bit: 256 levels (0 to 255)
  • 10-bit (classic Arduino Uno): 1024 levels (0 to 1023)
  • 12-bit (ESP32, many STM32): 4096 levels
  • 16-bit and 24-bit (precision external chips)

The smallest voltage step, the resolution (1 LSB), is Vref / 2^N. With 3.3 V and 12 bits that is about 0.8 mV per step.

flowchart LR
    S["Sensor voltage<br/>0 to 3.3 V"] --> SH["Sample and hold"]
    SH --> Q["Quantizer<br/>compare with Vref"]
    Q --> R["Integer<br/>0 to 4095"]
    V["Vref"] --> Q

The ADC samples a voltage, quantizes it and hands your code an integer.

Python Quantization: voltage to ADC counts and back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the converted-back voltage is never exactly equal to the input; the difference is quantization error, up to one LSB. An input above Vref saturates at the top count (4.0 V reads 4095).

Resolution versus accuracy

More bits means finer steps, not necessarily a more accurate answer. Real ADCs have offset and gain error, non-linearity, and noise from the supply and board. The ESP32's built-in ADC, for example, is 12-bit on paper but noticeably non-linear near the ends of its range unless calibrated. A rule of thumb: the last one or two bits of a built-in ADC are often noise. Averaging several samples, covered in signal conditioning, can recover some of that precision.

Choosing a reference voltage

The reference defines full scale. If your sensor only produces 0 to 1.1 V, using a 1.1 V reference (available on some chips) gives three times finer steps than a 3.3 V reference. Using a stable reference matters too: if Vref is the board supply and the battery sags, all readings shift.

Voltage dividers: matching a signal to the ADC range

Many sensors, such as an LDR or a thermistor, change resistance rather than voltage. A voltage divider of two resistors turns resistance into voltage:

Vout = Vin * R2 / (R1 + R2)

Dividers also scale a higher voltage (a 12 V battery) down to the 3.3 V that the ADC tolerates. Never apply a voltage above the ADC's supply to the pin; it can destroy the chip.

Python Voltage divider with a thermistor, read by a 10-bit ADC

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the estimates: the error is small near 25 C where the divider is most sensitive and larger at the extremes where the curve flattens. Choosing the fixed resistor close to the sensor's resistance in the range you care about maximizes sensitivity.

Sampling rate, Nyquist and aliasing

An ADC takes samples at a rate (samples per second). To capture a signal faithfully you must sample at more than twice its highest frequency (the Nyquist criterion). Sample too slowly and fast changes masquerade as slow ones: aliasing. A vibration at 95 Hz sampled at 100 Hz appears as a 5 Hz wobble.

Python Aliasing: sampling a 95 Hz signal at 100 Hz

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 1000 Hz and 400 Hz runs report about 95 Hz, while sampling at 100 Hz makes the signal look near 5 Hz. The fix is to sample fast enough, or put an anti-aliasing low-pass filter (a simple resistor and capacitor) before the ADC to remove frequencies above half the sample rate.

Common ADC mistakes

  • Floating input: an unconnected analog pin reads random values that follow nearby wires.
  • Too much voltage: 5 V on a 3.3 V ADC pin can destroy the chip.
  • Wi-Fi interference: on the ESP32, one ADC group cannot be used while Wi-Fi is active; plan your pin choice.
  • High source impedance: a sensor with a large output resistance cannot charge the sampling capacitor; add a buffer or reduce the resistance.
  • Reading right after a switch: when multiplexing channels, discard the first reading to let it settle.
  • Ignoring the reference: converting counts with the wrong Vref gives wrong values.

External ADCs

When you need more bits or channels than the built-in converter offers, connect an external ADC (ADS1115, 16-bit, four channels) over I2C. It also isolates the converter from the noisy digital chip.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the resolution (1 LSB) of a 12-bit ADC with a 3.3 V reference?

    3.3 V divided by 4096 levels is roughly 0.8 millivolts per step.
  2. 2. What does the Nyquist criterion require?

    Slower sampling folds fast signals into false low frequencies, called aliasing.
  3. 3. What happens if you apply 5 V to the ADC pin of a 3.3 V microcontroller?

    Input voltage above the supply can overstress the chip; use a divider or level shifter.
  4. 4. What does a voltage divider allow you to do with a resistive sensor?

    A divider converts resistance into a proportional voltage the ADC can read.

Summary

  • An ADC turns a voltage into an integer between 0 and 2^N minus 1, relative to Vref.
  • Resolution is Vref / 2^N; quantization error is up to one LSB, and more bits do not guarantee accuracy.
  • Voltage dividers convert resistance to voltage and scale high voltages down.
  • Sample faster than twice the highest frequency, or filter first, to avoid aliasing.
  • Watch for floating pins, overvoltage and noise.

Next: Actuators: Relays and Motors