Most microcontrollers can only measure voltages up to their own supply, usually 3.3 V. But the batteries in robots, solar setups and cars sit at 7 V, 12 V or more. Connect one straight to a pin and you'll destroy it. The fix is two resistors arranged as a voltage divider, which scales the battery voltage down to a safe level you can measure.
The idea
The voltage at the pin is a fixed fraction of the battery voltage:
V_pin = V_battery × R2 / (R1 + R2)
Your code measures V_pin, then works backwards to get the battery voltage.
Step 1: decide the highest voltage you'll ever see
Plan for the worst case, not the label on the battery. A "12 V" lead acid battery rests at about 12.7 to 12.8 V when full, but while it is charging it typically sits at 14 to 15 V, and some chargers go higher still. A 3-cell lithium pack is 12.6 V when full. Designing for a maximum of 16 V covers all of these with room to spare.
Step 2: choose the resistors
We need 16 V at the battery to give less than 3.3 V at the pin, so the ratio R2 / (R1 + R2) must be below 3.3 / 16 = 0.21.
R1 = 100 kΩ and R2 = 22 kΩ give 22 / 122 = 0.18. At 16 V the pin sees 2.89 V, safely under the limit, and it would take 18 V to reach 3.25 V. You can check other combinations with the voltage divider calculator.
Why such large values? The divider is connected all the time, so it slowly drains the battery. At 14.4 V, these resistors draw only 14.4 / 122,000 = 0.12 mA. Much smaller resistors would waste more, and much larger ones make the reading noisier.
Step 3: wire it up
- Battery positive to one end of R1.
- The other end of R1 to R2, and that junction to the ADC pin. On a Raspberry Pi Pico, use GP26 (ADC0).
- The other end of R2 to ground.
- Battery negative to the Pico's GND. Without a shared ground the reading is meaningless.
Double check before you connect the battery. Measure the junction with a multimeter first and make sure it reads below 3.3 V.
Step 4: the code
This MicroPython program for the Raspberry Pi Pico reads the pin once a second and prints the battery voltage:
from machine import ADC, Pin
import time
adc = ADC(Pin(26)) # GP26 is ADC0 on the Pico
R1 = 100_000 # top resistor, ohms
R2 = 22_000 # bottom resistor, ohms
VREF = 3.3 # the ADC's full scale voltage
SCALE = (R1 + R2) / R2 # undo the divider
def battery_voltage(samples=16):
total = 0
for _ in range(samples): # average a few readings to cut noise
total += adc.read_u16()
raw = total / samples
return raw * VREF / 65535 * SCALE
while True:
print("Battery: {:.2f} V".format(battery_voltage()))
time.sleep(1)
read_u16() returns a number from 0 to 65,535 for 0 V to 3.3 V. Multiplying by VREF / 65535 turns it back into the pin voltage, and SCALE undoes the divider. For example, a raw reading of 41,000 means 2.06 V at the pin, which works out to 11.45 V at the battery.
Step 5: calibrate
Resistors are only accurate to 1% or 5%, and the Pico's reference voltage isn't exactly 3.3 V, so your first readings may be a few percent out. Measure the battery with a multimeter, compare it with what the Pico prints, and adjust:
SCALE = SCALE * (multimeter_reading / pico_reading)
For example, if the multimeter says 12.60 V and the Pico says 12.35 V, multiply SCALE by 12.60 / 12.35. Once calibrated, readings are usually good to a few hundredths of a volt.
Common problems
- Reading jumps around: add the 100 nF capacitor and average more samples.
- Always reads about 0 V or full scale: check the shared ground and that you used an ADC capable pin.
- Arduino Uno instead of a Pico? The same idea works. The Uno's ADC runs from 0 to 5 V and returns 0 to 1,023, so use
raw * 5.0 / 1023for the pin voltage.
Going deeper
Source impedance and the ADC
A microcontroller's ADC briefly charges a small internal sampling capacitor each time it takes a reading. If the divider's resistors are large, that capacitor can't charge fully in the sampling time, and readings come out low or depend on the previous channel. The divider looks to the ADC like a single resistor equal to R1 and R2 in parallel: with 100 kΩ and 22 kΩ that's about 18 kΩ.
The usual fix is a capacitor, around 100 nF, from the ADC pin to ground. It acts as a local reservoir for the sampling capacitor. It also filters noise, at the cost of a slower response (here about 18 kΩ × 100 nF ≈ 1.8 ms time constant), which is fine for a battery that changes slowly.
The divider drains the battery
The divider always draws current: 16 V across 122 kΩ is about 130 µA, or roughly 3 mAh a day. That's trivial for a car battery but significant for a small battery left for months. Options:
- Use larger resistors (with the capacitor above to keep readings accurate).
- Switch the divider on only when measuring, using a MOSFET on the high side. A simple low-side switch doesn't work well here because the ADC pin then sees the full battery voltage through R1 when it's off.
Don't back-power the microcontroller
If the battery stays connected while the board is switched off, current flows from the battery through R1 and into the ADC pin's protection diode, then into the unpowered 3.3 V rail. With R1 = 100 kΩ that's limited to roughly 0.1 mA, which is usually harmless. With small resistors it can partly power the chip, or exceed the pin's injection-current limit in the datasheet. Keep R1 large, or switch the divider off along with the board.
Averaging and resolution
A 12-bit ADC has 4,096 steps, but noise means the last bit or two jump around. Averaging N readings reduces random noise by roughly √N, so 16 readings cut it by about 4 times:
def average_reading(read, n=16):
return sum(read() for _ in range(n)) / n
With MicroPython you'd pass adc.read_u16 as read. Averaging can't fix errors that are the same every time, such as resistor tolerance or reference error. That's what calibration is for.
The reference voltage matters
ADC readings are a fraction of the reference voltage. On boards where the reference is the 3.3 V supply, a supply that's really 3.25 V makes every reading about 1.5% off, and noise on the supply appears in your measurements. Calibrating against a multimeter corrects the steady part. For better accuracy, use a board or external ADC with a precision voltage reference.
Reading state of charge
Battery voltage only loosely indicates charge, and it sags under load and recovers at rest. Lead-acid batteries should rest for several hours with no load or charging before their voltage reflects their charge. Lithium-ion cells have a flat curve through the middle of their range, so small voltage errors mean large charge errors. Serious battery monitors measure current over time ("coulomb counting") as well as voltage.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.