Battery and sleep

Deep Sleep

How a battery node sleeps and wakes. Wake pins must not float.

Battery and sleep Deep sleep ESP32 family

Official path: examples/system/deep_sleep

01 Overview

Deep sleep is the core power-saving mechanism for battery-powered ESP32 devices: the CPU stops, most digital peripherals power down, and only the RTC domain and a few wake-up logic circuits remain active.

It solves the standby power problem for duty-cycled products—sensors wake up every minute or hour, take a reading, report, and sleep again, bringing average current down to microamps.

Typical use cases include temperature/humidity sensors, door/window contacts, environmental monitoring nodes, and low-power trackers. They don't need real-time response, only to be woken at specific moments.

Deep sleep is not standby: ordinary RAM contents are lost, and the program restarts from the beginning. Therefore, critical state must be kept in RTC memory or NVS.

It is also not suitable for devices that need to stay online or respond with low latency, such as remote controls or real-time control panels.

This example demonstrates both timer wake-up and GPIO wake-up, and provides a code framework for distinguishing cold boot from wake-up boot.

02 Hardware

  • Timer wake: No extra button needed; only the RTC timer is required.
  • EXT0 wake: The official demo uses GPIO25 on ESP32 and GPIO3 on ESP32-S2/S3; configure as input.
  • EXT1 wake: The demo uses GPIO2 and GPIO4; they must be held at a defined level (high or low) before sleep to avoid spurious wake-ups.
  • Wake pins: Any GPIO used for wake-up must not float; connect a pull-up or pull-down resistor, or drive it to a defined level by external circuitry.
  • Debug interface: USB-Serial-JTAG drops during deep sleep; use an external USB-UART connected to UART0 pins for debugging.
  • Power design: For battery operation, watch the quiescent current of external components (sensors, regulators) to avoid leakage.
  • RTC domain: Some GPIOs (e.g., RTC GPIOs) can retain state during deep sleep, useful for wake-up or holding external circuits.

03 Software flow

1. Call `esp_sleep_get_wakeup_cause()` to determine if it's a cold boot (ESP_SLEEP_WAKEUP_UNDEFINED) or a deep sleep wake.

2. Log the wake-up source (timer, EXT0, EXT1) and sleep duration (via `esp_sleep_get_wakeup_time()`).

3. Configure the next wake-up: call `esp_sleep_enable_timer_wakeup()` or `esp_sleep_enable_ext0_wakeup()` / `esp_sleep_enable_ext1_wakeup()`.

4. Disable unused wake-up sources to avoid unexpected wakes.

5. Store data that must survive (e.g., counters, state flags) in RTC memory (`RTC_DATA_ATTR`) or NVS.

6. Call `esp_deep_sleep_start()` to enter deep sleep.

7. After wake-up, the program restarts from `app_main()`, reading RTC memory to restore state.

04 Core points

  • Deep sleep is not standby — RAM is gone. All normal RAM variables are lost; use RTC memory or NVS for critical data.
  • Floating pins drain batteries. Any wake-up pin must have a pull-up/pull-down resistor or be driven to a defined level.
  • Custom work: wake map (which GPIO combinations trigger), sleep current acceptance (measure actual current with a multimeter), report period (timer duration).
  • Mind chip differences: EXT0 pins differ between ESP32 and ESP32-S2/S3; refer to the official demo.
  • Debugging note: USB drops after deep sleep; when using external UART, ensure your serial tool handles reconnection.

中文

电池与休眠

Deep Sleep 深度睡眠

电池节点怎么睡、怎么醒。唤醒脚不能浮空。

电池与休眠 深度睡眠 ESP32 family

官方路径: examples/system/deep_sleep

01 项目概述

深度睡眠是 ESP32 在电池供电场景下的核心功耗手段:CPU 停止、大部分数字外设断电,仅保留 RTC 域和少量唤醒逻辑。

它解决的是“间歇上报”类产品的待机功耗问题——传感器每分钟或每小时醒来一次,采集数据、上报、再睡,平均电流可以压到微安级。

典型场景包括温湿度计、门磁、环境监测节点、低功耗追踪器等。它们不需要实时响应,只需要在特定时刻被唤醒。

深度睡眠不是待机:普通 RAM 内容会丢失,程序从头开始执行。因此必须把关键状态放在 RTC 内存或 NVS 中。

它也不适用于需要持续在线或低延迟响应的设备,比如遥控器、实时控制面板。

本示例演示了定时唤醒和 GPIO 唤醒两种方式,并给出了如何区分冷启动与唤醒启动的代码框架。

02 项目硬件描述

  • 定时唤醒: 无需额外按键,仅需 RTC 定时器。
  • EXT0 唤醒: 官方示例在 ESP32 上使用 GPIO25,在 ESP32-S2/S3 上使用 GPIO3,需配置为输入模式。
  • EXT1 唤醒: 示例使用 GPIO2 和 GPIO4,休眠前必须确保它们有确定的电平(高或低),否则可能误唤醒。
  • 唤醒引脚: 任何用作唤醒的 GPIO 都不能浮空,必须接上拉或下拉电阻,或由外部电路驱动到确定电平。
  • 调试接口: 深睡时 USB-Serial-JTAG 会断开,调试请使用外部 USB-UART 连接 UART0 引脚。
  • 电源设计: 若使用电池供电,注意外部器件(如传感器、稳压器)的静态电流,避免漏电。
  • RTC 域: 某些 GPIO(如 RTC GPIO)在深睡时仍可保持状态,用于唤醒或维持外部电路。

03 项目软件流程描述

1. 调用 `esp_sleep_get_wakeup_cause()` 判断是冷启动(ESP_SLEEP_WAKEUP_UNDEFINED)还是深睡唤醒。

2. 打印唤醒源(定时器、EXT0、EXT1)和睡眠时长(通过 `esp_sleep_get_wakeup_time()` 获取)。

3. 根据需求配置下一次唤醒:调用 `esp_sleep_enable_timer_wakeup()` 或 `esp_sleep_enable_ext0_wakeup()` / `esp_sleep_enable_ext1_wakeup()`。

4. 关闭不需要的唤醒源,避免意外唤醒。

5. 将需要保留的数据(如计数器、状态标志)存入 RTC 内存(`RTC_DATA_ATTR`)或 NVS。

6. 调用 `esp_deep_sleep_start()` 进入深睡。

7. 唤醒后,程序从 `app_main()` 重新开始,读取 RTC 内存恢复状态。

04 项目核心点

  • 深睡不是待机,内存会丢。 所有普通 RAM 变量都会丢失,必须用 RTC 内存或 NVS 保存关键数据。
  • 浮空脚会误唤醒把电池抽干。 任何唤醒引脚必须接上拉/下拉电阻,或由外部电路驱动到确定电平。
  • 可定制: 唤醒矩阵(哪些 GPIO 组合触发)、休眠电流验收(用万用表测量实际电流)、上报周期(定时器时长)。
  • 注意不同芯片的差异: ESP32 和 ESP32-S2/S3 的 EXT0 引脚不同,参考官方示例。
  • 调试时注意: 深睡后 USB 会断开,使用外部 UART 时,确保串口工具支持断电重连。