To read a battery with an ESP32, scale its highest possible voltage down with a resistor divider before it reaches an ADC pin. Calculate from the battery's maximum charging or operating voltage, not its nominal label, then confirm the result is inside the measurable range and absolute limits for your exact ESP32 and board. Convert the calibrated pin voltage back by multiplying by the divider ratio.
This guide shows the calculation, a worked example, resistor selection, measurement checks, and the limits of using voltage as a battery percentage. It does not assume that every ESP32 pin tolerates 3.3 V or that every Li-ion cell has the same maximum voltage.
How the divider works
Connect an upper resistor R1 from battery positive to the measurement node, and a lower resistor R2 from that node to battery negative. Connect the ESP32 ADC input to the node and connect the battery negative to board ground so both circuits share the same reference. With a high-impedance ADC input, the approximate node voltage is:
VADC = Vbattery × R2 ÷ (R1 + R2)
The inverse relationship is Vbattery = VADC × (R1 + R2) ÷ R2. This assumes the resistors are connected as described and the ADC input does not significantly load the node. A wiring error, missing common ground, or additional connected load invalidates the calculation.

Choose the ratio from the highest battery voltage
- Identify the battery chemistry and configuration. Use its datasheet or charger specification to find the highest voltage the pack can reach, including charging, tolerance, and any expected transient.
- Find the ADC measurable range for the exact chip, attenuation, and core. Leave margin below the top of that range and below the GPIO's absolute maximum. The measurable range and absolute maximum are different specifications.
- Choose R1 and R2 so the divider output at the worst-case battery voltage stays within that margin:
Vbattery_max × R2 ÷ (R1 + R2) < VADC_target_max. - Check resistor tolerance, divider current, resistor power, ADC source impedance, and settling time before settling on values.
Illustrative example only: for a single-cell lithium-ion battery whose specified charging maximum is 4.2 V, two equal 100 kΩ resistors give a ratio of 2:1. At 4.2 V, the ADC node is about 2.1 V, and the divider draws about 21 µA before considering leakage. These values are not a universal recommendation: cell maximum voltage, board ADC range, resistor tolerance, sleep current budget, and ADC acquisition behavior must be checked for the real design.
| Battery voltage at the divider | ADC node with equal resistors | What to verify |
|---|---|---|
| 3.0 V | 1.50 V | That voltage falls inside the chosen chip's ADC range and provides useful resolution |
| 3.7 V | 1.85 V | Battery configuration and whether 3.7 V is nominal rather than maximum |
| 4.2 V | 2.10 V | That 4.2 V is the correct maximum for the specific cell and charger |
For a multi-cell pack, design for the pack's maximum series voltage, not the voltage of one cell. A divider ratio that is safe for one cell may overdrive the ADC on a two- or three-cell pack. Add margin for resistor tolerance and accidental charger or regenerative conditions; do not size the divider to land exactly at the ADC range boundary.
Check the real divider before connecting it to the GPIO
Assemble the divider with power removed. Confirm the two resistors are in series between battery positive and negative, the midpoint is between them, and the board ground shares the battery-negative reference. Check that the midpoint wire goes to a documented ADC input, not a supply or output pin. Inspect polarity and loose strands before powering the board.

With the divider powered and the ESP32 ADC still disconnected, measure battery voltage and midpoint voltage using a multimeter in DC-voltage mode. Compare the midpoint with the calculated ratio. A large difference suggests a wrong resistor, wrong connection, loading, an unexpected ground, or a battery value different from the assumption. Disconnect power before using resistance or continuity mode.
Then connect the ADC pin and take a calibrated reading using the API supported by your installed core. Arduino-ESP32 provides analogReadMilliVolts() for a calibrated millivolt estimate where supported. Configure attenuation for the exact target and keep the pin in the documented measurable range. Reconstruct the battery voltage by multiplying the pin reading by (R1 + R2) / R2. A calibrated ADC result is still an estimate, not a substitute for a reference instrument.

