Your project runs on a battery, and you want to show its charge on a display, or stop the motor before the battery is completely flat. You need to measure its voltage. The problem is that an analog pin can't take more than 5 V (or 3.3 V on the ESP32), and the battery may be 12.
The voltage divider
Two resistors in series, with the pin in the middle. See the voltage divider.
V_pin = V_bat × R2 ÷ (R1 + R2)
Example: a 3S lithium pack (maximum 12.6 V) with an Uno:
- R1 = 20k (top), and R2 = 10k (bottom).
- The voltage on the pin = 12.6 ÷ 3 = 4.2 V. Below 5 ✓.
- The maximum voltage you can measure = 5 × 3 = 15 V.
Work out your values with the voltage divider calculator.
Ready-made values
| Battery | Maximum voltage | For the Uno (5 V) | For the ESP32 (3.3 V) |
|---|---|---|---|
| Single lithium cell | 4.2 | Direct, no divider | 100k + 100k |
| 2S lithium | 8.4 | 10k + 10k | 100k + 47k |
| 3S lithium | 12.6 | 20k + 10k | 100k + 27k |
| 12 V lead-acid (while charging) | 14.4 | 20k + 10k | 100k + 22k |
| 24 V lead-acid (while charging) | 28.8 | 56k + 10k | 100k + 10k |
The first figure is R1 (top) and the second R2 (bottom). Always leave a margin: a lead-acid battery on charge rises well above 12, which is the reason for the figures in the table.
The code
const float R1 = 20000.0, R2 = 10000.0;
const float VREF = 5.0;
void loop() {
int raw = analogRead(A0);
float vPin = raw * VREF / 1023.0;
float vBat = vPin * (R1 + R2) / R2;
Serial.println(vBat, 2);
delay(1000);
}
A worked example: a reading of raw = 860 → the voltage on the pin is 860 × 5 ÷ 1023 ≈ 4.20 V → the battery is 4.20 × 3 ≈ 12.6 V. So the pack is fully charged.
Why is the reading inaccurate?
1. The 5 V isn't exactly 5
The ADC measures a ratio of the supply voltage. If the USB supplies 4.85 V, all readings will be wrong by the same proportion.
- Measure the actual 5V with a multimeter and put it in
VREF. - Or use the internal reference:
analogReference(INTERNAL)on the Uno (about 1.1 V), and choose the divider so the maximum voltage stays below 1.1. More stable than the supply, but it needs a one-off calibration.
2. The resistors themselves
1% resistors are better than 5%. Or measure the two actual resistors and put their values in the code.
3. Calibration
Measure the battery with the multimeter, compare with the Arduino's reading, and multiply by a correction factor. For example, the multimeter said 12.40 and the Arduino 12.15 → the factor is 12.40 ÷ 12.15 ≈ 1.021.
4. Noise
Take an average, or use a moving average that gives the new reading a small weight:
float vAvg = 0;
vAvg = vAvg * 0.9 + vBat * 0.1; // on every reading
Is the Uno running directly from the battery?
If the battery feeds the Arduino's 5V pin directly (with no regulator), VREF itself changes with the battery, so an ordinary measurement won't see any change! The fix: measure the internal 1.1 V reference relative to the supply, and work out the supply from it:
long readVcc() { // for the Uno and Nano (ATmega328P)
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2);
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
long r = ADCL; r |= ADCH << 8;
return 1125300L / r; // in millivolts
}
The figure 1125300 = 1.1 × 1023 × 1000. And because the 1.1 itself varies from chip to chip, calibrate it once with a multimeter and adjust the number.
Don't let the divider drain the battery
The divider draws current all the time: 12.6 ÷ 30k ≈ 0.42 mA. For a project that runs for months, that's a lot.
- Larger resistors (for example 200k and 100k) → less current. But the ADC prefers a source resistance of about 10k or less, so add a 100 nF capacitor from the pin to ground to keep the reading steady.
- Or switch off the divider with a MOSFET and turn it on only when reading. This matters a lot in ESP32 deep sleep projects.
With an ESP32
- The maximum voltage on the pin is 3.3 V, and the resolution is 12-bit (0–4095).
- Its ADC is non-linear, especially at the extremes. Use
analogReadMilliVolts(), which applies the factory calibration. - ADC2 doesn't work while Wi-Fi is on. Use ADC1 pins (32–39).
- Keep the maximum voltage on the pin around 2.5–2.8 V rather than 3.3, because the top of the range is less accurate.
From voltage to percentage
Lithium voltage is non-linear with capacity, and drops under load. So the percentage from voltage is approximate: read under a light load, and average several readings. See battery life.
An approximate table for a single lithium cell at rest (no load):
| Voltage | Approximate percentage |
|---|---|
| 4.20 | 100% |
| 4.00 | 80% |
| 3.85 | 60% |
| 3.75 | 40% |
| 3.65 | 20% |
| 3.50 | 5% |
For a 3S pack, multiply the voltages by 3. In code, the easiest approach is to make a table and pick the nearest row, instead of map, which assumes a straight line.
Cut the load before the battery runs out
Lithium is damaged if it falls below about 3 V per cell. So if there's no BMS protecting it, add protection in code:
if (vBat < 9.6) cutoff = true; // 3.2 V per cell in 3S
if (vBat > 10.5) cutoff = false; // back on after it has charged a bit
digitalWrite(LOAD_PIN, cutoff ? LOW : HIGH);
The gap between the two thresholds (hysteresis) stops the load switching on and off repeatedly: as soon as the load switches off, the battery voltage rises a little by itself.
Common faults
| Fault | Usual cause |
|---|---|
| The reading is stuck at 1023 | The voltage on the pin is above 5 V; the divider is wrong |
| The reading jumps around | Large resistors with no capacitor |
| A constant error of a few per cent | VREF isn't exactly 5, or the resistor tolerance |
| The reading doesn't change as the battery drains | The Uno is powered directly from the battery; use readVcc |
| The ESP32 reads zero with Wi-Fi on | A pin from ADC2 |
FAQ
Can I connect the battery directly if it's below 5 V? With the Uno, yes, for a single lithium cell. But if the Arduino is off and the battery is connected, current can enter the chip through the pin. Add a 10k resistor in series as protection.
Can I measure the current too? You need a current sensor. See current sensing.