esp32-deep-sleep

How to use Deep Sleep mode on an ESP32

  • 5 min

Deep Sleep is a low-power mode that almost completely shuts down the ESP32, resulting in extremely low power consumption.

In this mode, the ESP32 consumes only a few microamps of current, making it ideal for battery-powered or power-limited applications.

When the ESP32 is in Deep Sleep, it stops executing the program and enters a suspended state. The ESP32 shuts down most of the microcontroller’s internal circuits, except for some essential components like the Real-Time Clock (RTC).

To exit Deep Sleep mode, the available WakeUp Sources are:

  • Timer
  • Touchpad
  • ULP Coprocessor Wakeup
  • External Wakeup (Ext0 and Ext1)

Upon exiting Deep Sleep, the program restarts from the beginning. Variables stored in “normal” memory are erased. It’s almost like resetting the ESP32.

RTC

The RTC plays a very important role in Deep Sleep mode. If you need more information about this component, see ESP32 Hardware Details.

Saving Data in RTC Memory

To save data in RTC memory, you simply need to add the RTC_DATA_ATTR attribute before defining a variable.

RTC_DATA_ATTR int bootCount = 0;
Copied!

Wake Stub

We also have the possibility to execute a function when returning from deep sleep mode.

This function will only run when we return from a Deep Sleep mode, but not on the first boot of the processor.

This function is also stored in fast memory, and for that we use the RTC_IRAM_ATTR attribute.

void RTC_IRAM_ATTR esp_wake_deep_sleep() {
    esp_default_wake_deep_sleep();
    // add additional functionality here
}
Copied!

Obviously, this function should be as short as possible, normally just changing the content of a variable.

Using Deep Sleep Mode in the Arduino IDE

The functions needed to use Deep Sleep mode are defined in the “esp_sleep.h” file. This library contains the necessary functions and macros to configure and control the ESP32’s Deep Sleep mode.

#include <esp_sleep.h>
Copied!

Before entering Deep Sleep mode, we must configure a WakeUp Source. For example, if we want to use the Timer to wake up the ESP32 after a certain time, we can do so with the esp_sleep_enable_timer_wakeup() function.

This function takes as an argument the time in microseconds that the ESP32 should remain in Deep Sleep before waking up.

Finally, we would enter Deep Sleep mode using the esp_deep_sleep_start() function. This function will put the ESP32 into Deep Sleep mode and keep it in that state until a WakeUp Source occurs.

For example, to make the ESP32 wake up after 5 seconds:

esp_sleep_enable_timer_wakeup(5 * 1000000);
esp_deep_sleep_start();
Copied!

Using the Timer as a WakeUp source is so common that there is even an alternative, more concise function that allows us to use it in a single function.

/**
 * @brief Enter deep-sleep mode
 *
 * The device will automatically wake up after the deep-sleep time
 * Upon waking up, the device calls deep sleep wake stub, and then proceeds to load application.
 */
void esp_deep_sleep(uint64_t time_in_us) __attribute__((noreturn));
Copied!

Code Examples