ATmega328P/Analogue input/22. analogRead: 1024 steps
Analogue input · 22 of 33

analogRead: 1024 steps

analogRead turns the voltage on an analogue pin (A0 to A5 on the UNO, A0 to A7 on the Nano) into a whole number from 0 to 1023, one step for every 4.9 mV of a 5 V range. One jumper wire to GND, 3V3 and 5V shows the whole scale, and pulling it out shows what an unconnected input reads.

Your board

One converter, several pins

The ATmega328P has one analogue-to-digital converter (ADC) and a switch in front of it that picks which pin it listens to. A0 to A5, on the bottom right strip, are six inputs to that one converter. analogRead(A0) and analogRead(0) read the same pin.

The chip has eight analogue inputs. The UNO wires six of them to pins; A6 and A7 go nowhere. A6 and A7 are analogue only: there is no digital circuit behind them, so digitalRead, digitalWrite and INPUT_PULLUP do nothing useful there. On the Nano, analogRead(A6) works like any other.

The converter has 10 bits, so it answers with one of 1024 numbers, 0 to 1023. The datasheet's rule is count = input × 1024 ÷ reference. With the reference at 5 V, each step is 5 ÷ 1024 = 4.88 mV.

analogRead: a voltage becomes one of 1024 steps
DEFAULT, 5 V
Voltage on A03.300 V
Reading
675
One step
4.88 mV
Count back to volts
3.296 V
Every voltage from 3.296 to 3.301 V reads 675. The ADC reports which step the input is on, not where on the step. Converting back gives the bottom of the step, so a reading is never more precise than 4.9 mV, and the chip's own error adds two or three steps more.

At full scale the line looks smooth. Magnified, it is a staircase, and the count says which tread the voltage is standing on, not where on the tread. A reading of 675 means somewhere from 3.296 V to 3.301 V, and nothing in the sketch can recover more than that. The chip adds its own error on top: the datasheet gives 2.2 steps typical, 3.5 at most.

A hundred microseconds

The converter needs a clock of 50 to 200 kHz. The Arduino core divides the 16 MHz system clock by 128, giving 125 kHz, and one conversion takes 13 of those clocks: 104 µs. That caps a sketch at a little under 10,000 readings a second, and analogRead waits for its answer, so the sketch does nothing else for those 104 µs.

The very first conversion after the ADC switches on takes 25 clocks instead of 13. Nobody notices 200 µs once.

Three known voltages from one wire

A0 is an input, and analogRead never drives it, so a wire from A0 to GND, 3V3 or 5V is safe. Those three are on the strip to the left of the analogue pins, on the same edge.

A0 wired toCount, with a 5.00 V railWhy
GND0The bottom of the scale
3V3about 6753.3 × 1024 ÷ 5
5V1023The input is the reference itself

Pull the wire out and the number wanders: a few hundred one second, something else the next, and it moves when your hand comes near. Nothing is holding the pin, the same floating input as in digitalRead and the floating input, now measured in millivolts instead of HIGH and LOW.

What the pin wants to be connected to

The datasheet says the input is built for sources of about 10 kohm or less. Behind a bigger resistance, the converter's sampling capacitor does not fill in time and readings come out low, or carry a trace of the pin read before. A jumper wire is close to zero ohms; a sensor or a divider is worth checking.

The pin must never see more than the supply. Above 5 V the count stays at 1023 and the sketch has no way of knowing; past half a volt over, the pin's protection diode starts to conduct, which is how analogue inputs are killed.

The code

AnalogRead_A0.ino

Prints A0 five times a second, as the count and as the voltage it stands for. Upload it, open the Serial Monitor, then move one jumper wire from A0 to GND, to 3V3 and to 5V.

// AnalogRead_A0.ino — print A0 as a count and in volts, 5 a second
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: one jumper wire from A0 to GND, then to 3V3, then to 5V.
//   A0 is an input (analogRead never drives it), so all three are
//   safe. Pull the wire out to see a floating pin wander.

const float VREF = 5.0;  // DEFAULT reference: the 5 V rail

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

void loop() {
  int n = analogRead(A0);          // 0 to 1023, takes about 104 us
  float volts = n * VREF / 1024.0;   // the bottom of step n

  Serial.print("A0 = ");
  Serial.print(n);
  Serial.print("   ");
  Serial.print(volts, 3);
  Serial.println(" V");
  delay(200);
}

Expect about 0, about 675 and 1023 on a 5.00 V rail. The 3V3 reading comes out higher when the rail is lower, because the rail is the ruler: a little on the UNO, more on the Nano, whose USB power comes through a diode. The next two articles are about that.

When it does not work

A0 reads a number that wanders with nothing connected

Nothing is holding the pin, so the ADC measures whatever charge is left on it and on its own sampling capacitor, and that drifts. It also follows your finger and whatever channel was read just before. A floating analogue input has no right answer: wire it to something.

The last digit jumps by one or two between readings

That is the converter's own noise and error, two or three steps typical by the datasheet, plus ripple on the USB supply. Average several readings, or ignore changes smaller than a few counts.

3V3 reads about 690, or over 700, not 675

The count is a share of the 5 V rail, and on USB the rail is under 5 V: a little on the UNO, a few tenths of a volt on the Nano, after its diode. The 3.3 V regulator does not move, so a smaller ruler gives a bigger number. The next two articles show how to see the rail and how to measure against a reference that does not move.

Every reading is 1023

A0 is at or above the reference, which is the 5 V rail: the wire is on 5V, or the pin is wired to something bigger. Anything above 5 V on a pin is a fault. Move the wire to GND and check that the count drops to 0.

Where this goes next

Every count is a share of a reference voltage, and you can choose which one.

The reference: DEFAULT, INTERNAL, AREF →

Edit this page — content/books/atmega328p/analogread.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →