EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 11
Analog-to-Digital Conversion
Turning a voltage into a number.
An ADC measures an input voltage and returns an integer. A 10-bit ADC gives 1024 steps, so with a 5 V reference each step is about 4.9 mV.
The formula is: voltage = reading x reference / (2^bits). A noisy or high-impedance source gives unstable readings; average several samples and keep wires short.
Never feed an ADC pin more than its supply voltage.
EXAMPLE
uint16_t raw = adc_read(2); /* hypothetical call, channel 2 */ float volts = raw * 5.0f / 1024.0f; /* 10-bit, 5 V reference */