Sub topic 13
Reading battery voltage with a voltage divider
This chapter assumes you know Ohm's law, can build a circuit on a breadboard, and can upload a sketch that calls analogRead. You also need the battery chemistries and ratings from earlier chapters. Here you will wire a two-resistor divider to an analog pin, convert the reading back to battery volts, and avoid the errors that make battery readings drift.
Why you cannot connect a battery to an analog pin
The ADC (analog-to-digital converter) on a classic 5 V Arduino accepts 0 V to its reference voltage, which is normally the 5 V supply rail. A 9 V battery on A0 would exceed that limit and can damage the pin. Even a fresh 4.2 V LiPo cell on a 3.3 V board is over the limit.
The fix is a voltage divider. It uses two resistors in series to produce a fixed fraction of the battery voltage. You measure that fraction and scale it back up in software.
The divider equation
Put R1 between battery positive and the analog pin. Put R2 between the pin and ground.
The voltage at the node is:
Vpin = Vbattery × R2 / (R1 + R2)
Solving for the battery voltage gives the line used in the sketch:
Vbattery = Vpin × (R1 + R2) / R2
With R1 = R2 = 100 kΩ, the ratio is 1/2. A 9 V battery puts 4.5 V on the pin, and the pin never exceeds 5 V until the battery exceeds 10 V.
Choosing resistor values
Work backward from the highest voltage the battery can reach, not its nominal voltage. A "9 V" alkaline can read 9.6 V when new, and a 12 V lead-acid battery can reach about 14 V while charging. Pick a ratio so that the highest voltage lands below the ADC reference, with a little room.
| Battery | Maximum voltage | Board reference | Suggested R1 / R2 | Pin voltage at maximum |
|---|---|---|---|---|
| 1S LiPo | 4.2 V | 3.3 V | 100k / 100k | 2.1 V |
| 9 V alkaline | about 9.6 V | 5 V | 100k / 100k | 4.8 V |
| 2S LiPo | 8.4 V | 5 V | 100k / 100k | 4.2 V |
| 12 V lead-acid | about 14.4 V | 5 V | 200k / 100k | 4.8 V |
Two competing concerns set the absolute resistor size:
- Larger resistors waste less battery current. The 200 kΩ total in the 9 V example draws 9 V / 200 kΩ = 45 µA, all the time. That is negligible on a project that draws 50 mA, but it is a real drain on a sleeping project that draws 20 µA.
- Smaller resistors give the ADC a more stable input. The ADC on AVR boards such as the Uno is specified for a source impedance of about 10 kΩ or less. Two 100 kΩ resistors look like roughly 50 kΩ from the pin's side.
The base sketch
This sketch reads the pin, converts the count to a pin voltage, and scales it back to battery volts.
// Battery+ -> R1 (100k) -> A0 -> R2 (100k) -> GND
const float R1 = 100000.0;
const float R2 = 100000.0;
const float VREF = 5.0; // ADC reference; assumes a 5 V board
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
float vPin = raw * VREF / 1023.0;
float vBattery = vPin * (R1 + R2) / R2;
Serial.print("Battery: ");
Serial.print(vBattery, 2);
Serial.println(" V");
delay(1000);
}The ADC on a 10-bit board returns 0 to 1023, so one count is VREF / 1023. With a 5 V reference and a 2:1 divider, one count equals about 9.8 mV of battery voltage. Fractions of a hundredth of a volt in the printout are noise, not resolution.
Why the simple version drifts
Four problems appear quickly on real hardware. All of them are fixable.
1. The reference is not exactly 5.0 V
VREF = 5.0 is an assumption. The default analog reference is the board's supply rail. If the board is powered from USB, that rail may be 4.8 V to 5.1 V. If the board is powered from a regulator fed by the battery, the rail sags as the battery nears the regulator's dropout voltage. The ADC then misreports the battery precisely when you care most about it.
Two fixes exist:
- Use the stable internal reference. On an Uno or Nano,
analogReference(INTERNAL)selects a roughly 1.1 V reference, so the divider must bring the maximum battery voltage below 1.1 V. For 9.6 V, use R1 = 820 kΩ and R2 = 100 kΩ for a ratio of about 0.109 (1.05 V at the pin), but that raises the source impedance problem below. Other boards use different names and values, so check your board's documentation. - Measure the real rail once with a multimeter and store that number as
VREF, if the board will always run from the same supply.
2. High source impedance causes inaccurate readings
The ADC briefly connects a small sampling capacitor to the pin. A high-impedance divider cannot refill that capacitor fast enough, so the reading is low or depends on the previously read channel. Place a 100 nF capacitor from the analog pin to ground. It acts as a local reservoir and also filters noise. Reading the pin twice and discarding the first result also helps.
3. Resistor tolerance sets your accuracy
A 5% resistor pair can be off by about 5% or more in the ratio. Use 1% resistors, then calibrate with a multimeter, as shown below.
4. Voltage under load is not the resting voltage
A battery's terminal voltage sags when the load draws current, as described in the internal-resistance chapter. A reading taken while a motor starts will look much lower than one taken at idle. Read at a consistent point in your program, such as while motors are off, and average several samples.
An improved sketch
This version averages 16 samples, discards a throwaway first read, and applies a calibration factor measured against a multimeter.
const float R1 = 100000.0;
const float R2 = 100000.0;
const float VREF = 5.0;
const float CAL = 1.000; // multimeter reading / sketch reading
const int SAMPLES = 16;
float readBatteryVolts() {
analogRead(A0); // discard: lets the ADC input settle
long sum = 0;
for (int i = 0; i < SAMPLES; i++) {
sum += analogRead(A0);
delay(2);
}
float raw = sum / (float)SAMPLES;
float vPin = raw * VREF / 1023.0;
return vPin * (R1 + R2) / R2 * CAL;
}
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Battery: ");
Serial.print(readBatteryVolts(), 2);
Serial.println(" V");
delay(1000);
}To calibrate, measure the battery with a multimeter and record the sketch's reading. Set CAL to the multimeter value divided by the sketch value. For example, 8.95 V measured against 9.12 V printed gives CAL = 0.981.
Turning voltage into charge level
Battery voltage is only a rough indicator of remaining capacity, and the relationship depends on chemistry. A lithium-ion cell falls from 4.2 V to about 3.7 V early in its discharge, stays near 3.6 V to 3.8 V for most of its capacity, then drops quickly below 3.5 V. Alkaline cells fall more steadily. NiMH cells are nearly flat through most of their discharge, which makes voltage almost useless as a fuel gauge.
For this reason, treat a voltage reading as a threshold alarm more than a percentage display. A practical approach is to pick a "low" voltage for your chemistry, for example 3.5 V for a lithium-ion cell under light load, and trigger a warning or a shutdown when the averaged reading stays below it.
Common mistakes
- Using the nominal voltage instead of the maximum when sizing the divider.
- Swapping R1 and R2 in the formula, which gives a reading that moves in the wrong direction or a result several times too large.
- Powering the board from a regulator and keeping
VREF = 5.0even as the rail sags near dropout. - Forgetting a common ground. The divider ground, the board ground, and the battery negative terminal must be connected.
- Reading the voltage during a heavy load and treating the result as the battery's resting voltage.
Exercise
A 2S LiPo pack (8.4 V when full) is read by a 5 V board with R1 = 220 kΩ and R2 = 100 kΩ. What voltage appears at the pin when the pack is full, and what raw ADC value would you expect?
Show answer
The ratio is R2 / (R1 + R2) = 100k / 320k = 0.3125. The pin voltage is 8.4 × 0.3125 = 2.625 V. The raw value is 2.625 / 5 × 1023, which is about 537. The pin stays well below 5 V, so this is safe, though a 100k / 100k divider would use more of the ADC range and give better resolution.
Exercise
Your 9 V battery runs a 5 V regulator, and the sketch uses VREF = 5.0. The multimeter reads 8.00 V, but the sketch prints 8.60 V and keeps getting worse as the battery drains. What is the likely cause, and what are two fixes?
Show answer
The ADC reference is the board's 5 V rail, which is sagging as the battery approaches the regulator's dropout, so the real VREF is lower than the assumed 5.0 V. Fix it by using the internal reference with a divider scaled for about 1.1 V, or by using a regulator with lower dropout or a buck-boost converter so the rail stays at 5 V. A one-time calibration factor will not fix it, because the error changes as the battery drains.