ESPShell for Arduino :: Deep and Light Sleep

[ Russian ] ↔ [ English ]

Sleep modes in ESP32 and power consumption

ESP32 provides a Light Sleep mode, where peripheral clocks are gated off but RAM contents and CPU state remain preserved (fast wake-up, variables stay in memory). It also supports Deep Sleep, which powers down most internal components; waking up from this mode requires a full reboot, but power consumption becomes dramatically lower. Only the RTC memory and RTC peripherals remain powered (longer wake-up time, and persistent data must be stored explicitly in RTC memory).

Both modes reduce power consumption in battery-powered devices: Deep Sleep yields the lowest consumption (single-digit microamps), while Light Sleep (hundreds of microamps) allows fast resumption of operation and is often used together with Modem-sleep for Wi-Fi/Bluetooth in always-connected applications.

In ESPShell, sleep is controlled by a single command: "nap". With it, you can put the CPU into light or deep sleep and also configure wake-up conditions. A traditional status command is available as well: "show nap".

Sleeping: nap and nap deep

To put the processor into Light Sleep, use the "nap" command without arguments. For Deep Sleep (which reboots the chip on wake-up), use "nap deep". However, if you attempt this without configuring a wake-up source, ESPShell will refuse, pointing out that no wake-up condition is set:

  esp32#>nap
  % Wakeup source is not properly set, use "nap alarm" to set one
  % When should we wakeup?
  esp32#>
So first, let’s configure an alarm.

Waking up: alarms and events

ESPShell can wake the processor from three kinds of events:

Wake-up events are defined by the "nap alarm" command. Note that running several nap alarm commands does not replace the previous alarm — they are cumulative. For example, if you run "nap alarm uart 0" and then "nap alarm 5 sec", you will end up with two wake-up sources: a timer (5 seconds) and UART activity.

To clear all configured alarms, use "nap alarm disable-all":

  esp32#>nap alarm disable-all
  % All sleep wakeup sources were disabled
  esp32#>
If you are only changing a timer value, clearing alarms is unnecessary. But if you switch the wake-up source from timer to something else (e.g. to UART0), you must run nap alarm disable-all before defining the new source via nap alarm uart0.

IMPORTANT NOTE: on ESP32-S3 chips (with USB-CDC support), the chip cannot properly wake up from light sleep: after waking up, USB-CDC stops functioning. This issue occurs only when USB is used for its primary purpose. If the USB port is not used as a Hardware CDC interface, the chip wakes from light sleep correctly. ESPShell will warn you whenever it detects that waking up from light sleep may fail.

Example: putting the CPU into light sleep for 5 seconds

Now let’s put ESP32 into either light or deep sleep using ESPShell. First, configure an alarm — let’s set it to five seconds:

esp32#>nap alarm 5 sec
% Sleep wakeup timer: 5000000 usec
esp32#>
And now enter light or deep sleep:
  esp32#>nap
  % Entering light sleep
After 5 seconds the processor will wake up and continue execution. When waking up from deep sleep, the chip will reboot.

Command list and examples

CommandDescription and examples
nap [deep] nap
nap deep

Puts the processor into sleep mode: deep sleep (using the "deep" key) or light sleep (plain nap). After the processor returns to normal operation, you can run "uptime" to check the wake-up reason and the number of sleep cycles:

  % Last boot was 6 seconds ago
  % Reset reason: "returning from a deep sleep"
  %    CPU0: Deep sleep reset the digital core
  %    CPU1: Deep sleep reset the digital core
  % Returned from sleep: 1 time (sequental), wakeup caused by a timer
  % Slept for 5 seconds
  % Firmware reload count: 2 (# of resets since power-on)

nap alarm ... nap alarm uart NUMBER [EDGES]

Wake-up source: UART number NUMBER. Optional parameter EDGES (default: 3) defines how many rising edges on RX must occur to wake up the CPU. On DevKitC-clone boards, UART0 goes through a USB-UART converter chip. Due to this design, when the processor enters deep sleep, it cannot wake up on UART activity — the converter chip also shuts down. With a direct UART connection this issue does not occur. In Light Sleep this limitation also does not apply, since the converter chip remains powered.

nap alarm low|high PIN1 [PIN2 PIN3 ... PINn]

Wake-up source: a LOW or HIGH signal on pins PIN1, PIN2… PINn. For example, if we want the processor to wake up when logical HIGH appears on any of pins 2, 4, or 1:

            esp32#>nap alarm high 1 2 4
            % Sleep wakeup source: EXT1
            esp32#>
        

nap alarm TIMESPEC

Sets a time interval after which the processor should leave sleep mode. TIMESPEC describes a duration using a free-form combination of days, hours, minutes, seconds, and milliseconds:

  esp32#>nap alarm 25 days 1 hour 55 min 1 s
  % Sleep wakeup timer: 2166901000000 usec
  esp32#>
If TIMESPEC is a single number (e.g. "nap alarm 1"), it is treated as seconds. Thus, "nap alarm 1" wakes the processor exactly 1 second after entering sleep.

nap alarm disable-all

Disables all previously configured wake-up sources.

show nap show nap

Displays all currently active wake-up sources:

  esp32#>show nap
  % Enabled wakeup source: TIMER, duration: 3888000 sec   ← regular 45-day timer
  % Enabled wakeup source: EXT0 (single GPIO)             ← GPIO pin
  % Enabled wakeup source: UART RX                        ← UART activity
  esp32#>