← All notes

ESP32 · MicroPython

Deep sleep and battery monitoring on the ESP32

Updated 20 Sep 20266 min readMicroPython 1.2x · Thonny

The fastest way to flatten a battery is an ESP32 that never sleeps. With the radio on, an ESP32 draws somewhere around 80–240 mA. In deep sleep the chip itself can drop to around 10 µA. Your battery life is decided by how much time you spend in each state, and that's what this note covers.

Measure the battery through a divider

A Li-ion cell tops out at 4.2 V, which is too high for the ADC pin. Two equal resistors halve it. Use high values (100 kΩ each) so the divider itself doesn't drain the cell. That's about 21 µA at 4.2 V, which you can check in the voltage divider calculator.

from machine import Pin, ADC, deepsleep
import time

adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)        # full range, roughly 0–3.1 V

def battery_volts(samples=16):
    total = 0
    for _ in range(samples):
        total += adc.read_uv()   # calibrated microvolts
        time.sleep_ms(2)
    return total / samples / 1_000_000 * 2   # x2 for the 1:1 divider

v = battery_volts()
print("Battery:", round(v, 2), "V")

# ... read sensors, send the reading ...

deepsleep(15 * 60 * 1000)       # sleep 15 minutes, then reboot into main.py
Thonny tip: save this as main.py on the board. When the board is in deep sleep, Thonny can't talk to it. Press the reset button or Ctrl+C right after it wakes to get the REPL back.

The dev board is usually the problem

Most ESP32 dev boards have a USB-serial chip, a power LED and a linear regulator that keep drawing current while the ESP32 sleeps. That can easily add 1–10 mA, which is 100× what the chip itself uses. For a battery node, use a board designed for low quiescent current, or remove the LED and power it from a low-Iq regulator.