Sub topic 2
How the ADC turns voltage into a number
This chapter assumes you can write a basic sketch with setup() and loop(), upload it, and open the Serial Monitor. It also assumes you know what voltage and ground mean and can wire a breadboard. The chapter covers how the analog-to-digital converter (ADC) on an Arduino Uno turns a voltage into an integer. It then covers what that integer does and doesn't tell you, and the errors people commonly make when reading it.
Why a converter is needed
A microcontroller computes with integers. A sensor or a potentiometer produces a voltage that can take any value in a range, such as 1.37 V or 3.021 V. The ADC is the circuit that bridges the two. It samples the voltage on a pin at one instant and returns a whole number that represents it.
Most digital pins can only report HIGH or LOW. The ADC reports one of many levels, so you can tell 1 V from 2 V from 4 V.
The working model
On an Uno (ATmega328P), the ADC is a 10-bit successive-approximation converter. It compares the input voltage to a reference voltage and produces a result from 0 to 1023.
- 10 bits gives 2^10 = 1024 possible results.
- The lowest result, 0, means the input was at or near 0 V (ground).
- The highest result, 1023, means the input was at or above roughly the reference voltage.
- Everything in between is a proportion of the reference.
The core relationship is:
reading = floor(Vin / Vref * 1024) (clamped to 1023)The result is a ratio. The ADC does not know anything about volts. It only knows what fraction of the reference the input is. This matters later when the reference changes or when the supply voltage sags.
Step size
The smallest change the ADC can distinguish is one step, also called one least significant bit (LSB). With a 5 V reference:
step = 5 V / 1024 ≈ 4.88 mVAn input change smaller than about 4.9 mV may not change the reading at all. Resolution (how finely the range is divided) is not the same as accuracy (how close the reading is to the true voltage). A 10-bit ADC can have a few steps of error from offset, gain, and noise. The datasheet for the ATmega328P specifies an absolute accuracy of about ±2 LSB.
Table of example readings
| Input voltage | Reading (5 V reference) |
|---|---|
| 0.00 V | 0 |
| 1.25 V | 256 |
| 2.50 V | 512 |
| 3.75 V | 768 |
| 5.00 V | 1023 |
The 5.00 V row shows the clamp. A true calculation gives 1024, but the largest value the converter can produce is 1023.
How the conversion happens internally
A successive-approximation ADC works like a binary search. A capacitor holds the sampled voltage. An internal digital-to-analog converter (DAC) then produces trial voltages, and a comparator says whether the trial is above or below the held input. One bit is decided per step, starting with the most significant bit.
The sequence is:
- The ADC connects the selected pin to a small sample-and-hold capacitor.
- The capacitor charges to the input voltage.
- The input is disconnected, so the held value stays fixed during conversion.
- The converter tests each bit from bit 9 down to bit 0, ten comparisons in total.
- The result is placed in the ADC data registers.
On the Uno, the ADC clock is the CPU clock divided by 128, which is 125 kHz at 16 MHz. A conversion takes about 13 ADC clock cycles, so a single analogRead() takes about 104 microseconds. That limits you to roughly 9600 readings per second. Chapter-level takeaways are that reading is not instant and that you can read at most about ten thousand times per second at default settings.
The input multiplexer
The Uno has one ADC and six analog input pins, A0 to A5. An analog multiplexer connects one pin at a time to the converter. This has two consequences.
- You cannot read two pins at the same instant. Reading A0 and then A1 means two separate samples taken at different times.
- When the multiplexer switches from one pin to another, charge left on the sample capacitor from the earlier pin can disturb the first reading from the new pin. This is called channel crosstalk or ghosting. It shows up most when the source connected to the pin has a high output impedance.
A common workaround is to discard the first reading after switching channels, or to read twice and keep the second result.
int readSettled(int pin) {
analogRead(pin); // throwaway read lets the sample capacitor settle
return analogRead(pin); // this one is kept
}Source impedance
The sample capacitor must charge to the input voltage in the short sampling window. The ATmega328P datasheet recommends a source impedance of 10 kΩ or less. A potentiometer between 1 kΩ and 10 kΩ meets this. A 100 kΩ potentiometer can still work, but readings may lag or be skewed after channel switching, and noise pickup rises.
From the number back to volts
To convert a reading back to a voltage, reverse the formula:
volts = reading * Vref / 1024Many tutorials use 1023 as the divisor, and the sketch in this tutorial series does too. The difference is small. Dividing by 1024 matches how the ADC actually works, since each step is Vref / 1024 wide. Dividing by 1023 makes a full-scale reading come out as exactly Vref, which is convenient for display. For most hobby uses either is fine. Choose one and use it consistently.
const int POT_PIN = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(POT_PIN);
float volts = raw * (5.0 / 1023.0);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" volts=");
Serial.println(volts, 2);
delay(100);
}Open the Serial Monitor at 9600 baud and turn the knob of a potentiometer wired to A0. With the wiper at the ground end you should see a value near 0. At the 5 V end you should see a value near 1023.
What the reference is
The converter's scale depends on its reference. By default on an Uno, the reference is AVcc, which is the chip's supply voltage, nominally 5 V. The analogReference() function can change it to an internal 1.1 V source or to an external voltage on the AREF pin. The next chapter covers this in detail. For now, two points matter.
- The default reference is the supply, so if the supply is 4.7 V from a weak USB port, a full-scale reading represents 4.7 V and your "5.0" in the conversion formula is wrong.
- A reading taken as a ratio of the supply is a feature when the sensor is powered from the same supply. A potentiometer wired between 5 V and ground gives the same reading whether the supply is 4.7 V or 5.0 V, because both the input and the reference move together.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Reading stuck at 0 | Wiper not connected, or wired to ground | Check that the middle terminal goes to the analog pin |
| Reading stuck near 1023 | Pin tied to 5 V, or wiper wire loose and floating high | Check the wiring and use a firm connection |
| Values jump randomly with nothing connected | Floating input picks up noise | Connect the pin to a defined source |
| Volts print as 0.00 or 5.00 only | Integer division in the formula, such as `raw * (5 / 1023)` | Use `5.0` so the math is floating point |
| Reading never reaches 1023 | Supply or reference differs from the assumption | Measure 5 V with a multimeter |
| Second channel reads like the first | Multiplexer settling | Discard the first read after switching |
The integer division trap
The expression 5 / 1023 is evaluated with integers and equals 0. Multiplying anything by it gives 0. Writing 5.0 / 1023.0 forces floating-point arithmetic. This is the most common error in beginner conversion code.
int raw = 512;
float wrong = raw * (5 / 1023); // 0.0
float right = raw * (5.0 / 1023.0); // about 2.50Type size
On the Uno, int is 16 bits and holds -32768 to 32767. A single reading fits easily, but a sum of many readings can overflow. Summing ten readings of 1023 gives 10230, which still fits. Summing fifty readings gives 51150, which does not. Use long for accumulators.
Pins are not the only inputs
analogRead() accepts either a pin number such as A0 or a channel number such as 0. The constants A0 to A5 are defined for the board you selected. On a board with different pin numbering, using the A0 name keeps the code portable. The ATmega328P also has internal channels, such as the 1.1 V bandgap reference and the internal temperature sensor, but analogRead() does not expose them directly.
Putting it together
The key points about the ADC are:
- It returns a ratio of the input to the reference, as an integer from 0 to 1023 on a 10-bit converter.
- One step on a 5 V reference is about 4.88 mV, and accuracy is a few steps worse than resolution suggests.
- A conversion takes about 104 µs on an Uno, one pin at a time through a multiplexer.
- Source impedance of 10 kΩ or less, and a throwaway read after switching channels, give cleaner results.
- Converting to volts depends on knowing the true reference.
Exercise
A sketch on an Uno with the default 5 V reference prints a reading of 307. What voltage does that represent, using the divisor 1024, and what is the size of one step in millivolts?
Show answer
Volts = 307 * 5 / 1024 ≈ 1.499 V, so about 1.50 V. One step is 5 V / 1024 ≈ 4.88 mV.
Exercise
A student writes float volts = analogRead(A0) * (5 / 1023); and always sees 0.00. Explain why and give the corrected line.
Show answer
5 / 1023 is integer division and evaluates to 0, so the product is 0. Use floating-point constants: float volts = analogRead(A0) * (5.0 / 1023.0);
Exercise
You read A0 and then immediately A1 on a pin with a 100 kΩ source, and the A1 value seems to follow A0. What causes this and what is one fix?
Show answer
The sample capacitor keeps some charge from the previous channel and a high source impedance cannot recharge it to the new voltage in the short sampling window. Fixes include reading the pin twice and keeping the second value, or lowering the source impedance to 10 kΩ or less.