ENع
←analogRead
Lesson 20 · Analog: ADC, DAC and sensors

Reading an analog pin

12 min9 steps5 questionsIntermediate
About this lesson

A digital pin sees only HIGH or LOW; an analog pin measures a voltage. Turn a 10 kΩ potentiometer on an Arduino Uno R3 to 3.20 V and follow analogRead(A0) inside the ATmega328P: the multiplexer picks the pin, a 14 pF capacitor samples and holds the voltage, and a 10-bit successive-approximation converter finds 1010001111, which is 655. Then scale it back to volts and see what one step (4.88 mV) and one reading (104 µs) cost. The Signals lessons “Quantization and bit depth” and “Inside an ADC: flash and successive approximation” go deeper into the converter; the Circuits lesson “Voltage and current dividers” explains the potentiometer.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 20 · Interactive
01 / 09

A pin that measures

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.

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
Loading figure…
or ← →
Read the lesson step by step
  1. A pin that measures

    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.

    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
  2. Turn the knob to 3.20 V

    A potentiometer is a 10 kΩ resistor with a sliding contact, the wiper. Its ends sit on 5 V and GND, so the wiper picks a voltage between them. At 64% of the way up it gives 3.20 V.

    potentiometer  10 kΩ, ends on 5 V and GND
    wiper          slides along it: a voltage divider
    position       64% up from GND
    wiper voltage  0.64 x 5 V = 3.20 V  ->  pin A0
  3. analogRead(A0) starts

    Line 2 calls analogRead(A0). The Arduino core writes the channel number into a register called ADMUX, then sets the ADSC bit to start a conversion. The CPU then waits until the converter clears ADSC.

    line 2   int raw = analogRead(A0);
    core     ADMUX = reference | channel 0
             set ADSC        start converting
             while ADSC is 1, wait
             return ADC      the 10-bit result
  4. Pick one pin

    The chip has six analog pins but only one converter. A multiplexer, a selector switch, connects just the chosen pin to it. ADMUX says channel 0, so 3.20 V from A0 goes through and the other five wait.

    multiplexer  6 pins in, 1 out
    ADMUX        channel 0  ->  A0 connected
    A0           3.20 V passes through
    A1 A2 A3 A4 A5  not connected this time
  5. Sample, then hold

    A switch lets the voltage charge a tiny 14 pF capacitor to 3.20 V. 1.5 ADC clock ticks after the start, the switch opens. The capacitor now holds 3.20 V steady, even if the knob moves, while the bits are worked out.

    sample   switch closed: C(S/H) = 14 pF charges to 3.20 V
    hold     1.5 ADC clocks in (12 µs), the switch opens
             the capacitor keeps 3.20 V for the rest of the conversion
    source   should be about 10 kΩ or less, to charge it in time
  6. Ten bits, top bit first

    Successive approximation: test one bit per clock tick. Is 3.20 V at least 2.50 V, half of 5 V? Yes: bit 9 is 1. At least 3.75 V? No: bit 8 is 0. Ten tests later, the ADC register holds 1010001111.

    try each bit from the top; keep it if Vin >= the trial level
    bit 9  try  512  = 2.500 V   yes -> 1
    bit 8  try  768  = 3.750 V   no  -> 0
    bit 7  try  640  = 3.125 V   yes -> 1
    bit 6  try  704  = 3.438 V   no  -> 0
    bit 5  try  672  = 3.281 V   no  -> 0
    bit 4  try  656  = 3.203 V   no  -> 0
    bit 3  try  648  = 3.164 V   yes -> 1
    bit 2  try  652  = 3.184 V   yes -> 1
    bit 1  try  654  = 3.193 V   yes -> 1
    bit 0  try  655  = 3.198 V   yes -> 1
    result  1010001111
  7. The number: 655

    Binary 1010001111 is 655. The datasheet's rule agrees: 3.20 × 1024 / 5 = 655.4, rounded down to 655. analogRead returns it, and line 2 stores it in raw.

    register  1010001111  =  655
    rule      ADC = Vin x 1024 / Vref
              3.20 x 1024 / 5 = 655.36  ->  655
    line 2    raw = 655
  8. Back to volts

    Line 3 undoes the rule: 655 × 5.0 / 1024 = 3.198 V, and println shows 3.20. One step is 5 V / 1024 = 4.88 mV, so every voltage from 3.198 V up to 3.203 V reads 655.

    line 3   v = 655 x 5.0 / 1024 = 3.1982
    line 4   Serial.println(v)  ->  3.20   (2 decimals by default)
    step     5 V / 1024 = 4.88 mV  (resolution)
    655 means 3.198 V <= Vin < 3.203 V
  9. 104 µs per reading

    The ADC clock is 16 MHz / 128 = 125 kHz: one tick every 8 µs. A conversion takes 13 ticks, 104 µs, so at most about 9,615 readings a second. Our delay(500) asks for only two.

    ADC clock    16 MHz / 128 = 125 kHz  (must be 50..200 kHz for 10 bits)
    one tick     8 µs
    conversion   13 ticks = 104 µs   (the very first: 25 ticks)
    fastest      about 9,615 readings per second
    line 5       delay(500)  ->  2 readings per second

Quiz Unlocks after the last step.