An ESP32 connected to WiFi draws tens to over a hundred mA on average, with transmit peaks above 300mA. That's not sustainable on a battery. The solution: sleep deeply most of the time, wake up only to measure and send, then go back to sleep.
Basic code
RTC_DATA_ATTR int bootCount = 0; // survives deep sleep
void setup() {
Serial.begin(115200);
bootCount++;
Serial.printf("Wake-up number %d\n", bootCount);
// ... read sensors, send data ...
esp_sleep_enable_timer_wakeup(10ULL * 60 * 1000000ULL); // 10 minutes (µs)
esp_deep_sleep_start();
}
void loop() {}
After deep sleep the chip restarts from setup(). Normal variables are lost; only RTC_DATA_ATTR variables are kept.
Waking up with a button (ext0/ext1)
// Wake when GPIO33 goes LOW (RTC GPIO pins only)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0);
Only RTC pins can wake the chip (for example GPIO 0, 2, 4, 12–15, 25–27, 32–39 on the original ESP32). You can combine timer and button wake-up.
Real current draw depends on the board
The ESP32 chip needs only around ten µA in deep sleep, but on a DevKit the total is usually several mA because of:
- The AMS1117 regulator, which has a high quiescent current.
- The power LED that's always on.
- The USB-UART chip (CP2102/CH340) that stays powered.
To really save power, use a board designed for batteries (low-quiescent LDO, no LED), or build your own circuit around a bare ESP32 module and a low-quiescent LDO. Measure the current with a multimeter or a dedicated tool before trusting any estimate.
Tips to stay awake less
- Use a static IP instead of DHCP, and store the WiFi channel and BSSID in RTC memory for a quick connect.
- Batch readings and send once, instead of sending on every wake-up.
- Use a sensor with a one-shot (forced) mode that sleeps by itself, or power the sensor from a GPIO so it's fully off during sleep.
- Turn WiFi off before sleeping, and keep
delay()calls as short as possible. - Prefer plain MQTT or HTTP; avoid heavy HTTPS if you don't need it.
Quick battery life estimate
Average current = (awake current × awake time + sleep current × sleep time) / cycle. Example: awake 5 s at 120mA every 10 minutes, sleeping at 20µA, gives roughly 1mA average. A 2500mAh 18650 cell then lasts about 100 days in practice, before self-discharge and temperature losses. This is only an estimate, so measure the real device.
See a real application: ESP32 + BME280 weather station over MQTT.