Sub topic 3
Reference voltage and converting readings to volts
This chapter assumes you can write and upload a basic sketch with setup() and loop(). It also assumes you can open the Serial Monitor and wire a simple breadboard circuit. You should know that the ADC (analog-to-digital converter) turns a voltage into an integer from 0 to 1023 on a 10-bit board such as the Uno. Here you learn what that integer is measured against, how to turn it back into volts, and where the conversion goes wrong.
What the reading actually means
analogRead() does not return volts. It returns a fraction of a full-scale voltage, expressed in 1024 steps. The full-scale voltage is the reference voltage, often written Vref. It is the input voltage at which the ADC reports its top value.
- An input of 0 V gives 0.
- An input equal to Vref gives 1023.
- Anything in between gives a proportional value.
The same raw number therefore means different voltages depending on Vref. A reading of 512 is about 2.5 V when Vref is 5 V. It is about 0.55 V when Vref is 1.1 V.
Volts per step
A 10-bit ADC divides the range 0 to Vref into 1024 possible output codes, 0 to 1023. The size of one step, also called one LSB (least significant bit), is:
step = Vref / 1024With Vref = 5 V, one step is 5 / 1024, which is about 4.88 mV. Any change in the input smaller than about 4.88 mV may not change the reading at all. This is the resolution limit of the measurement. It is not a fault in your wiring or code.
Converting a reading to volts
The usual conversion is:
volts = raw * Vref / 1023This maps raw = 1023 to exactly Vref, which is convenient for display. The ADC is ideally defined as raw = floor(Vin * 1024 / Vref), so a more exact estimate of the input is raw * Vref / 1024. Each code covers a band of voltages, and that formula gives the bottom edge of the band. The two formulas differ by about 0.1 percent. That is smaller than the ADC's own accuracy, which is about plus or minus 2 LSB on an ATmega328P, so either is fine for this tutorial. Pick one and use it everywhere.
This sketch reads the potentiometer on A0, converts the value assuming a 5 V reference as on an Arduino Uno, and prints both numbers:
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);
}Turn the knob from one end to the other. The output should run from about raw=0 volts=0.00 to about raw=1023 volts=5.00.
Why `5.0` and not `5`
The decimal point matters. In C++, dividing two integers gives an integer result.
5 / 1023evaluates to0, because the fraction is truncated.5.0 / 1023.0is a floating-point division and gives about 0.004888.
If your voltage always prints as 0.00, check for integer division first.
Doing it in integers
Floating-point math works on AVR boards but is slow and adds code size. If you only need millivolts, integer math is enough:
const int POT_PIN = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(POT_PIN);
long millivolts = raw * 5000L / 1023;
Serial.print(millivolts);
Serial.println(" mV");
delay(100);
}The L suffix makes 5000L a long, so the multiplication happens in 32 bits. This matters on the Uno, where int is 16 bits and holds at most 32767. The product 1023 * 5000 is 5,115,000, and without the L it overflows silently and prints nonsense for readings above 6.
Where the reference comes from
On an Uno the reference is selectable. The analogReference() function chooses it, and you call it in setup() before you read.
| Argument | Reference used (Uno, ATmega328P) | Full scale | Step size |
|---|---|---|---|
| `DEFAULT` | Board supply voltage, Vcc | 5 V | about 4.88 mV |
| `INTERNAL` | Built-in bandgap reference | 1.1 V | about 1.07 mV |
| `EXTERNAL` | Voltage you apply to the AREF pin | Your voltage | Your voltage / 1024 |
If you never call analogReference(), you get DEFAULT. The reference is then the supply rail itself, which has two consequences.
Consequence 1: the reference is only as accurate as your supply
Powered over USB, an Uno's 5 V rail can sit anywhere from roughly 4.7 V to 5.2 V, and it moves with load. If the real supply is 4.85 V and your code assumes 5.0, every converted voltage is about 3 percent too high.
A potentiometer wired between 5V and GND does not suffer from this when you treat its output as a fraction of travel. Both the pot and the reference sag together, so raw / 1023 stays correct. The error only appears when you convert to absolute volts.
If you need accurate absolute volts, measure the 5V pin with a multimeter and use that number:
const int POT_PIN = A0;
const float VREF = 4.87; // measured with a multimeter on the 5V pin
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(POT_PIN);
float volts = raw * (VREF / 1023.0);
Serial.println(volts, 3);
delay(200);
}Printing three decimals here is only for display. The ADC cannot resolve differences below one 4.88 mV step, so digits beyond that are not meaningful.
Consequence 2: a more stable reference needs a different choice
INTERNAL selects a fixed 1.1 V bandgap reference. It does not change with the supply, and its steps are about 4.5 times finer. The cost is a much smaller input range. Any input above 1.1 V reads 1023, so a pot wired across 5 V would saturate in the first fifth of its travel.
This option suits small signals, such as a temperature sensor that outputs 10 mV per degree. It does not suit a full-range potentiometer.
const int SENSOR_PIN = A0;
const float VREF = 1.1;
void setup() {
analogReference(INTERNAL);
Serial.begin(9600);
}
void loop() {
int raw = analogRead(SENSOR_PIN);
float volts = raw * (VREF / 1023.0);
Serial.println(volts, 3);
delay(200);
}With this sketch, only inputs between 0 and 1.1 V give meaningful readings. Do not connect a 5 V pot wiper to A0 here and expect a proportional response.
After you switch references, the first reading or two may be inaccurate while the reference settles. Discard one analogRead() after changing it if the first value matters.
Common errors
- Voltage always prints 0.00. You used integer division. Write
5.0 / 1023.0or cast tofloat. - Voltages are garbled above about 0.1 V in integer math. The product overflowed a 16-bit
int. Uselongand anLsuffix. - The reading pins at 1023 early in the knob's travel. The input exceeds Vref. Check that you did not leave
analogReference(INTERNAL)in the sketch. - Volts disagree with the multimeter by a few percent. The supply is not exactly 5 V. Measure it and use that value as
VREF. - The multimeter and sketch disagree by less than one step. That is quantization, and no code change fixes it.
- Readings change when you plug in a motor or LED strip. Because
DEFAULTuses Vcc as the reference, load changes on the supply move the reference. UseINTERNALor a stable external reference if absolute accuracy matters.
Other boards
The formulas stay the same on other boards. Only the numbers change. A 3.3 V board reports its top value at 3.3 V, and a board with a 12-bit ADC has 4096 steps (top value 4095). Substitute the board's Vref for 5 and its maximum raw value for 1023. A later chapter covers the board-specific details.
Exercise
Your Uno runs from USB with the default reference, and you measure 4.90 V on the 5V pin. analogRead() returns 614. Using volts = raw * Vref / 1023, what voltage is on the pin, to two decimals? Then give the step size in millivolts for this reference.
Show answer
volts = 614 * 4.90 / 1023 = 2.94 V (about 2.941). The step size is 4.90 / 1024, which is about 4.79 mV. Using 5.0 instead of the measured 4.90 would give 3.00 V, an error of about 2 percent.
Exercise
A colleague writes int mv = raw * 5000 / 1023; on an Uno and gets wrong values once the knob passes the lowest few positions. What is the bug, and how do you fix it?
Show answer
On the Uno an int is 16 bits, so raw 5000 overflows once the product exceeds 32767, which happens for raw above 6. Make the arithmetic 32-bit: `long mv = raw 5000L / 1023;`.