This article is a complete guide for the ESP32 Deep Sleep mode with Arduino IDE. We’ll show you how to put the ESP32 into deep sleep and take a look at different modes to wake it up: timer wake up, touch wake up, and external wake up. This guide provides practical examples with code, code explanation, and circuit diagrams.
Updated 8 October 2024.
Related Content: ESP8266 Deep Sleep with Arduino IDE
This article is divided into 4 different parts:
The ESP32 can switch between different power modes:
You can compare the five different modes on the following table from the ESP32 Espressif datasheet.
The ESP32 Espressif datasheet also provides a table comparing the power consumption of the different power modes.
And here’s also Table 10 to compare the power consumption in active mode:
Having your ESP32 running on active mode with batteries is not ideal since batteries’ power will drain very quickly.
If you put your ESP32 in deep sleep mode, it will reduce the power consumption, and your batteries will last longer.
Having your ESP32 in deep sleep mode means cutting with the activities that consume more power while operating but leaving just enough activity to wake up the processor when something interesting happens.
In deep sleep mode, neither CPU nor Wi-Fi activities take place, but the Ultra Low Power (ULP) co-processor can still be powered on.
While the ESP32 is in deep sleep mode, the RTC memory also remains powered on, so we can write a program for the ULP co-processor and store it in the RTC memory to access peripheral devices, internal timers, and internal sensors.
This mode of operation is useful if you need to wake up the main CPU by an external event, timer, or both while maintaining minimal power consumption.
During deep sleep, some of the ESP32 pins can be used by the ULP co-processor, namely the RTC_GPIO pins, and the Touch Pins. The ESP32 datasheet provides a table identifying the RTC_GPIO pins. You can find that table here on page 14.
You can use that table as a reference, or take a look at the following pinout to locate the different RTC_GPIO pins. The RTC_GPIO pins are highlighted with an orange rectangular box.
You might also like reading: ESP32 Pinout Reference: Which GPIO pins should you use?
After putting the ESP32 into deep sleep mode, there are several ways to wake it up:
To write a sketch to put your ESP32 into deep sleep mode, and then wake it up, you need to keep in mind that:
The ESP32 can go into deep sleep mode, and then wake up at predefined periods of time. This feature is specially useful if you are running projects that require time stamping or daily tasks, while maintaining low power consumption.
The ESP32 RTC controller has a built-in timer you can use to wake up the ESP32 after a predefined amount of time.
Enabling the ESP32 to wake up after a predefined amount of time is very straightforward. In the Arduino IDE, you just have to specify the sleep time in microseconds in the following function:
esp_sleep_enable_timer_wakeup(time_in_us)
Let’s see how this works using an example from the library. Open your Arduino IDE, and go to File > Examples > ESP32 Deep Sleep, and open the TimerWakeUp sketch.
/*
Simple Deep Sleep with Timer Wake Up
=====================================
ESP32 offers a deep sleep mode for effective power
saving as power is an important factor for IoT
applications. In this mode CPUs, most of the RAM,
and all the digital peripherals which are clocked
from APB_CLK are powered off. The only parts of
the chip which can still be powered on are:
RTC controller, RTC peripherals ,and RTC memories
This code displays the most basic deep sleep with
a timer to wake it up and how to store data in
RTC memory to use it over reboots
This code is under Public Domain License.
Author:
Pranav Cherukupalli <cherukupallip@gmail.com>
*/
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void setup(){
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
/*
First we configure the wake up source
We set our ESP32 to wake up every 5 seconds
*/
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
/*
Next we decide what all peripherals to shut down/keep on
By default, ESP32 will automatically power down the peripherals
not needed by the wakeup source, but if you want to be a poweruser
this is for you. Read in detail at the API docs
http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
Left the line commented as an example of how to configure peripherals.
The line below turns off all RTC peripherals in deep sleep.
*/
//esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
//Serial.println("Configured all RTC Peripherals to be powered down in sleep");
/*
Now that we have setup a wake cause and if needed setup the
peripherals state in deep sleep, we can now start going to
deep sleep.
In the case that no wake up sources were provided but deep
sleep was started, it will sleep forever unless hardware
reset occurs.
*/
Serial.println("Going to sleep now");
delay(1000);
Serial.flush();
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop(){
//This is not going to be called
}
Let’s take a look at this code. The first comment describes what is powered off during deep sleep with timer wake up.
In this mode CPUs, most of the RAM, and all the digital peripherals which are clocked from APB_CLK are powered off. The only parts of the chip which can still be powered on are: RTC controller, RTC peripherals ,and RTC memories
When you use timer wake-up, the parts that will be powered on are RTC controller, RTC peripherals, and RTC memories.
These first two lines of code define the period of time the ESP32 will be sleeping.
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
This example uses a conversion factor from microseconds to seconds, so that you can set the sleep time in the TIME_TO_SLEEP variable in seconds. In this case, the example will put the ESP32 into deep sleep mode for 5 seconds.
With the ESP32, you can save data on the RTC memories. The ESP32 has 8kB SRAM on the RTC part, called RTC fast memory. The data saved here is not erased during deep sleep. However, it is erased when you press the reset button (the button labeled EN on the ESP32 board).
To save data on the RTC memory, you just have to add RTC_DATA_ATTR before a variable definition. The example saves the bootCount variable on the RTC memory. This variable will count how many times the ESP32 has woken up from deep sleep.
RTC_DATA_ATTR int bootCount = 0;
Then, the code defines the print_wakeup_reason() function, that prints the source that caused the wake-up from deep sleep.
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println("Wakeup caused by ULP program"); break;
default: Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
}
}
In the setup() is where you should put your code. You need to write all the instructions before calling the esp_deep_sleep_start() function.
This example starts by initializing the serial communication at a baud rate of 115200.
Serial.begin(115200);
Then, the bootCount variable is increased by one in every reboot, and that number is printed in the serial monitor.
++bootCount;
Serial.println("Boot number: " + String(bootCount));
Then, the code calls the print_wakeup_reason() function, but you can call any function you want to perform a desired task. For example, you may want to wake up your ESP32 once a day to read a value from a sensor.
Next, the code defines the wake-up source by using the following function:
esp_sleep_enable_timer_wakeup(time_in_us)
This function accepts as argument the time to sleep in microseconds as we’ve seen previously. In our case, we have the following:
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Then, after all the tasks are performed, the ESP32 goes to sleep by calling the following function:
esp_deep_sleep_start()
As soon as you call the esp_deep_sleep_start() function, the ESP32 will go to sleep and will not run any code written after this function. When it wakes up from deep sleep, it will run the code from the beginning.
The loop() section is empty because the ESP32 will sleep before reaching this part of the code. So, you need to write all your tasks in the setup() before calling the esp_deep_sleep_start() function.
Upload the example sketch to your ESP32. Make sure you have the right board and COM port selected. Open the Serial Monitor at a baud rate of 115200.
Every 5 seconds, the ESP32 wakes up, prints a message on the serial monitor, and goes to deep sleep again.
Every time the ESP32 wakes up, the bootCount variable increases. It also prints the wake-up reason as shown in the figure below.
However, notice that if you press the EN button on the ESP32 board, it resets the boot count to 1 again.
You can modify the provided example, and instead of printing a message you can make your ESP do any other task. The timer wake up is useful to perform periodic tasks with the ESP32, like daily tasks, without draining much power.
You can wake up the ESP32 from deep sleep using the touch pins. This section shows how to do that using the Arduino IDE.
Enabling the ESP32 to wake up using a touchpin is simple. In the Arduino IDE, you need to use the following function—pass as argument the touch pin and the touch threshold:
touchSleepWakeUpEnable(TOUCH_PIN, THRESHOLD);
Let’s see how this works using an example from the library. Open your Arduino IDE, and go to File > Examples > ESP32 Deep Sleep, and open the TouchWakeUp sketch.
/*
Deep Sleep with Touch Wake Up
=====================================
This code displays how to use deep sleep with
a touch as a wake up source and how to store data in
RTC memory to use it over reboots
ESP32 can have multiple touch pads enabled as wakeup source
ESP32-S2 and ESP32-S3 supports only 1 touch pad as wakeup source enabled
This code is under Public Domain License.
Author:
Pranav Cherukupalli <cherukupallip@gmail.com>
*/
#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40 /* Greater the value, more the sensitivity */
#else //ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted) */
#define THRESHOLD 5000 /* Lower the value, more the sensitivity */
#endif
RTC_DATA_ATTR int bootCount = 0;
touch_pad_t touchPin;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println("Wakeup caused by ULP program"); break;
default: Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
}
}
/*
Method to print the touchpad by which ESP32
has been awaken from sleep
*/
void print_wakeup_touchpad() {
touchPin = esp_sleep_get_touchpad_wakeup_status();
#if CONFIG_IDF_TARGET_ESP32
switch (touchPin) {
case 0: Serial.println("Touch detected on GPIO 4"); break;
case 1: Serial.println("Touch detected on GPIO 0"); break;
case 2: Serial.println("Touch detected on GPIO 2"); break;
case 3: Serial.println("Touch detected on GPIO 15"); break;
case 4: Serial.println("Touch detected on GPIO 13"); break;
case 5: Serial.println("Touch detected on GPIO 12"); break;
case 6: Serial.println("Touch detected on GPIO 14"); break;
case 7: Serial.println("Touch detected on GPIO 27"); break;
case 8: Serial.println("Touch detected on GPIO 33"); break;
case 9: Serial.println("Touch detected on GPIO 32"); break;
default: Serial.println("Wakeup not by touchpad"); break;
}
#else
if (touchPin < TOUCH_PAD_MAX) {
Serial.printf("Touch detected on GPIO %d\n", touchPin);
} else {
Serial.println("Wakeup not by touchpad");
}
#endif
}
void setup() {
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32 and touchpad too
print_wakeup_reason();
print_wakeup_touchpad();
#if CONFIG_IDF_TARGET_ESP32
//Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
touchSleepWakeUpEnable(T3, THRESHOLD);
touchSleepWakeUpEnable(T7, THRESHOLD);
#else //ESP32-S2 + ESP32-S3
//Setup sleep wakeup on Touch Pad 3 (GPIO3)
touchSleepWakeUpEnable(T3, THRESHOLD);
#endif
//Go to sleep now
Serial.println("Going to sleep now");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() {
//This will never be reached
}
The first thing you need to do is setting a threshold value for the touch pins.
#define THRESHOLD 40 // Greater the value, more the sensitivity
The values read by a touch pin decrease when you touch it. The threshold value means that the ESP32 will wake up when the value read on the touch pin is below 40. You can adjust that value depending on the desired sensitivity.
Important: however, if you’re using an ESP32-S2 or ESP32-S3 model, things work a little
differently. That’s why there’s a different section in the code defining a different threshold for those boards. In this case, the lower the value, the more the sensitivity.
#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40 // Greater the value, more the sensitivity
#else // ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted)
#define THRESHOLD 5000 // Lower the value, more the sensitivity
#endif
To set a touch pin as a wake-up source, you can use the touchSleepWakeUpEnable() function that accepts as arguments the touch pin and the threshold value that will wake up the board.
In this example, it sets GPIO 15 (T3) and GPIO 27 (T7) as wake-up sources with the same threshold value.
#if CONFIG_IDF_TARGET_ESP32
// Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
touchSleepWakeUpEnable(T3, THRESHOLD);
touchSleepWakeUpEnable(T7, THRESHOLD);
If you’re using an ESP32-S2 or S3 model, the example just defines wake-up on GPIO 3 (T3). Note that the touch pin numbering might be different depending on the board model you’re using.
#else // ESP32-S2 + ESP32-S3
// Setup sleep wakeup on Touch Pad 3 (GPIO3)
touchSleepWakeUpEnable(T3, THRESHOLD);
Finally, call the esp_deep_sleep_start() to put the ESP32 in deep sleep mode.
esp_deep_sleep_start();
These were the general instructions to set up touch pins as a wake-up source. Now, let’s take a look at the other sections of the code.
When the ESP32 wakes up from sleep, it will run the code from the start until it finds the esp_deep_sleep_start() function.
In this particular example, we have a control variable called bootCount that will be increased between each sleep cycle so that we have an idea of how many times the ESP32 woke up.
// Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
We also call the print_wakeup_reason() and print_wakeup_touchpad() functions defined earlier in the code to print the wake-up reason and the pin that caused the wake-up.
// Print the wakeup reason for ESP32 and touchpad too
print_wakeup_reason();
print_wakeup_touchpad();
To test this example, wire a cable to GPIO 15, as shown in the schematic below.
If you’re using an ESP32-S2 or ESP32-S3 model, please check the location of your touch pins.
Upload the code to your ESP32, and open the Serial Monitor at a baud rate of 115200.
The ESP32 goes into deep sleep mode.
You can wake it up by touching the wire connected to Touch Pin 3.
When you touch the pin, the ESP32 displays on the Serial Monitor: the boot number, the wake-up cause, and which touch-sensitive GPIO caused the wake-up.
Besides the timer and the touch pins, we can also awake the ESP32 from deep sleep by toggling the value of a signal on a pin, like the press of a button. This is called an external wake up. You have two possibilities of external wake up: ext0, and ext1.
This wake up source allows you to use a pin to wake up the ESP32.
The ext0 wake up source option uses RTC GPIOs to wake up. So, RTC peripherals will be kept on during deep sleep if this wake up source is requested.
To use this wake up source, you use the following function:
esp_sleep_enable_ext0_wakeup(GPIO_NUM_X, level)
This function accepts as first argument the pin you want to use, in this format GPIO_NUM_X, in which X represents the GPIO number of that pin.
The second argument, level, can be either 1 or 0. This represents the state of the GPIO that will trigger wake up.
Note: with this wake up source, you can only use pins that are RTC GPIOs.
the RTC controller. So, RTC peripherals and RTC memories can be powered off in this mode.
To use this wake-up source, you use the following function:
esp_sleep_enable_ext1_wakeup_io(bitmask, mode);
This function accepts two arguments:
If you’re using an ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2, these are the available modes:
Note: you can only use pins that are RTC GPIOs.
For all the details about the ext1 deep sleep wake-up source, take a look at the Espressif deep sleep documentation.
y use pins that are RTC GPIOs.
Let’s explore the example that comes with the ESP32 library. Go to File > Examples > ESP32 Deep Sleep > ExternalWakeUp:
/*
Deep Sleep with External Wake Up
=====================================
This code displays how to use deep sleep with
an external trigger as a wake up source and how
to store data in RTC memory to use it over reboots
This code is under Public Domain License.
Hardware Connections
======================
Push Button to GPIO 33 pulled down with a 10K Ohm
resistor
NOTE:
======
Only RTC IO can be used as a source for external wake
source. They are pins: 0,2,4,12-15,25-27,32-39.
Author:
Pranav Cherukupalli <cherukupallip@gmail.com>
*/
#include "driver/rtc_io.h"
#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) // 2 ^ GPIO_NUMBER in hex
#define USE_EXT0_WAKEUP 1 // 1 = EXT0 wakeup, 0 = EXT1 wakeup
#define WAKEUP_GPIO GPIO_NUM_33 // Only RTC IO are allowed - ESP32 Pin example
RTC_DATA_ATTR int bootCount = 0;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println("Wakeup caused by ULP program"); break;
default: Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
}
}
void setup() {
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
/*
First we configure the wake up source
We set our ESP32 to wake up for an external trigger.
There are two types for ESP32, ext0 and ext1 .
ext0 uses RTC_IO to wakeup thus requires RTC peripherals
to be on while ext1 uses RTC Controller so does not need
peripherals to be powered on.
Note that using internal pullups/pulldowns also requires
RTC peripherals to be turned on.
*/
#if USE_EXT0_WAKEUP
esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1); //1 = High, 0 = Low
// Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
// EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
// No need to keep that power domain explicitly, unlike EXT1.
rtc_gpio_pullup_dis(WAKEUP_GPIO);
rtc_gpio_pulldown_en(WAKEUP_GPIO);
#else // EXT1 WAKEUP
//If you were to use ext1, you would use it like
esp_sleep_enable_ext1_wakeup_io(BUTTON_PIN_BITMASK(WAKEUP_GPIO), ESP_EXT1_WAKEUP_ANY_HIGH);
/*
If there are no external pull-up/downs, tie wakeup pins to inactive level with internal pull-up/downs via RTC IO
during deepsleep. However, RTC IO relies on the RTC_PERIPH power domain. Keeping this power domain on will
increase some power comsumption. However, if we turn off the RTC_PERIPH domain or if certain chips lack the RTC_PERIPH
domain, we will use the HOLD feature to maintain the pull-up and pull-down on the pins during sleep.
*/
rtc_gpio_pulldown_en(WAKEUP_GPIO); // GPIO33 is tie to GND in order to wake up in HIGH
rtc_gpio_pullup_dis(WAKEUP_GPIO); // Disable PULL_UP in order to allow it to wakeup on HIGH
#endif
//Go to sleep now
Serial.println("Going to sleep now");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() {
//This is not going to be called
}
This example awakes the ESP32 when you trigger GPIO 33 to high. The code example shows how to use both methods: ext0 and ext1. If you upload the code as it is, you’ll use ext0. The function to use ext1 is commented. We’ll show you how both methods work and how to use them.
Let’s take a quick look at the code.
With the ESP32, you can save data on the RTC memories. The ESP32 has 8kB SRAM on the RTC part, called RTC fast memory. The data saved here is not erased during deep sleep. However, it is erased when you press the reset button (the button labeled EN on the ESP32 board).
To save data on the RTC memory, you just have to add RTC_DATA_ATTR before a variable definition. The example saves the bootCountvariable on the RTC memory. This variable will count how many times the ESP32 has woken up from deep sleep.
RTC_DATA_ATTR int bootCount = 0;
Then, the code defines the print_wakeup_reason() function, that prints the reason by which the ESP32 has been awakened from sleep.
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println("Wakeup caused by ULP program"); break;
default: Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
}
}
In the setup(), you start by initializing the serial communication:
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
Then, you increment one to the bootCount variable, and print that variable in the Serial Monitor.
++bootCount;
Serial.println("Boot number: " + String(bootCount));
Next, you print the wake-up reason using the print_wakeup_reason() function defined earlier.
//Print the wakeup reason for ESP32
print_wakeup_reason();
After this, you need to enable the wake-up sources. We’ll test each of the wake-up sources, ext0 and ext1, separately.
In this example, the ESP32 wakes up when the GPIO 33 is triggered to high:
esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1); // 1 = High, 0 = Low
Instead of GPIO 33, you can use any other RTC GPIO pin. Just define it on the WAKEUP_GPIO variable at the beginning of the code.
#define WAKEUP_GPIO GPIO_NUM_33 // Only RTC IO are allowed
We also call the following two lines:
rtc_gpio_pullup_dis(WAKEUP_GPIO);
rtc_gpio_pulldown_en(WAKEUP_GPIO);
The rtc_gpio_pullup_dis() function disables any internal pull-up resistors on the wake-up GPIO—it ensures that the pin is not unintentionally held high. This is important because a pull-up resistor would keep the pin high, potentially causing unintended wakeups.
The rtc_gpio_pulldown_en() function enables the internal pull-down resistor on the wake-up GPIO—it ensures the pin is held low until a valid wakeup signal (HIGH) is received. By configuring the pin with a pull-down resistor, we guarantee that it remains in a stable low state during deep sleep. This stability ensures that the ESP32 wakes up only when the specified GPIO pin receives an external high signal, matching the wakeup condition set by the esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1).
To test this example, wire a pushbutton to your ESP32 by following the next schematic diagram. The button is connected to GPIO 33 using a pull down 10K Ohm resistor.
Note: only RTC GPIOs can be used as a wake up source. Instead of GPIO 33, you could also use any RTC GPIO pins to connect your button.
Let’s test this example. Upload the example code to your ESP32. Make sure you have the right board and COM port selected. Open the Serial Monitor at a baud rate of 115200.
Press the pushbutton to wake up the ESP32.
Try this several times, and see the boot count increasing with each button press.
Using this method is useful to wake up your ESP32 using a pushbutton, for example, to execute a particular task. However, with this method, you can only use one GPIO as a wake-up source.
What if you want to have different buttons, all of them wake up the ESP, but do different tasks? For
The ext1 wake-up source allows you to wake up the ESP32 using different GPIOs and perform different tasks depending on the GPIO that caused the wake-up.
Instead of using the esp_sleep_enable_ext0_wakeup() function, you use the esp_sleep_enable_ext1_wakeup_io() function.
esp_sleep_enable_ext1_wakeup_io(BUTTON_PIN_BITMASK(WAKEUP_GPIO), ESP_EXT1_WAKEUP_ANY_HIGH);
In this particular example, to run that part of the code, make sure you set the USE_EXT0_WAKEUP variable to 0 at the beginning of the code like so:
#define USE_EXT0_WAKEUP 0 // 1 = EXT0 wakeup, 0 = EXT1 wakeup
The first argument of the esp_sleep_enable_ext1_wakeup_io() function is a bitmask of the GPIOs you’ll use as a wake-up source, and the second argument defines the logic to wake up the ESP32.
To define the GPIOs bitmask, we can use the BUTTON_PIN_BITMASK() macro defined at the beginning of the code.
#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) // 2 ^ GPIO_NUMBER in hex
BUTTON_PIN_BITMASK(GPIO) is a macro that creates a bitmask for a specific GPIO. It is not a function but behaves somewhat similarly by accepting an argument and returning a value.
So, if you want to return the bitmask for a specific GPIO, you just need to call it like so:
BUTTON_PIN_BITMASK(WAKEUP_GPIO)
So, the esp_sleep_enable_ext1_wakeup_io() will look like this:
esp_sleep_enable_ext1_wakeup_io(BUTTON_PIN_BITMASK(WAKEUP_GPIO), ESP_EXT1_WAKEUP_ANY_HIGH);
To create a bitmask for multiple GPIOs using the BUTTON_PIN_BITMASK(GPIO) macro, you can use bitwise OR (|) to combine the individual bitmasks for each GPIO pin. Here’s how you can do it:
Suppose you want to create a bitmask for GPIO pins 2 and 15. You can do this by combining the individual bitmasks for each pin like this:
uint64_t bitmask = BUTTON_PIN_BITMASK(GPIO_NUM_2) | BUTTON_PIN_BITMASK(GPIO_NUM_15);
We’ll see an example using multiple GPIOs as a wake-up source next.
The second argument of the esp_sleep_enable_ext1_wakeup_io() function is the wake-up mode. As we’ve seen previously, these are your options:
If you’re using an ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2, these are the available modes:
In our particular example, we’re using ESP_EXT1_WAKEUP_ANY_HIGH. So, the ESP32 will wake-up when any of the GPIOs from the bitmask are high.
Like with the ext0, we disable pull-up internal resistors and enable pull-down resistors to ensure stability of the reading of the wake-up GPIO.
Now, you should be able to wake up the ESP32 using different buttons, and identify which button caused the wake up. In this example we’ll use GPIO 2 and GPIO 15 as a wake-up source.
Wire two buttons to your ESP32. In this example, we’re using GPIO 2 and GPIO 15, but you can connect your buttons to any RTC GPIOs.
You need to make some modifications to the example code we’ve used before:
The next sketch has all those changes implemented.
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
https://RandomNerdTutorials.com/learn-esp32-with-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include "driver/rtc_io.h"
#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) // 2 ^ GPIO_NUMBER in hex
#define WAKEUP_GPIO_2 GPIO_NUM_2 // Only RTC IO are allowed - ESP32 Pin example
#define WAKEUP_GPIO_15 GPIO_NUM_15 // Only RTC IO are allowed - ESP32 Pin example
// Define bitmask for multiple GPIOs
uint64_t bitmask = BUTTON_PIN_BITMASK(WAKEUP_GPIO_2) | BUTTON_PIN_BITMASK(WAKEUP_GPIO_15);
RTC_DATA_ATTR int bootCount = 0;
/*
Method to print the GPIO that triggered the wakeup
*/
void print_GPIO_wake_up(){
int GPIO_reason = esp_sleep_get_ext1_wakeup_status();
Serial.print("GPIO that triggered the wake up: GPIO ");
Serial.println((log(GPIO_reason))/log(2), 0);
}
/*
Method to print the reason by which ESP32 has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0:
Serial.println("Wakeup caused by external signal using RTC_IO");
break;
case ESP_SLEEP_WAKEUP_EXT1:
Serial.println("Wakeup caused by external signal using RTC_CNTL");
print_GPIO_wake_up();
break;
case ESP_SLEEP_WAKEUP_TIMER:
Serial.println("Wakeup caused by timer");
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
Serial.println("Wakeup caused by touchpad");
break;
case ESP_SLEEP_WAKEUP_ULP:
Serial.println("Wakeup caused by ULP program");
break;
default:
Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason);
break;
}
}
void setup() {
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
//Use ext1 as a wake-up source
esp_sleep_enable_ext1_wakeup_io(bitmask, ESP_EXT1_WAKEUP_ANY_HIGH);
// enable pull-down resistors and disable pull-up resistors
rtc_gpio_pulldown_en(WAKEUP_GPIO_2);
rtc_gpio_pullup_dis(WAKEUP_GPIO_2);
rtc_gpio_pulldown_en(WAKEUP_GPIO_15);
rtc_gpio_pullup_dis(WAKEUP_GPIO_15);
//Go to sleep now
Serial.println("Going to sleep now");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() {
//This is not going to be called
}
Let’s take a quick look at how the code works.
You define the GPIOs bitmask at the beginning of the code:
#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) // 2 ^ GPIO_NUMBER in hex
#define WAKEUP_GPIO_2 GPIO_NUM_2 // Only RTC IO are allowed -
#define WAKEUP_GPIO_15 GPIO_NUM_15 // Only RTC IO are allowed
// Define bitmask for multiple GPIOs
uint64_t bitmask = BUTTON_PIN_BITMASK(WAKEUP_GPIO_2) | BUTTON_PIN_BITMASK(WAKEUP_GPIO_15);
We create a function called print_GPIO_wake_up() that prints the GPIO that caused the wake-up:
void print_GPIO_wake_up(){
int GPIO_reason = esp_sleep_get_ext1_wakeup_status();
Serial.print("GPIO that triggered the wake up: GPIO ");
Serial.println((log(GPIO_reason))/log(2), 0);
}
The esp_sleep_get_ext1_wakeup_status() function returns a bitmask with the GPIO that caused the wake-up. We can get the GPIO number as follows:
log(GPIO_reason))/log(2)
We modified the print_wakeup_reason() function to print the GPIO that caused the wake-up when the wake-up source is ext1:
case ESP_SLEEP_WAKEUP_EXT1:
Serial.println("Wakeup caused by external signal using RTC_CNTL");
print_GPIO_wake_up();
break;
Enable ext1 as a wake-up source by passing the GPIOs bitmask and wake-up mode.
esp_sleep_enable_ext1_wakeup_io(bitmask, ESP_EXT1_WAKEUP_ANY_HIGH);
Don’t forget to disable any internal pull-up resistors and enable pull-down resistors on the GPIOs used as a wake-up source.
rtc_gpio_pulldown_en(WAKEUP_GPIO_2);
rtc_gpio_pullup_dis(WAKEUP_GPIO_2);
rtc_gpio_pulldown_en(WAKEUP_GPIO_15);
rtc_gpio_pullup_dis(WAKEUP_GPIO_15);
Finally, call esp_deep_sleep_start() to put the ESP32 in deep sleep mode.
esp_deep_sleep_start();
After uploading the code to the board, the ESP32 will be in deep sleep mode. You can wake it up by pressing the pushbuttons.
Open the Serial Monitor at a baud rate of 115200. Press the pushbuttons to wake up the ESP32. On the Serial Monitor, you should get the wake-up reason and the GPIO that caused the wake-up.
In this article we’ve shown you how to use deep sleep with the ESP32 and different ways to wake it up. You can wake up the ESP32 using a timer, the touch pins, or a change on a GPIO state.
Let’s summarize what you’ve learned about each wake-up source:
Nguồn: https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/