Sub topic 10
Analog inputs: A0 to A5 and the 10-bit ADC
This chapter assumes you understand voltage, current, and resistance, and that you can use setup() and loop() in an Arduino sketch. You should also be able to connect a simple sensor to a breadboard.
What an analog input does
An analog input measures a voltage that can have many values between two limits. On an Arduino Uno, pins A0 through A5 connect to the microcontroller's analog-to-digital converter, or ADC. An ADC changes a voltage into a number that software can use.
The Uno's ADC has 10-bit resolution. Ten bits can represent 2¹⁰, or 1,024, values. With the default reference voltage, analogRead() returns a number from 0 to 1023:
0represents a voltage near 0 V.1023represents a voltage near the reference voltage.- Values between them represent voltages between those limits.
The default reference is usually the board's 5 V supply, called AVcc. The actual supply may be slightly above or below 5 V, so the conversion is an estimate unless you measure or configure the reference more precisely.
| Reading | Approximate voltage with a 5 V reference |
|---|---|
| 0 | 0.00 V |
| 512 | 2.50 V |
| 1023 | 5.00 V |
The ADC does not measure current or resistance directly. A sensor circuit must first produce a voltage. For example, a potentiometer produces an adjustable voltage, while a thermistor and a fixed resistor form a voltage divider whose voltage changes with temperature.
Reading A0 in a sketch
The following sketch reads A0, converts the result to volts, and sends both values to the Serial Monitor.
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
float volts = raw * (5.0 / 1023.0);
Serial.print("ADC: ");
Serial.print(raw);
Serial.print(" Voltage: ");
Serial.print(volts, 2);
Serial.println(" V");
delay(500);
}analogRead(A0) starts a conversion and returns its result. The expression 5.0 / 1023.0 estimates the voltage represented by one count. The Serial Monitor must use the same baud rate as Serial.begin(), which is 9600 in this example.
You do not normally need pinMode() for an analog input. The Arduino core configures the ADC input when analogRead() uses it. A0 through A5 can also act as digital pins, but a pin has only one active function at a time.
Reference voltage and measurement limits
The reference voltage defines the ADC's measurement range. If the reference is 5 V, a 2.5 V input produces roughly 512. If the reference changes to 4.8 V, the same input produces a larger reading because 2.5 V occupies more of the ADC range.
The Uno also provides an AREF pin for an external reference. Changing the ADC reference with analogReference() requires care. The selected reference must be connected correctly, and the input voltage must remain within the selected range. Do not connect an external voltage to AREF while using the default reference without checking the board documentation.
The ADC has finite resolution. With a 5 V reference, one count represents about 4.9 mV. Small signal changes below that size may disappear in rounding and electrical noise. Averaging several readings can reduce random noise, but it cannot create real additional resolution.
Wiring and common errors
Connect the sensor circuit's ground to the Uno's GND. A voltage without a shared reference has no reliable meaning. Keep the signal within the allowed voltage range, and avoid leaving A0 disconnected. A disconnected input can pick up electrical noise and produce changing readings.
A sensor with a very high output resistance may charge the ADC's internal sampling capacitor slowly. This can make readings inaccurate, especially when switching between analog pins. Use a suitable voltage divider, a buffer circuit, or a short delay between channel changes when the sensor requires it.
Exercise
With a 5 V reference, what voltage does an ADC reading of 768 represent approximately?
Show answer
About 3.75 V, because 768 × 5.0 ÷ 1023 is approximately 3.75.