Resistor values, current, and ADC settling
Lower resistor values draw more current continuously but provide a lower source impedance to the ADC. Higher values reduce battery drain but make the divider node more sensitive to leakage, contamination, ADC sampling behavior, and capacitive settling. The divider's approximate source resistance is the parallel combination R1 ∥ R2. If it is high, the ADC sample capacitor may not settle quickly enough for accurate back-to-back conversions. Follow the chip documentation, allow settling after enabling a switched divider, and compare results against a multimeter.
The current through the divider is approximately Vbattery ÷ (R1 + R2). Estimate total battery drain over the actual operating time, including how often the measurement is taken. A switched divider can reduce idle drain, but its transistor, off-state leakage, GPIO default state, and settling delay all need design attention. Do not disconnect only the ground side if that leaves the ADC input floating or creates an unintended current path through the GPIO.
A small capacitor from the ADC node to ground can reduce high-frequency noise, but together with the divider's source resistance it creates a time constant. That can smooth a reading while delaying response. Test it against the speed and accuracy your application needs; do not copy a capacitor value without checking the circuit.
Common mistakes
- Using the battery's nominal voltage instead of its maximum charging voltage.
- Assuming the ADC's measurable range is the same as the pin's absolute maximum rating.
- Using a web example's GPIO number without checking the board pinout and chip variant.
- Forgetting the common ground or connecting the divider midpoint to a non-ADC pin.
- Converting raw counts with a presumed 3.3 V reference without checking calibration and attenuation.
- Calling voltage-to-percentage linear for a chemistry whose discharge curve is nonlinear.
Voltage is not a universal state-of-charge gauge
A voltage reading can support battery monitoring, but percentage requires a chemistry-specific discharge curve and suitable conditions. Cell voltage changes with load, temperature, age, and time since charging. A robust product may need current integration, rest-period measurement, or a battery-monitor IC. Use voltage thresholds from the battery manufacturer and the system's safety requirements; do not infer remaining runtime from a single ADC sample.
For unstable ESP32 readings, first troubleshoot the ADC input, attenuation, wiring, and calibration in the separate ADC guide. For a pack above the ESP32 input range, redesign the divider rather than relying on software clipping or protection.
Frequently asked questions
Can I connect a 4.2 V cell directly to an ESP32 ADC?
Do not assume that is safe. The allowed input and measurable range depend on the exact chip and configuration. A divider designed with margin is the usual approach, followed by verification against the datasheet.
Does the battery negative need to connect to ESP32 GND?
For this simple divider measurement, the ADC needs the same reference as the divider. Connect grounds as the circuit design requires and ensure no separate supply creates a back-feed path.
Why does the ADC reading change when I connect the pin?
The ADC input loads the divider and samples through internal circuitry. A high source impedance, long wiring, or insufficient settling can change the result. Compare the unloaded midpoint to the connected reading and check the target's ADC guidance.
Can I calculate an accurate battery percentage from voltage alone?
Not universally. The relationship depends on chemistry, load, temperature, and cell condition; voltage alone is often a rough indicator.
Check resistor power and margin
Once the ratio is safe, check the dissipation in each resistor at the highest battery voltage. The divider current is approximately I = Vbattery / (R1 + R2); the upper resistor dissipates I² × R1 and the lower one I² × R2. Compare those estimates with the resistor ratings and the derating guidance for the enclosure temperature. The values may be small in a high-resistance divider, but the calculation also shows the continuous battery drain if the circuit stays connected.
Include resistor tolerance in the worst-case output calculation. For a conservative bound, combine the tolerance that raises R2 with the tolerance that lowers R1, then recalculate the maximum node voltage. Also allow margin for battery charging tolerance and any pack arrangement. If the result is near the input limit, choose a larger ratio or another suitable measurement interface; software clamping occurs after the electrical input and cannot protect an over-voltage pin.
After selecting values, measure both the divider input and its midpoint with the same battery condition. Compare the midpoint with the ratio calculation, then connect the ADC and compare again. A change only after the ADC is attached can indicate that the source impedance is too high for the sampling behavior or that the wiring is loading the node. If the voltage is correct but the reconstructed battery value is not, check whether the code uses the actual resistor ratio and calibrated pin millivolts rather than raw counts as volts.
Decide whether the divider should stay connected
A permanently connected divider is simple to understand and measure, but it draws current whenever the battery is present. Estimate that drain from the full pack voltage and resistor total, then compare it with the product’s standby budget over the intended storage period. If that drain is unacceptable, a switched divider can disconnect one side during sleep, but the switch adds leakage, voltage drop, control-state and settling questions.
For a switched arrangement, define what the switch does during reset, deep sleep, firmware upload, and a low battery condition. Allow the divider node to settle after enabling it before taking conversions, and check that the GPIO does not inadvertently feed current into the ADC while the divider is off. A MOSFET or load switch must be chosen for the topology and polarity; a generic diagram should not be copied without checking its body diode and control reference.
Finally, distinguish measuring a battery from charging it. The divider is only a sensing path. It does not regulate charge current, provide cell balancing, replace a protection circuit, or make a battery safe to connect to an ESP32 pin. Preserve the battery maker’s approved charger and protection, and keep charging current out of the ADC network.
Keep the ADC estimate within its useful range
A divider can keep the pin electrically safe and still produce a poor measurement if the expected battery range uses only a small part of the ADC range. Check both the lowest useful battery voltage and the maximum, then estimate the corresponding node voltages. If the low end falls near the ADC’s noisy floor, consider a different ratio or a measurement device designed for that span. Do not compromise the maximum-voltage margin to gain a few more codes at the low end.
When the battery is charging or powering a changing load, record the measurement condition. A cell’s terminal voltage may move with charge current and load, so compare readings taken at similar conditions. The divider calculation reconstructs terminal voltage; it does not identify the cell’s health or state of charge by itself.
Technical sources
- Espressif Arduino-ESP32 ADC API, calibrated millivolts and chip-specific ranges (consulted 2026-11-01).
- Espressif ESP-IDF ADC driver documentation (consulted 2026-11-01).
- Seeed Studio, XIAO ESP32C6: board-specific battery ADC example (consulted 2026-11-01; its resistor and pin values apply to that board only).
Be the first to leave a comment.