ESP32 (29) – Deep sleep

by luca

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:

[youtube id=”j69Ulck-1Qs” width=”600″ height=”350″]

Related Posts


Stivi Wednesday March 7th, 2018 - 04:59 PM

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 Thursday March 8th, 2018 - 03:15 PM

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

Stivi Friday March 9th, 2018 - 11:46 AM

Thanks a lot Luca! You made my day!

I haven´t seen it!

It´s working now!

Alessandro Dionisi Saturday March 17th, 2018 - 05:30 PM

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 Sunday March 18th, 2018 - 04:06 PM

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 Sunday March 18th, 2018 - 09:01 PM

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!

Jorge Fuentes Friday March 1st, 2019 - 04:51 PM

Any compatibility with Time.h Library?

luca Sunday March 3rd, 2019 - 10:32 AM

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

Jorge Fuentes Monday March 4th, 2019 - 02:01 PM

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 Comment

10 − 4 =