Implementing microcontroller sleep mode for ESP32 and STM32 power reduction is critical for any battery-powered IoT device. India’s diverse deployment environments — from remote agricultural sensors to urban smart meters — demand months-long battery life. This guide covers all major sleep modes with practical code examples.
Table of Contents
- Why Sleep Modes Matter for IoT
- ESP32 Sleep Modes
- ESP32 Deep Sleep with Wake Sources
- STM32 Sleep, Stop, and Standby Modes
- Power Comparison Table
- Calculating Battery Life
- Frequently Asked Questions
Why Sleep Modes Matter for IoT
A sensor node sending data every 10 minutes runs active for perhaps 0.5 seconds, then should sleep for 9 minutes 59.5 seconds. If active current is 100 mA and sleep current is 10 µA on ESP32, average current drops to approximately 0.1 mA — extending a 1000 mAh battery from 10 hours to 416 days. Getting ESP32 and STM32 microcontroller sleep mode right is the single biggest factor in battery-powered IoT longevity.
ESP32 Sleep Modes
ESP32 has several power states:
- Modem Sleep: CPU active, WiFi/BT radio off. ~20 mA. Good for active processing without communication.
- Light Sleep: CPU paused, RAM retained, wakes quickly (~2 ms). ~0.8 mA. Ideal for tasks needing fast wake.
- Deep Sleep: Only RTC and ULP coprocessor active. RAM cleared (except RTC_DATA). ~10–20 µA. Best for long-interval sensor logging.
- Hibernation: Minimal RTC only. ~2.5 µA. Longest battery life, wakes on external pin only.
ESP32 Deep Sleep with Wake Sources
#include "esp_sleep.h"
#include "driver/rtc_io.h"
// RTC_DATA_ATTR persists across deep sleep
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR float lastTemp = 0.0;
void setup() {
Serial.begin(115200);
bootCount++;
// Read sensor
float temp = readTemperature(); // Your sensor function
lastTemp = temp;
// Upload if significant change
if (abs(temp - lastTemp) > 0.5) {
connectWiFiAndUpload(temp);
}
// Timer wake-up: wake after 10 minutes
esp_sleep_enable_timer_wakeup(10 * 60 * 1000000ULL); // microseconds
// GPIO wake-up: wake on button press (GPIO 33)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); // 0 = LOW trigger
Serial.println("Entering deep sleep...");
Serial.flush();
esp_deep_sleep_start();
}
void loop() {} // Never reached in deep sleep design
STM32 Sleep, Stop, and Standby Modes
STM32 offers three main low-power modes:
- Sleep Mode: Core clock stopped, peripherals running. ~1–3 mA on STM32F4. Wake by any interrupt.
- Stop Mode: All clocks stopped, HSI/HSE off, SRAM/register content retained. ~2–100 µA depending on settings. Wake by EXTI or RTC alarm.
- Standby Mode: Almost everything off, only WKUP pin/RTC active. ~2–5 µA. SRAM content lost.
/* STM32 Stop Mode 2 entry (STM32L4 series, lowest power) */
#include "stm32l4xx_hal.h"
void enterStopMode(uint32_t seconds) {
// Configure RTC wakeup
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, seconds, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
// Enter Stop Mode 2 (0.4 µA on STM32L4)
HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
// Wake here - restore clocks
SystemClock_Config();
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
}
Power Comparison Table
| Mode | ESP32 | STM32F4 | STM32L4 |
|---|---|---|---|
| Active (WiFi TX) | 160–260 mA | 100 mA | 40 mA |
| Active (CPU only) | 50–80 mA | 100 mA | 10 mA |
| Light Sleep | 0.8 mA | 1–2 mA | 0.5 mA |
| Deep Sleep / Stop 2 | 10–20 µA | ~100 µA | 0.4 µA |
| Standby / Shutdown | 2.5 µA | 2 µA | 0.03 µA |
Calculating Battery Life
Formula: Battery Life (hours) = Battery Capacity (mAh) / Average Current (mA)
Example: ESP32 wakes every 15 minutes, active 2 seconds (50 mA average), then deep sleeps (20 µA):
- Active duty: 2s / 900s = 0.22%
- Average current: (0.0022 × 50) + (0.9978 × 0.02) = 0.11 + 0.02 = 0.13 mA
- With 3000 mAh battery: 3000 / 0.13 = 23,077 hours ≈ 963 days ≈ 2.6 years
Frequently Asked Questions
Does deep sleep on ESP32 lose WiFi connection?
Yes. Deep sleep powers off the WiFi radio. The ESP32 reconnects to WiFi on waking. Use esp_wifi_set_ps() for modem sleep to maintain connection at the cost of higher current.
Can I retain variable values across ESP32 deep sleep?
Yes. Declare variables with RTC_DATA_ATTR — they are stored in RTC slow memory (8 KB) which is powered during deep sleep.
Which STM32 series is best for battery-powered IoT in India?
STM32L4 series (ultra-low power) — designed specifically for battery-powered IoT with 0.4 µA Stop 2 current and smart peripherals that work without waking the CPU.
Add comment