Most sensors produce a voltage, but microcontrollers work with numbers. An analogue-to-digital converter (ADC) bridges the gap by measuring a voltage and turning it into a number. Knowing how it works helps you get readings you can trust.
Resolution: how many steps
An ADC divides its input range into steps. An N-bit ADC has 2N steps:
| Bits | Steps | Step size with a 3.3 V reference |
|---|---|---|
| 8 | 256 | 12.9 mV |
| 10 | 1,024 | 3.22 mV |
| 12 | 4,096 | 0.806 mV |
| 16 | 65,536 | 0.050 mV |
Converting a reading back to volts:
voltage = reading × reference voltage ÷ 2N
MicroPython's read_u16() always scales readings to 0 to 65,535, whatever the hardware's real resolution, so divide by 65,535 there.
The reference voltage
Readings are measured relative to a reference voltage. Often that's the board's supply, such as 3.3 V. If the supply is a little off, or noisy, every reading is too. Anything above the reference reads as the maximum, and voltages above the chip's rated limit can damage the pin, which is why higher voltages need a voltage divider.
Sampling rate
An ADC takes snapshots. To capture a signal that changes, you must sample fast enough: at more than twice the highest frequency in the signal. For a temperature that changes over minutes, a reading a second is plenty. For audio, tens of thousands of samples a second are needed.
Getting cleaner readings
- Average several readings. It smooths random noise.
- Add a capacitor (around 100 nF) from the ADC pin to ground, close to the pin.
- Keep analogue wires short and away from motors, PWM lines and switching regulators.
- Calibrate against a multimeter: measure a known voltage and correct your scale factor.
Going deeper
Effective number of bits
Noise and non-linearity mean an ADC's useful resolution is lower than its nominal bit count. Datasheets give this as ENOB (effective number of bits). A "12-bit" microcontroller ADC commonly delivers somewhere around 9 to 11 effective bits. When precision matters, look for the ENOB, INL (integral non-linearity) and DNL (differential non-linearity) specifications rather than the headline bit count.
Aliasing
Sampling slower than twice a signal's frequency doesn't just miss detail. It creates false, lower frequencies in your data, called aliases. A 1,050 Hz tone sampled at 1,000 samples per second appears as a 50 Hz signal. Mains hum and PWM noise often alias into sensor readings this way. An analogue low-pass filter before the ADC (an "anti-aliasing filter") removes frequencies the sampling rate can't represent.
def alias_frequency(signal_hz, sample_rate_hz):
folded = signal_hz % sample_rate_hz
return min(folded, sample_rate_hz - folded)
for f in (100, 450, 1050, 1950):
print(f"{f} Hz sampled at 1000/s looks like {alias_frequency(f, 1000)} Hz")
100 Hz sampled at 1000/s looks like 100 Hz
450 Hz sampled at 1000/s looks like 450 Hz
1050 Hz sampled at 1000/s looks like 50 Hz
1950 Hz sampled at 1000/s looks like 50 Hz
Oversampling for extra resolution
If a signal has a little random noise, averaging 4n samples can gain n extra bits of resolution: 4 samples for 1 bit, 16 for 2 bits, 64 for 3 bits. This only works if the noise is at least about one step in size and truly random, and it trades away sample rate.
Types of ADC
- SAR (successive approximation) ADCs, the type built into most microcontrollers, balance speed and resolution well.
- Delta-sigma ADCs, such as those in precision measurement chips and load-cell amplifiers, reach 16 to 24 bits at low sample rates.
- Flash and pipelined ADCs are very fast, used in oscilloscopes and radio.
An external I²C or SPI ADC with its own precision reference is often the simplest upgrade over a microcontroller's built-in ADC.
Driving the input
SAR ADCs charge an internal sampling capacitor at the start of each conversion. A high-resistance source can't charge it in time, so readings sag. Keep the source resistance within the datasheet's limit, add a capacitor at the pin, or buffer the signal with an op-amp voltage follower.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.