ESP32 Deep Sleep Mastery: Cutting Power Consumption from 240mA to 10µA
The ESP32 is a powerful chip — which means it consumes significant power when running at full tilt. WiFi transmission peaks at 240 mA. But deep sleep brings this to 10 µA, a 24,000× reduction. For battery-powered devices, the difference between getting this right and getting it wrong is the difference between a one-year battery life and a three-day battery life.
This guide covers every ESP32 sleep mode, all wake-up sources, RTC memory usage, and the ULP coprocessor for advanced low-power sensing.
ESP32 Power Mode Overview
| Mode | CPU | WiFi/BT | RTC Memory | Current | |---|---|---|---|---| | Active (WiFi Tx) | Running | On | Retained | 160–240 mA | | Modem Sleep | Running | Off | Retained | 3–20 mA | | Light Sleep | Paused | Off | Retained | 0.8 mA | | Deep Sleep | Off | Off | Retained | 10–150 µA | | Deep Sleep + ULP | Off | Off | Retained | 150 µA | | Hibernation | Off | Off | Lost | 2.5 µA |
For most IoT devices, deep sleep is the target mode between active periods.
Modem Sleep: WiFi Off, CPU Running
Modem sleep disables the WiFi/BT radio while keeping the CPU running. The RTOS continues running, timers fire, peripherals work. Use this when you need the CPU active (processing sensor data continuously) but do not need WiFi every second.
#include "esp_wifi.h"// Enable automatic modem sleep — WiFi turns off between DTIM beacons
esp_wifi_set_ps(WIFI_PS_MAX_MODEM);
// Or disable modem sleep for lowest latency (highest power)
esp_wifi_set_ps(WIFI_PS_NONE);
Current drops from 160 mA to 3–20 mA depending on DTIM interval. Not sufficient for multi-year battery operation.
Light Sleep: CPU Paused, State Retained
Light sleep halts the CPU clocks and most peripherals. FreeRTOS tasks are paused. The system wakes on timer, GPIO, UART, or other sources, then resumes exactly where it left off — stack intact, task states intact. Wake latency is ~5 ms.
// Configure automatic light sleep in FreeRTOS idle hook
// Enable in sdkconfig: CONFIG_PM_ENABLE=y, CONFIG_FREERTOS_USE_TICKLESS_IDLE=y// The tickless idle automatically enters light sleep when all tasks are blocked
// No code changes required — just enable in sdkconfig
Current: ~0.8 mA. Better, but still 80× higher than deep sleep for long idle periods.
Deep Sleep: The Primary Battery-Saver
In deep sleep, the main CPU cores and most RAM are powered off. Only the RTC domain (RTC memory, RTC peripherals, RTC controller) remains powered. Wake causes a full reboot — app_main runs from the beginning.
#include "esp_sleep.h"#define SLEEP_DURATION_SEC 60
void go_to_deep_sleep(void) {
ESP_LOGI("SLEEP", "Entering deep sleep for %d seconds", SLEEP_DURATION_SEC);
// Configure wakeup source: timer
esp_sleep_enable_timer_wakeup((uint64_t)SLEEP_DURATION_SEC * 1000000ULL);
// Optional: enable GPIO wakeup (e.g., button press)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_0, 0); // Wake on GPIO0 LOW
// Enter deep sleep — does not return
esp_deep_sleep_start();
}
void app_main(void) {
// Determine wake reason
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_TIMER:
ESP_LOGI("BOOT", "Woke from timer");
break;
case ESP_SLEEP_WAKEUP_EXT0:
ESP_LOGI("BOOT", "Woke from GPIO button press");
break;
default:
ESP_LOGI("BOOT", "Power-on reset or unknown wakeup");
}
// ... do work, then sleep again
go_to_deep_sleep();
}
RTC Memory: Preserving State Across Sleep
Since app_main runs fresh on every wake, any state you need to preserve across sleep cycles must live in RTC memory. RTC SLOW memory (8 KB) and RTC FAST memory (8 KB) both survive deep sleep.
// RTC_DATA_ATTR places variable in RTC slow memory
RTC_DATA_ATTR uint32_t boot_count = 0;
RTC_DATA_ATTR float last_temperature = 0.0f;
RTC_DATA_ATTR uint8_t pending_batch[120] = {0}; // 30 readings × 4 bytes
RTC_DATA_ATTR uint8_t batch_index = 0;void app_main(void) {
boot_count++; // Persists across sleep cycles
float temperature = read_temperature_sensor();
pending_batch[batch_index++] = (uint8_t)(temperature * 2); // Compressed
if (batch_index >= 30) {
// Upload all 30 readings, then reset
upload_batch_to_cloud(pending_batch, batch_index);
batch_index = 0;
}
esp_sleep_enable_timer_wakeup(10ULL * 60 * 1000000); // 10 min
esp_deep_sleep_start();
}
RTC memory limitation: 8 KB total. Store only what you need — sensor readings, counters, state flags, pending upload data. Do not try to fit large buffers or strings.
Wake Stubs: Code That Runs Before Full Boot
Deep sleep wake stubs are functions stored in RTC fast memory that execute immediately on wakeup — before the full ESP-IDF boot process runs. Use them to make ultra-fast wake/decision/sleep decisions without the ~300 ms boot overhead.
#include "esp_attr.h"RTC_DATA_ATTR uint32_t stub_counter = 0;
RTC_DATA_ATTR bool do_full_boot = false;
// Stored in RTC fast memory — runs immediately on wakeup
void RTC_IRAM_ATTR esp_wake_deep_sleep(void) {
esp_default_wake_deep_sleep(); // Required: clears wakeup flags
stub_counter++;
if (stub_counter >= 12) {
// Full boot every 12 wakeups (2 hours if 10-min interval)
do_full_boot = true;
stub_counter = 0;
return; // Proceed to full app_main
}
// Otherwise, go back to sleep immediately — no full boot
esp_sleep_enable_timer_wakeup(10ULL * 60 * 1000000);
esp_deep_sleep_start();
}
The wake stub can reduce effective wake time from 300 ms to under 1 ms for cycles where you do not need full connectivity.
ULP Coprocessor: Sensing During Sleep
The ULP (Ultra-Low Power) coprocessor is an 8 MHz RISC processor that runs while the main CPU is in deep sleep, consuming only ~150 µA total. It can read GPIOs, communicate via I2C/SPI via bit-banging, and wake the main CPU only when a threshold is crossed.
// ULP program in ULP assembly (simplified concept)
// Reads ADC, stores result in RTC memory, wakes main CPU if threshold exceeded#include "ulp_main.h"
RTC_DATA_ATTR uint32_t ulp_adc_result = 0;
RTC_DATA_ATTR uint32_t ulp_threshold = 2048; // ~1.65V on 3.3V ref
void init_ulp_program(void) {
// Load and start ULP binary (compiled from ULP assembly)
ulp_load_binary(0, ulp_main_bin_start,
(ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
// Set ULP wakeup period: sample every 1 second
ulp_set_wakeup_period(0, 1000000); // 1 second in microseconds
// Start ULP — main CPU can now deep sleep
ulp_run(&ulp_entry - RTC_SLOW_MEM);
esp_sleep_enable_ulp_wakeup();
esp_deep_sleep_start();
}
The ULP enables truly event-driven wake — the main CPU only powers on when something interesting happens (a threshold crossed, a motion detected), not on a fixed timer.
Real Current Measurements
Measured on an ESP32-WROOM-32E with a Nordic PPK2 power profiler:
For the battery life calculation and a complete worked example showing how these numbers translate to multi-year operation, see our guide on [IoT battery life optimization](/iot-battery-power-optimization).
[Contact Code Caracal](/contact) — we build production firmware for clients across 15+ countries.