Introduction
The ESP32 draws ~240mA at full speed — draining a battery in hours. Deep Sleep drops this to as low as 10 microamperes, extending battery life from hours to weeks. This tutorial covers every deep sleep technique.
Power Modes Comparison
| Mode | Current | CPU |
|---|---|---|
| Active | ~240mA | ON |
| Light Sleep | ~0.8mA | Paused |
| Deep Sleep | ~10µA | OFF |
Project 1: Timer Wake-Up (Most Common)
#define SLEEP_SECS 30
RTC_DATA_ATTR int bootCount = 0; // Survives deep sleep!
void setup() {
Serial.begin(115200);
bootCount++;
Serial.println("Boot #" + String(bootCount));
// Do your work here: read sensor, send data...
esp_sleep_enable_timer_wakeup(SLEEP_SECS * 1000000ULL);
esp_deep_sleep_start();
}
void loop() {}
Project 2: Wake on Button Press
#define BUTTON GPIO_NUM_33
void setup() {
Serial.begin(115200);
esp_sleep_enable_ext0_wakeup(BUTTON, 1);
auto cause = esp_sleep_get_wakeup_cause();
if (cause == ESP_SLEEP_WAKEUP_EXT0) Serial.println("Woke from button!");
else Serial.println("Normal boot");
delay(2000);
esp_deep_sleep_start();
}
void loop() {}
RTC Memory Explained
Variables declared with RTC_DATA_ATTR are stored in RTC memory (8KB) and survive sleep cycles. Use this for counters, timestamps, or flags between wake cycles.
Real Battery Life Example
- Active always: 3000mAh / 240mA = ~12.5 hours
- Sleep 29s, active 1s every 30s: avg ~8mA = ~375 hours (15 days)!
Conclusion
Deep sleep is the single most impactful optimization for battery-powered IoT devices. A simple 30-second sleep cycle can extend battery life from hours to weeks.