Sub topic 13
Reading Battery Voltage with a Voltage Divider
Assume you know Ohm’s law, can build a breadboard circuit, and can upload an Arduino sketch. This chapter shows how to measure a battery safely with an analog input. The analog-to-digital converter (ADC) changes a voltage into a number that your sketch can read.
Why the battery cannot connect directly
An Arduino analog input has a maximum allowed voltage. On a typical 5 V Arduino board, the ADC range is 0 to about 5 V. A battery can exceed this voltage, especially when freshly charged. Connecting it directly can damage the input.
A voltage divider is a circuit made from two resistors. It produces a smaller voltage from a larger one.
For resistors named R1 and R2, the pin voltage is:
Vpin = Vbattery × R2 / (R1 + R2)
With two equal 100 kΩ resistors, the divider ratio is 2:1. A 5 V battery produces 2.5 V at A0.
| Battery voltage | R1 | R2 | A0 voltage |
|---|---|---|---|
| 5.0 V | 100 kΩ | 100 kΩ | 2.5 V |
| 4.2 V | 100 kΩ | 100 kΩ | 2.1 V |
Wiring and measurement
Connect the battery positive terminal to R1. Connect the other end of R1 to A0. Connect A0 to R2, then connect the other end of R2 to Arduino ground. Connect the battery negative terminal to the same ground.
The Arduino’s reference voltage, VREF, determines the ADC conversion. A 10-bit ADC returns values from 0 through 1023. The following sketch assumes a 5 V board with a 5 V reference.
// Battery+ -> R1 (100k) -> A0 -> R2 (100k) -> GND
const float R1 = 100000.0;
const float R2 = 100000.0;
const float VREF = 5.0;
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 code first converts the ADC reading to the voltage at A0. It then reverses the divider ratio to estimate the battery voltage.
Choosing resistor values
Higher resistance reduces divider current and preserves battery energy. However, very high resistance can make the ADC reading noisy or inaccurate because the input source becomes weak. Values between about 10 kΩ and 100 kΩ are common. Add a small capacitor from A0 to ground if the reading is noisy, and average several readings in software.
For a 3.3 V board, use a divider ratio that keeps the maximum battery voltage below 3.3 V. Also measure the actual reference voltage when accuracy matters. USB power and board tolerances can make VREF differ from its nominal value.
Exercise
With R1 = 30 kΩ, R2 = 10 kΩ, and a 4.2 V battery, what voltage reaches A0?
Show answer
Vpin = 4.2 × 10 / (30 + 10) = 1.05 V.