Build note
ADC: turning a voltage into a number
A digital pin can only tell you whether a voltage is above or below about 2.5 V. The ADC tells you what the voltage actually is. It is a ruler with 1024 evenly spaced marks laid across the reference voltage.
10 bits, 1024 steps
The converter is 10 bits wide, so it reports 0 to 1023. With AVcc at 5 V as the reference, each step is:
5 V / 1024 = 4.88 mV
A reading of 512 is about 2.5 V. A reading of 1023 is just under 5 V. That resolution is plenty for a potentiometer and barely enough for a microphone.
The prescaler is the accuracy knob
The converter needs a clock between 50 kHz and 200 kHz to reach full 10-bit
accuracy. The 16 MHz system clock has to be divided down, and the division
factor is ADPS2:0 in ADCSRA.
ADPS2:0 |
Division | ADC clock at 16 MHz |
|---|---|---|
011 |
8 | 2 MHz |
111 |
128 | 125 kHz |
The first converted-potentiometer example in the folder uses /8, which is
2 MHz. It works, but it is far above the datasheet’s range, so the lower bits of
the result can be noise. The PWM part of this series uses /128, which lands at
125 kHz and is the setting to copy.
The registers
| Register | Page | Job |
|---|---|---|
ADMUX |
217 | Reference and input channel |
ADCSRA |
218 | Enable, start, prescaler |
ADCL, ADCH |
219 | The result, low byte first |
ADC |
219 | Reads both result bytes in the right order |
ADMUX needs REFS0 set for AVcc. MUX3:0 selects the channel, and 0000 is
ADC0 on PC0.
ADCSRA needs ADEN to turn the converter on and ADSC to start one
conversion. Hardware clears ADSC when the conversion finishes, so waiting for
it to clear is the ready signal.
The loop
ADCSRA |= (1 << ADSC);
loop_until_bit_is_clear(ADCSRA, ADSC);
uint16_t value = ADC;
ADC is a convenience alias that reads ADCL then ADCH. Reading ADCH first
gives you a high byte that may belong to the previous conversion, which is a
classic source of values that jump around for no reason.
The example turns the number into a bar graph. The thresholds are 256, 512 and
768, and the three if blocks are deliberately not exclusive, so the LEDs light
cumulatively rather than moving as a single dot.
Wiring
5V ---[ pot outer leg ]
|
+--- wiper ---> PC0 (chip pin 23, Arduino A0)
|
GND ---[ pot outer leg ]
LEDs: PB1 -> 220 ohm -> LED -> GND
PB2 -> 220 ohm -> LED -> GND
PB3 -> 220 ohm -> LED -> GND
AVcc on pin 20 goes to 5 V. If AVcc is the reference, AREF can be left floating or fitted with a 100 nF capacitor to ground.
The failures that cost time
- AVcc not connected. The converter measures against a reference that is not there, and the readings are meaningless.
- Reading
ADCHbeforeADCL. You get a stale high byte. - Prescaler too fast. The value works but the last bits wobble.
- A floating input. An unconnected ADC pin reads whatever the air is doing. Tie unused channels to ground or a known voltage.
Making it useful
Convert the reading to millivolts and print it over the UART from Part 2:
uint32_t mv = (uint32_t)value * 5000 / 1023;
That is a voltmeter with five-millivolt steps. It is also the input side of the PWM part, where the same potentiometer sets an LED’s brightness.
How this was made
- Written by: Rupesh, from his own
c-03-adcnotes and code, with AI drafting and editing. - Numbers: from the ATmega328P datasheet and the converter arithmetic.
- AI-assisted sections: structure and copy editing.