5V10kA1A2A3A4A5MUXA0S/H14pFSAR ADC10-bit125 kHz9876543210010101010101010101011void loop() {2 int raw = analogRead(A0);3 float v = raw * 5.0 / 1024;4 Serial.println(v);5 delay(500);6}int rawfloat vINSIDE THE ATmega328P3.20 Vholds 3.20 V3.20A06553.198
Back: a potentiometer on A0, and inside the chip the multiplexer, sample and hold, converter and registerFront: the firmware card and the variables raw and vTokens: the voltage, the channel, the result and clock ticksA 10 kΩ potentiometer on an Arduino Uno R3 sets pin A0 to 3.20 V. analogRead(A0) picks A0 with the multiplexer, holds the voltage on a 14 pF capacitor and finds ten bits, top bit first: 1010001111 = 655. The firmware scales it back: 655 × 5.0 / 1024 = 3.198 V. Each step is 4.88 mV, and one reading takes 13 ticks of a 125 kHz clock: 104 µs.

The voltage

  1. A digital pin only sees HIGH or LOW. An analog input pin measures any voltage from 0 to 5 V and turns it into a number. The Arduino Uno R3 has six: A0 to A5. We read A0.

Inside analogRead

Using the number

What happens

;; step 1/9 · analog
digital pin   HIGH or LOW, nothing between
analog pin    any voltage 0 .. 5 V  ->  a number 0 .. 1023
Arduino Uno R3: A0 A1 A2 A3 A4 A5
inside the ATmega328P: one converter, shared by all six

The datasheet's rule is ADC = Vin × 1024 / Vref, and 1023 (0x3FF) means "the reference minus one step", so this lesson divides by 1024 to go back. Arduino's ReadAnalogVoltage example divides by 1023.0 instead; the two differ by less than one step (4.88 mV). Real readings are not exact: the datasheet gives ±2 LSB absolute accuracy, and the reference is the board's 5 V supply, which is rarely exactly 5.00 V. Expect the last digit or two to jitter; later lessons in this module cover the reference voltage, smoothing and calibration.