The ESP32 is power-hungry when active — WiFi alone draws 80–240mA. But with proper deep sleep management, the same chip can sip current at 10 microamps, making battery-powered projects that last months or even years entirely realistic.
Understanding Sleep Modes
| Mode | CPU | WiFi/BT | RAM | Current |
|---|---|---|---|---|
| Active | ON | ON | ON | 80–240mA |
| Modem Sleep | ON | OFF | ON | 20–30mA |
| Light Sleep | Paused | OFF | ON | ~0.8mA |
| Deep Sleep | OFF | OFF | RTC only | ~10µA |
| Hibernation | OFF | OFF | OFF | ~2.5µA |
Deep sleep is the sweet spot for most battery-powered IoT sensors.
Basic Deep Sleep Code
Wakeup Sources
Timer wakeup — Wake after N seconds. Most common for sensors.
esp_sleep_enable_timer_wakeup(30 * 1000000ULL); // 30 seconds
External wakeup (EXT0) — Wake when a single GPIO goes HIGH or LOW. Use for PIR sensors, button presses.
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); // Wake on GPIO33 = HIGH
Touch wakeup — Wake when a touch pin is touched. Useful for wearables.
touchSleepWakeUpEnable(T3, 40); // Wake when touch pin T3 drops below threshold 40
Battery Life Calculation
For a weather sensor that wakes every 5 minutes, connects WiFi, reads DHT22, sends MQTT, and sleeps:
- Active time: ~3 seconds at ~120mA average = 0.1mAh per cycle
- Sleep time: 297 seconds at 0.01mA = 0.00083mAh per cycle
- Per cycle total: ~0.1mAh
- Cycles per day: 288
- Daily consumption: ~28.8mAh
- 2000mAh battery: ~69 days
Optimize the active period and you can push past 100 days easily.
Common Mistakes
Using delay() instead of deep sleep: delay(30000) keeps the CPU and RAM active for 30 seconds at full power. esp_deep_sleep_start() drops to 10µA for the same period.
Not holding GPIO state during sleep: External peripherals (sensors, displays) may reset or behave unexpectedly when the ESP32 wakes. Use RTC GPIO pins for peripherals that need stable power.
Long WiFi reconnection: Each wake-sleep cycle reconnects WiFi from scratch (~1–2 seconds). Consider storing WiFi channel and BSSID in RTC memory to reduce reconnection time to under 200ms.
