Sub topic 2
How the ADC Turns Voltage into a Number
Assume you know the structure of an Arduino sketch, can upload code, and can use the Serial Monitor. You should also know basic voltage and ground connections. This chapter focuses on the analog-to-digital converter, or ADC.
Analog voltage becomes a digital value
A voltage can vary continuously. For example, a sensor might produce any voltage between 0 V and 5 V. A microcontroller works with digital numbers, so it needs a circuit that measures the voltage and represents it as a number.
An ADC is a circuit that converts an analog voltage into a digital value. The Arduino Uno has a 10-bit ADC. Ten bits can represent 2¹⁰, or 1024, different values. The returned values range from 0 through 1023.
- A voltage near 0 V produces a value near 0.
- A voltage near the reference voltage produces a value near 1023.
- Voltages between them produce values between 0 and 1023.
The ADC does not preserve every possible voltage. It rounds each input to the nearest available step. This process is called quantization.
Resolution determines the steps
Resolution is the number of bits used for each measurement. The number of possible codes is calculated as:
number of codes = 2^number of bits
For a 10-bit ADC:
2¹⁰ = 1024 codes
The code range has 1024 values, but the highest code is 1023 because counting starts at zero.
| ADC resolution | Number of codes | Raw value range |
|---|---|---|
| 8 bit | 256 | 0 to 255 |
| 10 bit | 1024 | 0 to 1023 |
| 12 bit | 4096 | 0 to 4095 |
Higher resolution provides smaller voltage steps. It does not automatically make the measurement more accurate. Accuracy also depends on the reference voltage, electrical noise, the ADC itself, and the circuit connected to the input.
Reference voltage sets the scale
The ADC needs a maximum voltage for its scale. This voltage is the reference voltage, often written as Vref. On a typical Arduino Uno, the default reference is the board’s 5 V supply, called AVcc.
The voltage represented by a raw reading can be estimated with this formula:
voltage = raw reading × Vref ÷ (2^bits − 1)
For a 10-bit ADC with a 5 V reference:
voltage = raw reading × 5.0 ÷ 1023
A reading of 512 represents approximately:
512 × 5.0 ÷ 1023 = 2.50 V
The formula assumes that Vref is actually 5.0 V. A board powered from USB may have a supply closer to 4.8 or 4.9 V, so the calculated result is an estimate unless you measure or otherwise know the reference voltage.
Test the conversion
This sketch reads A0 and calculates the approximate voltage for a 10-bit ADC using a 5 V reference.
const int INPUT_PIN = A0;
const float REFERENCE_VOLTAGE = 5.0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(INPUT_PIN);
float voltage = raw * REFERENCE_VOLTAGE / 1023.0;
Serial.print("raw = ");
Serial.print(raw);
Serial.print(", voltage = ");
Serial.print(voltage, 3);
Serial.println(" V");
delay(250);
}If the input is connected to ground, the raw value should be close to 0. If it is connected to the board’s 5 V supply, it should be close to 1023. Intermediate voltages should produce intermediate readings.
Check your understanding
Exercise
An Arduino Uno returns a raw ADC value of 205. Using a 5.0 V reference and a 10-bit ADC, what voltage does the value represent approximately?
Show answer
Use 205 × 5.0 ÷ 1023. The result is approximately 1.00 V.