ESP32 (29) – Deep sleep

luca 22/01/2018 9

One of the major concerns for embedded devices is the power consumption. If the device you’re designing will be battery powered, it’s indeed important to reduce as much as possible its power consumption  to maximize the autonomy (= the working time before it’s necessary to replace or recharge the battery).

The esp32 chip offers 5 different power modes. The “normal” mode is named active mode; in this mode all the features of the chip are available. By starting to reduce the CPU speed and disabling some peripherals and cores, the chip switches to different power saving modes, as summarized in the following diagram:


In this first post about the esp32 power saving modes, I’ll explain the deep sleep mode.

Deep sleep

The esp-idf framework actually supports two power saving modes: light sleep and deep sleep. Between the two, the deep sleep mode is the one which offers greater energy savings. In this mode, are turned off:

  • both the CPUs
  • most of the RAM memory
  • all the peripherals

by default are instead kept active:

  • the RTC controller
  • the RTC peripherals, including the ULP coprocessor
  • the RTC memories (slowfast)

You can put the chip in deep sleep with the esp_deep_sleep_start() method, while it’s possible to wake up it via different events:


When the chip wakes up from deep sleep, a new boot sequence is performed. It’s therefore very important to understand that the execution of your program does not restart at the point where the esp_deep_sleep_start() method is called.

Let’s see how to configure and use two wake up events; in a future post I’ll write about touch pad and ULP.


The simplest wake up event is for sure the one which leverages a timer of the RTC controller. Thanks to the method:

esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)

you can wake up the esp32 chip after the specified number of milliseconds. The method must be called before entering the deep sleep mode:

// wakeup after 10 seconds

I/O triggers

In a previous post I’ve already blogged about the possibility to receive interrupts when a digital pin of the chip changes its status. We can leverage a similar functionality to wake up the chip from sleep.

With the method

esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)

you can enable the wake up if the specified pin (gpio_num) changes its status (level).

You can only use the pins with RTC function (0, 2, 4, 12-15, 25-27, 32-39) and the possible levels are 0 (= low) or 1 (high). If, for example, you want to wake up the chip from deep sleep if pin 4 has a low level, you’ll write:

esp_sleep_enable_ext0_wakeup(4, 0);

The framework also offers a method to monitor different pins:

esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)

the pins (this method also accepts only the ones specified above) must be specified in a bitmask and  the wakeup modes are:

  • ESP_EXT1_WAKEUP_ALL_LOW = wakeup  when all the pins are low
  • ESP_EXT1_WAKEUP_ANY_HIGH = wakeup when at least one pin is high
When the chip wakes up from the sleep, the pins specified will be configured as RTC IO. To be able to use them again as normal digital pins, you have first to call the rtc_gpio_deinit(gpio_num) method. The ext0_wakeup method at the moment cannot be used together with touch pad or ULP events.

After the wake up…

If you configured more than one wake up event, you can know which specific event woke up the chip with:

esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause()

The possible constants are:


For the ext1_wakeup event, a specific method is available to get the bitmask of the pins:

uint64_t esp_sleep_get_ext1_wakeup_status()


As explained above, in deep sleep mode the content of the RTC fast and RTC slow memories is preserved. You can therefore use those memory segments to store data that must be retained during the sleep.

To ask the compiler to store a variable in the RTC slow memory, you can use the RTC_DATA_ATTR attribute, or the RTC_RODATA_ATTR one if the variable is read only:

RTC_DATA_ATTR static time_t last;


I wrote a program (its source code is in my Github repository) to demonstrate the deep sleep mode and two different wake up events:


  1. Stivi 07/03/2018 at 16:59 - Reply

    i have trouble with the deepsleep of the esp32 (huzzah32)

    I hope any one of you could help me.
    i am using this:

    switch (esp_deep_sleep_get_wakeup_cause()) {
    uint64_t wakeup_pin_mask = esp_deep_sleep_get_ext1_wakeup_status();
    if (wakeup_pin_mask != 0) {
    int pin = __builtin_ffsll(wakeup_pin_mask) – 1;
    printf(“Wake up from GPIO %d\n”, pin);
    } else {
    printf(“Wake up from GPIO\n”);

    and get the following error:

    “error: esp_deep_sleep_get_ext1_wakeup_status() not declared in this scope”

    I include deepsleep:

    Did anyone had an hint for me?

    Thanks a lot!!



    • luca 08/03/2018 at 15:15 - Reply

      Hi, it seems they changed the method to esp_sleep_get_ext1_wakeup_status()

  2. Stivi 09/03/2018 at 11:46 - Reply

    Thanks a lot Luca! You made my day!

    I haven´t seen it!

    It´s working now!

  3. Alessandro Dionisi 17/03/2018 at 17:30 - Reply

    Ciao Luca,

    Grazie del tuo video, mi sarà molto di aiuto nel progettino che sto realizzando. Vorrei costruire un tastierino wireless bluetooth usando un comune matrix keypad a membrana e ESP32. Il mio problema è che vorrei sfruttare il deep sleep e risvegliare il dispositivo solo quando l’utente digita qualche tasto. Mi accontenterei anche di perdere la prima cifra digitata eventualmente. Tu che consigli mi daresti per realizzare questa cosa? Grazie mille!

    • luca 18/03/2018 at 16:06 - Reply

      Ciao Alessandro, quei tastierini normalmente sono gestiti come “matrice”, quindi penso si possa usare il wake sui pin di I/O a cui hai collegato il tastierino! buon diverimento!

      • Alessandro Dionisi 18/03/2018 at 21:01 - Reply

        Si certo, però il problema è che dato che sono gestiti a matrice, per poter capire quale tasto è stato premuto, si cicla sulle righe (collegate ai pin di output) e si da livello logico alto, andando poi a leggere le colonne per capire quale tasto è stato premuto. Per questo motivo, diversamente dal pushbutton che è sempre “alimentato”, gli switch della matrice sono alimentati a rotazione ma solo mentre il codice è in esecuzione. Forse ho capito male io, me lo confermi? Grazie ancora!

  4. Jorge Fuentes 01/03/2019 at 16:51 - Reply

    Any compatibility with Time.h Library?

    • luca 03/03/2019 at 10:32 - Reply

      Hi, if you’re referring to Arduino Time library, sorry but my tutorials are not for Arduino

      • Jorge Fuentes 04/03/2019 at 14:01 - Reply

        Yes, this library is for Arduino, but it can use with ESP32.

        In general words, ¿can we store real time variables in deep-sleep mode?

Leave A Response »

This website uses cookies to ensure you get the best experience on our website maggiori informazioni

Questo sito utilizza i cookie per fonire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o clicchi su "Accetta" permetti al loro utilizzo.