ESP32 External Wake Up from Deep Sleep
This article shows how to put the ESP32 in deep sleep mode and wake it with an external wake up like a button press. We'll cover how to use ext0 and ext1 methods.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelIntermediate
- Available atAjvika Labs, Kakinada
About this project
This article shows how to put the ESP32 in deep sleep mode and wake it up with an external wake-up. An external wake-up source can be the press of a pushbutton, the detection of movement, or other scenarios that involve changing the value of a signal: from HIGH to LOW or LOW to HIGH.
You have two possibilities of external wake-up:
ext0
, and
ext1
Throughout this article we’ll cover the following subjects:
how to put the ESP32 in deep sleep mode;
wake up the ESP32 by changing the value of one GPIO (with a pushbutton) using the ext0 method;
wake up the ESP32 using several GPIOs using the ext1 method;
identify which GPIO caused the wake-up.
To learn more about deep sleep and other wake-up sources, you can follow the next tutorials:
[Complete Guide]
ESP32 Deep Sleep with Arduino IDE and Wake Up Sources
ESP32 Touch Wake Up from Deep Sleep
ESP32 Timer Wake Up from Deep Sleep
Writing a Deep Sleep Sketch
To write a sketch to put your ESP32 into deep sleep mode, and then wake it up, you need to keep in mind that:
First, you need to configure the wake-up sources. This means configuring what will wake up the ESP32. You can use one or combine more than one wake-up source. In this article, we’ll show you how to use the external wake-up sources.
You can decide what peripherals to shut down or keep on during deep sleep. However, by default, the ESP32 automatically powers down the peripherals that are not needed with the wake-up source you define.
Finally, you use the
esp_deep_sleep_start()
function to put your ESP32 into deep sleep mode.
External Wake Up
External wake up is one of the ways to wake up the ESP32 from deep sleep. This means that you can wake up the ESP32 by toggling the value of a signal on a pin, like the press of a button. 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 are 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 the first argument the pin you want to use, in this format
GPIO_NUM_X
, in which
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:
you can only use pins that are
RTC GPIOs
with this wake-up source. Here’s a list of the RTC GPIOs for different ESP32 chip models:
ESP32-S3
: 0-21;
ESP32
: 0, 2, 4, 12-15, 25-27, 32-39;
ESP32-S2
: 0-21;
This wake-up source allows you to use multiple RTC GPIOs. This wake-up source is implemented by 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:
A bitmask of the GPIO numbers that will cause the wake up;
Mode
: the logic to wake up the ESP32. It can be:
ESP_EXT1_WAKEUP_ALL_LOW
: wake up when all GPIOs go low;
ESP_EXT1_WAKEUP_ANY_HIGH
: wake up if any of the GPIOs go high.
If you’re using an ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2, these are the available modes:
ESP_EXT1_WAKEUP_ANY_LOW
: wake up when any of the selected GPIOs is low
ESP_EXT1_WAKEUP_ANY_HIGH
: wake up when any of the selected GPIOs is high
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
Parts Required
To follow this tutorial, you need the following parts:
ESP32 DOIT DEVKIT V1 Board
(read Best
ESP32 development boards
)
2x Pushbuttons
2x 10k Ohm resistor
Breadboard
Jumper wires
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Code – External Wake Up Example
To program the ESP32 we’ll use Arduino IDE. So, you need to make sure you have the ESP32 Arduino core installed. Follow the next tutorial to install the ESP32 add-on, if you haven’t already:
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
The following example demonstrates how to use the external wake-up source. You can find it at
File
>
Examples
>
ESP32
Deep Sleep
>
ExternalWakeUp
, or you can copy it from below.
/*
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 <
[email protected]
>
*/
#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
You have two possibilities of external wake-up:
ext0
, and
ext1
Throughout this article we’ll cover the following subjects:
how to put the ESP32 in deep sleep mode;
wake up the ESP32 by changing the value of one GPIO (with a pushbutton) using the ext0 method;
wake up the ESP32 using several GPIOs using the ext1 method;
identify which GPIO caused the wake-up.
To learn more about deep sleep and other wake-up sources, you can follow the next tutorials:
[Complete Guide]
ESP32 Deep Sleep with Arduino IDE and Wake Up Sources
ESP32 Touch Wake Up from Deep Sleep
ESP32 Timer Wake Up from Deep Sleep
Writing a Deep Sleep Sketch
To write a sketch to put your ESP32 into deep sleep mode, and then wake it up, you need to keep in mind that:
First, you need to configure the wake-up sources. This means configuring what will wake up the ESP32. You can use one or combine more than one wake-up source. In this article, we’ll show you how to use the external wake-up sources.
You can decide what peripherals to shut down or keep on during deep sleep. However, by default, the ESP32 automatically powers down the peripherals that are not needed with the wake-up source you define.
Finally, you use the
esp_deep_sleep_start()
function to put your ESP32 into deep sleep mode.
External Wake Up
External wake up is one of the ways to wake up the ESP32 from deep sleep. This means that you can wake up the ESP32 by toggling the value of a signal on a pin, like the press of a button. 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 are 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 the first argument the pin you want to use, in this format
GPIO_NUM_X
, in which
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:
you can only use pins that are
RTC GPIOs
with this wake-up source. Here’s a list of the RTC GPIOs for different ESP32 chip models:
ESP32-S3
: 0-21;
ESP32
: 0, 2, 4, 12-15, 25-27, 32-39;
ESP32-S2
: 0-21;
This wake-up source allows you to use multiple RTC GPIOs. This wake-up source is implemented by 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:
A bitmask of the GPIO numbers that will cause the wake up;
Mode
: the logic to wake up the ESP32. It can be:
ESP_EXT1_WAKEUP_ALL_LOW
: wake up when all GPIOs go low;
ESP_EXT1_WAKEUP_ANY_HIGH
: wake up if any of the GPIOs go high.
If you’re using an ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2, these are the available modes:
ESP_EXT1_WAKEUP_ANY_LOW
: wake up when any of the selected GPIOs is low
ESP_EXT1_WAKEUP_ANY_HIGH
: wake up when any of the selected GPIOs is high
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
Parts Required
To follow this tutorial, you need the following parts:
ESP32 DOIT DEVKIT V1 Board
(read Best
ESP32 development boards
)
2x Pushbuttons
2x 10k Ohm resistor
Breadboard
Jumper wires
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Code – External Wake Up Example
To program the ESP32 we’ll use Arduino IDE. So, you need to make sure you have the ESP32 Arduino core installed. Follow the next tutorial to install the ESP32 add-on, if you haven’t already:
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
The following example demonstrates how to use the external wake-up source. You can find it at
File
>
Examples
>
ESP32
Deep Sleep
>
ExternalWakeUp
, or you can copy it from below.
/*
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 <
[email protected]
>
*/
#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
Technologies used
Hardware components
- ESP32
- Arduino
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.