Look below the sketch

You can skip this page. It shows what analogRead does inside the chip.

The chip has an ADC, a part that turns a voltage into a number7. Small memory slots called registers control it.

analogRead(A0) starts one conversion on A0 and waits until it finishes3.

analogRead() result against the voltage on A0A straight line from 0 at 0 V to 1023 at 5 V. The lab reads 0 at 0 V, 256 at 1.25 V, 512 at 2.5 V, 768 at 3.75 V, 1023 at 5 V. 1023 is ten bits all set to 1: 11 in ADCH and 11111111 in ADCL.analogRead()025651276810230 V1.252.53.755 VVIN on A0ADC = VIN × 1024 / VREF2565127681023, the topADCHADCL1023 = ten 1s19181716151413121110analogRead() result against the voltage on A0A straight line from 0 at 0 V to 1023 at 5 V. The lab reads 0 at 0 V, 256 at 1.25 V, 512 at 2.5 V, 768 at 3.75 V, 1023 at 5 V. 1023 is ten bits all set to 1: 11 in ADCH and 11111111 in ADCL.analogRead()025651276810230 V1.252.53.755 VVIN on A02565127681023, the topADCHADCL19181716151413121110
The conversion gives ADC = VIN × 1024 / VREF2, a 10-⁠bit number. Ten bits hold 0 to 10231, so at 5 V, where the formula says 1024, the lab reads 1023: every bit of ADCH and ADCL set to 1. The dots are the readings the lab gives at 0, 1.25, 2.5, 3.75 and 5 V.

The four registers

  • ADMUX picks the reference and the input. Its bits REFS1:0 = 01 pick AVCC. MUX3:0 = 0000 picks input ADC02.
  • ADCSRA runs the ADC. ADEN turns it on. ADPS2:0 = 111 divides its clock by 1282.
  • Writing 1 to ADSC starts a conversion3. ADSC reads as 1 until the conversion finishes2.
  • ADCL and ADCH hold the result, ADC = VIN × 1024 / VREF. Read ADCL first, then ADCH2.

The same reading, two ways

l2-read-arduino.ino
// Lesson 2, Arduino version: read the knob on A0 and
// print the reading.
void setup() {
  Serial.begin(115200);
}

void loop() {
  int reading = analogRead(A0);  // 0 to 1023 on the UNO R3
  Serial.println(reading);
  delay(20);
}
l2-read-registers.ino
// Lesson 2, register version: the same reading, done with
// the ADC registers.
void setup() {
  Serial.begin(115200);
  // Reference AVCC, input ADC0 (A0), result right-adjusted
  ADMUX = (1 << REFS0);
  // Turn the ADC on, with its clock at 16 MHz / 128
  ADCSRA = (1 << ADEN)
         | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}

void loop() {
  ADCSRA |= (1 << ADSC);           // start one conversion
  while (ADCSRA & (1 << ADSC)) {}  // ADSC stays 1 until done
  uint8_t low = ADCL;              // read ADCL first
  uint8_t high = ADCH;
  int reading = (high << 8) | low;
  Serial.println(reading);
  delay(20);
}

Registers in this lesson (ATmega328P data-⁠space addresses)2

RegisterAddressBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
ADMUX0x7CREFS1REFS0ADLAR–MUX3MUX2MUX1MUX0
ADCSRA0x7AADENADSCADATEADIFADIEADPS2ADPS1ADPS0
ADCH0x79––––––ADC9ADC8
ADCL0x78ADC7ADC6ADC5ADC4ADC3ADC2ADC1ADC0

Watch the registers

Switch between the two programs and slide the knob. Watch ADCH and ADCL change. At 2.5 V both programs print 5122.

Sources for this page

  1. Arduino reference: analogRead
  2. Microchip ATmega328P datasheet DS40002061A: §24.7 ADC conversion result (p256); Table 24-3 (p257); ADEN and ADSC (p258)
  3. ArduinoCore-⁠avr, cores/arduino/wiring_analog.c
  4. Arduino tutorial: Analog Read Serial: what a potentiometer does; 0 V reads 0 and 5 V reads 1023