Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Analog and PWM

Reading Analog Inputs with analogRead

Learn how the Arduino ADC turns a voltage into a number: analogRead, 10-bit resolution, reference voltage, converting readings to volts, potentiometers and ADC quirks.

The real world is analog: light fades smoothly, temperature drifts gradually, a knob turns through every position. A digital pin can only tell HIGH from LOW, so the Arduino includes an analog-to-digital converter (ADC) that measures a voltage and reports it as a number. This lesson explains how that number relates to volts, how to read it with analogRead() and how to avoid the common traps.

From voltage to number

The Uno's ADC has 10 bits of resolution, so it distinguishes 2ยนโฐ = 1024 levels, numbered 0 to 1023. It compares the input voltage with a reference voltage, by default the supply (about 5 V). A reading of 0 means 0 V, and 1023 means the reference voltage:

TEXT
reading = Vin / Vref ร— 1023        (roughly; 1024 steps, so 1 step = Vref/1024)
Vin     = reading ร— Vref / 1024

At a 5 V reference, one step is 5 / 1024 โ‰ˆ 4.88 mV. That is the finest voltage change the ADC can see.

C Stand-in: simulate a 10-bit ADC

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice that 5.0 V and 6.0 V both read 1023: the ADC saturates. Never feed more than the supply voltage into an analog pin; 6 V into a 5 V board can damage it.

analogRead

C++
int reading = analogRead(A0);   // 0..1023

On the Uno, analog inputs are A0 to A5. No pinMode() is needed. The conversion takes about 100 microseconds, so you can sample roughly 9,000 times per second.

The classic first circuit: a potentiometer

A potentiometer ("pot") is a variable resistor with three legs. Connect the outer legs to 5V and GND and the middle leg (the wiper) to A0. Turning the knob moves the wiper voltage anywhere from 0 to 5 V.

C++
void setup() {
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(A0);
  float volts = reading * 5.0 / 1024.0;
  Serial.print(reading);
  Serial.print("  ");
  Serial.print(volts, 2);
  Serial.println(" V");
  delay(200);
}
flowchart LR
    V["5V"] --> P1["Pot leg 1"]
    P1 --- W["Wiper"]
    W --> A["A0"]
    P1 --- P3["Pot leg 3"]
    P3 --> G["GND"]

Figure: a potentiometer as a voltage divider. The wiper voltage follows the knob.

Using a reading to control something

Readings are most useful when compared with thresholds. A night light turns on when a light sensor reading drops below a limit. Here is the logic with a scripted series of readings:

C Stand-in: a threshold with hysteresis

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Two thresholds (a gap) is called hysteresis. With a single threshold at 400, the lamp would flicker when the readings hover around 400. The gap makes the decision stable.

Reference voltage options

analogReference() changes what 1023 stands for:

Setting Uno behavior
DEFAULT the supply voltage (5 V or 3.3 V)
INTERNAL internal 1.1 V reference (ATmega328P)
EXTERNAL the voltage you apply to the AREF pin

With the 1.1 V reference, one step is only about 1.07 mV, which gives finer resolution for small signals such as the output of a temperature sensor. Call analogReference() in setup() before reading, and never apply a voltage to AREF while using INTERNAL or DEFAULT; that shorts the sources. Also, throw away the first reading after changing the reference.

C Stand-in: resolution with different references

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

ADC quirks

  • Accuracy is about ยฑ2 steps. Do not trust the last bit or two.
  • Source impedance. The ADC expects a signal source below about 10 kฮฉ. Sensors with high resistance give wrong readings unless buffered.
  • Pin switching. Reading a different pin right after another can give leftover charge; read twice and keep the second value if the sensor impedance is high.
  • Noise. Power supply noise shows up directly. Averaging (Smoothing and Filtering) and a 100 nF capacitor from the input to GND help.
  • Other boards differ: ESP32 has a 12-bit ADC (0 to 4095) and a 3.3 V range with a non-linear response; the Uno R4 can be set to 14 bits with analogReadResolution().

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What range of values does analogRead() return on an Uno?

    The ADC has 10 bits, giving 1024 values, 0 to 1023.
  2. 2. A reading of 512 with a 5 V reference is about what voltage?

    512 / 1024 x 5 V = 2.5 V.
  3. 3. Why use hysteresis around a threshold?

    Separate on and off thresholds stabilize the decision.
  4. 4. What is the danger of putting 6 V on an analog pin of a 5 V board?

    Inputs above the supply voltage can damage the pin protection diodes.

Summary

  • The ADC converts a voltage into 0 to 1023 relative to the reference voltage; one step is about 4.88 mV at 5 V.
  • analogRead(A0) needs no pinMode, and takes about 100 ยตs.
  • Convert with volts = reading * Vref / 1024.0.
  • Use hysteresis for threshold decisions.
  • analogReference() and noise reduction improve accuracy; other boards have different resolutions.

Next: Voltage Dividers