Battery and sleep

Light Sleep

Faster wake than deep sleep, retains more state, ideal for low-power applications requiring quick response.

Battery and sleep Light sleep ESP32 family

Official path: examples/system/light_sleep

01 Overview

Light Sleep is a low-power mode on ESP32 where the CPU is halted but RTC and some peripherals remain powered, and RAM contents are preserved. Wake-up is much faster than deep sleep, typically in microseconds to milliseconds.

Compared to deep sleep, light sleep has higher current (typically around 0.8 mA depending on peripherals) but retains more system state, avoiding re-initialization of most peripherals and reconnecting Wi-Fi.

Use when: you need frequent wake-ups, fast response, and moderate power savings, such as sensor polling, button wake-up, or BLE beacons.

Skip when: a coin cell must last months or years; deep sleep (current as low as 10 µA) is more appropriate.

During light sleep, Wi-Fi and Bluetooth are usually disabled, but can be kept on with extra power. After wake, the CPU resumes from the point it left off, without restarting app_main.

02 Hardware

  • MCU: ESP32 family, supports light sleep with wake sources like timer, GPIO, UART.
  • Power: Light sleep current is typically 0.8 mA (Wi-Fi/BT off), actual value depends on peripherals and frequency. Measure current on the real battery path; USB power lies due to regulators and LEDs.
  • Wake pins: GPIO wake supports edge trigger; ensure proper pull-up/down resistors to avoid false wake-ups from floating pins.
  • RTC peripherals: RTC domain stays powered, retaining RTC memory and RTC GPIO states.
  • External devices: If external sensors need power, design a power switch to cut them off during light sleep to save energy.
  • Layout: Follow ESP32 hardware design guidelines for crystal and RF sections to ensure clock stability in low-power modes.

03 Software flow

1. Configure wake sources: use `esp_sleep_enable_timer_wakeup()` for timer, or `esp_sleep_enable_gpio_wakeup()` for GPIO edge.

2. Optionally save critical data in RTC memory using `RTC_DATA_ATTR` or check wake cause with `esp_sleep_get_wakeup_cause()`.

3. Disable unused peripherals (e.g., Wi-Fi, Bluetooth) by calling `esp_wifi_stop()` etc.

4. Call `esp_light_sleep_start()` to enter light sleep.

5. After wake, CPU resumes from the breakpoint; check wake cause, handle events, then continue main loop or re-enter light sleep.

6. Note: light sleep does not restart app_main, but ensure peripheral states are correct after wake.

04 Core points

  • Pick sleep class from latency budget: If wake time >10 ms, deep sleep may be more power-efficient; if <1 ms, light sleep is the only choice.
  • Wake source constraints: Light sleep supports GPIO, timer, UART, but not all sources (e.g., touch sensor) may be available; check datasheet.
  • Peripheral power: Peripherals left on during light sleep still consume power; manage carefully.
  • Wi-Fi keep-alive: Keeping Wi-Fi connected significantly increases power consumption; weigh the trade-off.
  • Custom work: Sleep state machine and peripheral retention strategy need tuning per application.

中文

电池与休眠

Light Sleep 浅睡

比深睡更快醒来,保留更多状态,适合需要快速响应的低功耗场景。

电池与休眠 浅睡 ESP32 family

官方路径: examples/system/light_sleep

01 项目概述

浅睡(Light Sleep)是 ESP32 的一种低功耗模式,CPU 暂停,但 RTC 和部分外设保持供电,RAM 内容不丢失。唤醒速度远快于深睡,通常在微秒到毫秒级。

相比深睡,浅睡电流更高(典型 0.8 mA 左右,视外设而定),但保留了更多系统状态,无需重新初始化大部分外设和重新连接 Wi-Fi。

适用场景:需要频繁唤醒、快速响应,且功耗要求不极端的 IoT 设备,如传感器轮询、按键唤醒、低功耗蓝牙信标等。

不适用场景:需要数月甚至数年电池寿命的纽扣电池节点,此时深睡(电流可低至 10 µA 以下)更合适。

浅睡期间,Wi-Fi 和蓝牙默认关闭,但可通过配置保持连接(需额外功耗)。唤醒后,CPU 从断点继续执行,无需重启 app_main。

02 项目硬件描述

  • MCU: ESP32 系列,支持浅睡模式,唤醒源包括定时器、GPIO、UART 等。
  • 电源: 浅睡电流典型 0.8 mA(关闭 Wi-Fi/蓝牙),实际值取决于外设和频率。测量电流必须使用真实电池路径,USB 供电会因稳压器和 LED 产生误导。
  • 唤醒引脚: GPIO 唤醒支持边沿触发,需注意外部上拉/下拉电阻,避免浮空导致误唤醒。
  • RTC 外设: RTC 域保持供电,可保留 RTC 内存和 RTC GPIO 状态。
  • 外部器件: 若外部传感器等需要供电,需设计电源开关,浅睡时关闭以节省功耗。
  • 布局: 晶振和射频部分遵循 ESP32 硬件设计指南,确保低功耗模式下时钟稳定。

03 项目软件流程描述

1. 配置唤醒源:使用 `esp_sleep_enable_timer_wakeup()` 设置定时器,或 `esp_sleep_enable_gpio_wakeup()` 设置 GPIO 边沿。

2. 可选:在 RTC 内存中保存关键数据,使用 `RTC_DATA_ATTR` 或 `esp_sleep_get_wakeup_cause()` 判断唤醒原因。

3. 关闭不需要的外设(如 Wi-Fi、蓝牙),调用 `esp_wifi_stop()` 等。

4. 调用 `esp_light_sleep_start()` 进入浅睡。

5. 唤醒后,CPU 从断点继续,检查唤醒原因,处理事件,然后继续主循环或再次进入浅睡。

6. 注意:浅睡不会执行 `app_main` 重启,但需确保唤醒后外设状态正确。

04 项目核心点

  • 先定延迟预算: 如果唤醒时间要求 >10 ms,深睡可能更省电;如果 <1 ms,浅睡是唯一选择。
  • 唤醒源限制: 浅睡支持 GPIO、定时器、UART 等,但触摸传感器等可能不支持,需查阅数据手册。
  • 外设功耗: 浅睡期间,未关闭的外设仍耗电,需仔细管理。
  • Wi-Fi 保持: 若需保持 Wi-Fi 连接,功耗会显著增加,需权衡。
  • 可定制: 睡眠状态机、外设保持策略需根据具体应用调整。