Sub topic 10
Analog inputs: A0 to A5 and the 10-bit ADC
This chapter assumes you know basic voltage, current, and resistance, can write a sketch with setup() and loop(), and can read a simple breadboard diagram. It covers how the Uno turns a voltage into a number, how to convert that number back, and the errors that most often corrupt readings.
What the analog pins do
The Uno has six analog input pins, labeled A0 to A5. Each one connects to an analog-to-digital converter (ADC), a circuit inside the ATmega328P microcontroller that measures a voltage and reports it as an integer.
The ADC is a single circuit shared by all six pins. An analog multiplexer (a switch that connects one pin at a time) routes the chosen pin to the converter. This has two consequences:
- The Uno measures one analog pin at a time, never several at once.
- Switching from one pin to another can leave a small residual charge from the previous reading.
The ADC resolution is 10 bits. That gives 2^10 = 1024 possible values, from 0 to 1023. A reading of 0 means the pin is at 0 V (GND). A reading of 1023 means the pin is at or near the reference voltage.
Reference voltage and resolution
The ADC compares the input voltage to a reference voltage. By default on the Uno, the reference is the board's supply voltage, about 5 V. The size of one step, called the least significant bit (LSB), is the reference divided by 1024:
5.0 V / 1024 ≈ 4.88 mV per stepThe conversion formula is:
volts = reading * (reference / 1024)Many tutorials divide by 1023 instead, as the example below does. The difference is under 0.1% and is smaller than the converter's own error, so either works in practice. Dividing by 1024 is the more accurate description of how the hardware works, because each reading represents a band of voltages 4.88 mV wide.
The code below reads A0 and prints both the raw value and the voltage.
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
float volts = raw * (5.0 / 1023.0);
Serial.print(raw);
Serial.print(" -> ");
Serial.print(volts, 2);
Serial.println(" V");
delay(500);
}Open the Serial Monitor at 9600 baud to see the output. Connect A0 to 5V and the reading should be close to 1023. Connect it to GND and it should be close to 0.
Wiring a potentiometer
A potentiometer (pot) is a variable resistor with three terminals. The outer two connect across a voltage, and the middle one, the wiper, outputs a voltage between them. This is a voltage divider you can adjust by turning the knob.
- Connect one outer terminal to 5V.
- Connect the other outer terminal to GND.
- Connect the wiper to A0.
Turning the knob sweeps A0 from 0 V to 5 V, so the reading sweeps from 0 to about 1023. A 10 kΩ pot is a common choice. It draws only 0.5 mA from the 5V pin (5 V / 10,000 Ω) and has a low enough output resistance for the ADC.
Source impedance and settling
Before each conversion, the ADC charges a small internal capacitor (the sample-and-hold capacitor) from the selected pin. The Atmel datasheet recommends that the signal source have an output impedance of about 10 kΩ or less. A higher source impedance charges the capacitor too slowly, and the reading is pulled toward the previous channel's value.
Two practical rules follow:
- Use sensors or dividers with a low output resistance. A 10 kΩ pot is fine. A voltage divider built from two 1 MΩ resistors is not.
- If you read several pins in a row and values seem to bleed into each other, read the pin twice and discard the first result, or add a short
delay()of a few milliseconds between channels.
int readSettled(int pin) {
analogRead(pin); // discard: lets the multiplexer settle
delay(2);
return analogRead(pin); // use this one
}
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(readSettled(A0));
Serial.print(" ");
Serial.println(readSettled(A1));
delay(500);
}Reading cost
A single analogRead() takes about 100 microseconds on the Uno. The ADC clock is the 16 MHz system clock divided by 128, which gives 125 kHz, and one conversion needs 13 ADC clock cycles. That limits you to roughly 9,600 readings per second. This is fast enough for sensors such as potentiometers, light sensors, and temperature sensors. It is too slow for audio sampling without changing the ADC prescaler (a divider setting) in the registers.
Changing the reference voltage
The default reference is the supply voltage, which is a weak point. If the supply is not exactly 5 V, every reading is scaled wrongly. A board powered over USB often sits at 4.7 to 5.1 V depending on the port and cable. A reading of 512 then means a different voltage on different days.
The analogReference() function selects another reference:
| Argument | Reference used | Typical use |
|---|---|---|
| `DEFAULT` | The supply voltage, about 5 V | General use |
| `INTERNAL` | A built-in 1.1 V reference | Small signals, such as a sensor that outputs 0 to 1 V |
| `EXTERNAL` | The voltage applied to the AREF pin | A precise external reference |
With INTERNAL, one step becomes 1.1 V / 1024 ≈ 1.07 mV, which gives about 4.5 times the resolution for signals below 1.1 V. The internal reference is stable and does not depend on the supply voltage.
void setup() {
Serial.begin(9600);
analogReference(INTERNAL); // 1.1 V full scale
analogRead(A0); // discard the first reading after the change
}
void loop() {
int raw = analogRead(A0);
float volts = raw * (1.1 / 1024.0);
Serial.print(volts, 3);
Serial.println(" V");
delay(500);
}With INTERNAL, never apply more than 1.1 V of signal expecting a meaningful result. Readings saturate at 1023 above that level.
Also keep every analog input between 0 V and the supply voltage (plus a small margin). A voltage below GND or above 5 V can damage the ATmega328P, regardless of the reference you chose.
Noise and how to reduce it
Readings are rarely perfectly steady. Typical sources of jitter are supply noise, long wires acting as antennas, and the converter's own error of about ±2 LSB. Common remedies:
- Average several readings. Averaging 8 samples reduces random noise noticeably at a cost of under 1 ms.
- Add a 0.1 µF capacitor from the analog pin to GND, close to the pin, to filter high-frequency noise. This works only when the source impedance is low.
- Keep analog wires short and away from motors, relays, and switching supplies.
int readAveraged(int pin, int samples) {
long sum = 0;
for (int i = 0; i < samples; i++) {
sum += analogRead(pin);
}
return sum / samples;
}
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(readAveraged(A0, 8));
delay(200);
}Floating analog inputs
An analog pin with nothing connected is floating. It picks up stray electric fields and returns values that drift or follow nearby wires. The reading carries no information. This is common when a sensor is unplugged or a breadboard wire is loose.
Unlike digital pins, analog pins have no benefit from INPUT_PULLUP for measuring a voltage, because the pull-up resistor (about 20 kΩ to 50 kΩ) would itself pull the pin up and distort the measurement. Leave the pin in its default input mode when reading analog voltages.
Analog pins as digital pins
A0 to A5 can also work as digital pins. They are numbered 14 to 19, so pinMode(A0, OUTPUT) and digitalWrite(A0, HIGH) are valid. This is useful when you run out of digital pins. If you do this, that pin is no longer available for analog reading in the same circuit.
On the Uno, A4 and A5 also carry the I2C signals SDA and SCL. If you use an I2C device, A4 and A5 are occupied.
Note that analogWrite() does not work on A0 to A5. These pins have no PWM hardware. The analogRead() and analogWrite() names are misleading: one measures a voltage, and the other produces a PWM waveform, not a true analog voltage.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Reading jumps randomly | Floating pin or loose wire | Check the connection, and ground the pin through the sensor |
| Always 1023 | Pin tied to 5V, or the signal exceeds the reference | Check wiring, or change the reference |
| Always 0 | Pin tied to GND, or the sensor ground is not shared | Connect the sensor GND to the board GND |
| Voltage calculation is 0.2 V off | The supply is not exactly 5 V | Measure the 5V pin and use that value, or use `INTERNAL` |
| Channel reads follow the previous channel | High source impedance | Discard the first reading, or buffer the signal |
| Integer math gives 0 | `raw * 5 / 1023` written with ints that overflow, or `5 / 1023` evaluated as an integer | Use floating-point constants such as `5.0 / 1023.0` |
Exercise
A sensor outputs 0 to 1.0 V and is connected to A0. You call analogReference(INTERNAL) and analogRead(A0) returns 512. What voltage is the sensor outputting, and why is this setup better than the default reference for this sensor?
Show answer
The voltage is about 512 * (1.1 / 1024) ≈ 0.55 V. With the default 5 V reference, a 0 to 1.0 V signal would use only about 205 of the 1024 steps, with 4.88 mV per step. With the 1.1 V internal reference, the same signal uses about 930 steps at 1.07 mV per step, so the resolution is about 4.5 times finer. The internal reference also does not change when the USB supply voltage varies.
Exercise
You read A0 and then A1 in quick succession. A0 is connected to a potentiometer, and A1 is connected to a sensor through a 470 kΩ resistor. The A1 values drift toward the A0 values. What is causing this, and name two ways to fix it?
Show answer
The 470 kΩ source impedance is far above the recommended 10 kΩ, so the sample-and-hold capacitor cannot fully charge to the A1 voltage before the conversion, and it keeps some charge from A0. Fixes: discard the first reading after switching channels and take a second one, add a delay before reading, reduce the source resistance, or place a buffer such as an op-amp voltage follower between the sensor and A1.