Electronics
How to measure a voltage with an Arduino
To measure a voltage with an Arduino, connect it to an analog pin, A0 to A5, read it with analogRead, and scale the count back to volts: V = reading × 5 ÷ 1024. The pin can take 0 to 5 V on an Uno. For anything higher, put a voltage divider in front and multiply back up.
From count to volts
The board's ADC compares the pin with its 5 V reference and gives a whole number from 0 to 1023, rounding down to the mark below:
Each step is 5 ÷ 1024 ≈ 4.88 mV. 2.5 V reads 512, and 4.0 V reads 819.
Live experiment
Read the ruler
The ADC is a ruler with 1024 marks between 0 and 5 V. Slide the voltage on A0, or nudge it a millivolt at a time, and the magnifier shows which mark analogRead gives: the one at or below. Then try exactly 5 V.
“Analog input” is lesson 6 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 54 before it first.
Measuring more than 5 V
Scale the voltage down so it never passes 5 V. A ÷3 divider, 20 kΩ on top and 10 kΩ below, turns a 12 V battery into 4 V and keeps anything up to 15 V safe. Then multiply back by 3 in code:
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
float atPin = reading * 5.0 / 1024; // volts on A0
Serial.println(atPin * 3); // the battery: 12.00
delay(1000);
}
Write 5.0, not 5, so the sum keeps its decimals. 819 × 5 ÷ 1024 × 3 ≈ 12.0 V.
Accuracy comes from the reference
The ADC doesn't know what a volt is. It only reports the input as a fraction of its reference, which on an Uno is its own 5 V supply. Powered from USB, that can be anywhere from about 4.75 to 5.25 V. If it's really 4.8 V, a true 2.5 V reads 533, and the sum reports 2.60 V: 4% high.
Three fixes, cheapest first:
- Measure the real supply with a multimeter and put that number in place of 5.0.
- Use the internal reference.
analogReference(INTERNAL)switches the Uno to a built-in 1.1 V reference that doesn't follow the supply; scale your divider so the pin stays under 1.1 V. - Average several readings to smooth out noise, at the cost of a slower answer.
Don't drive it from too high a resistance
The ADC samples by briefly charging a tiny capacitor, so it works best when whatever drives the pin has a resistance of 10 kΩ or less. A 20 kΩ and 10 kΩ divider looks like about 6.7 kΩ to the pin, which is fine. A divider of megohms would read low and drift, and a 100 nF capacitor from the pin to GND helps there.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Analog input” is lesson 6 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 54 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: a whole number from 0 to 1023. The ADC compares the voltage with its 5 V reference and gives a whole number from 0 (0 V) to 1023 (5 V), not volts. digitalRead is the one that says HIGH or LOW.
- Why not “the voltage, in volts”? The ADC gives a count, not volts. Multiply by 5 and divide by 1024 to turn it into volts.
- Why not “HIGH or LOW”? HIGH or LOW is digitalRead's answer. analogRead measures how far between 0 and 5 V the pin is.
Show the answer
Answer: A0 to A5. Only the six analog pins, A0 to A5, are wired to the board's voltage ruler. The others only say HIGH or LOW.
- Why not “The pins marked ~”? The ~ pins are PWM outputs, for analogWrite. Measuring a voltage needs the analog inputs, A0 to A5.
- Why not “Only pin 13”? Pin 13 is a digital pin with the built-in LED on it. It has no connection to the ADC.
- Why not “Any pin”? Only six pins are wired to the ADC. The rest can only read HIGH or LOW.
Show the answer
Answer: So noise picked up between the filter and the pin is filtered too. A filter only cleans what passes through it. Any wire after it is an antenna that can pick up fresh noise.
- Why not “It charges faster there”? The capacitor charges at a rate set by R × C wherever it sits. Its position matters for the noise it lets past.
- Why not “It doesn't matter”? It does: wire between the filter and the pin acts as an antenna, so noise picked up there reaches the ADC unfiltered.
- Why not “It looks neater”? Placement here is about electrical noise, not looks. Any wire after the filter can pick up noise the filter never sees.
“Analog input” is lesson 6 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 54 before it first.
Questions people ask
What is the maximum voltage an Arduino analog pin can read?
On a 5 V board like the Uno, 5 V, the same as its reference. Never put more than that, or anything below 0 V, on the pin. For bigger voltages, scale them down first with a voltage divider.
Why are my Arduino voltage readings a few percent off?
analogRead compares the input with the board's own 5 V supply, and on USB that can be anywhere from about 4.75 to 5.25 V. If it's really 4.8 V, every reading comes out about 4% high. Measure the real 5 V with a meter and use that number in your sum.
How accurate is an Arduino's analog input?
It splits 0 to 5 V into 1024 steps of about 4.9 mV, so it can't see anything smaller. The reference, the resistor tolerances in any divider, and noise usually limit accuracy more than the step size does.
Related topics
- How Arduino analogRead worksHow analogRead turns 0–5 V into 0–1023, and how to scale it for analogWrite.
- How to measure a 12 V battery with a voltage dividerScale 12 V down to 4 V for a meter or a microcontroller, without draining the battery.
- Voltage divider explainedTwo resistors that split a voltage: the formula, potentiometers, and why a load drags the output down.
- What is aliasing, and what is the Nyquist rate?Sample too slowly and a fast wheel seems to crawl backwards.