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 use Arduino IDE to program the ESP32 and cover how to use ext0 and ext1 methods.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/04/ESP32-deep-sleep-external-wake-up.jpg?resize=828%2C466&quality=100&strip=all&ssl=1)
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 configure 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 source.
- 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 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.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/external-wake-up.png?resize=263%2C188&quality=100&strip=all&ssl=1)
External Wake Up (ext0)
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.
External Wake Up (ext1)
This wake up source allows you to use multiple RTC GPIOs. You can use two different logic functions:
- Wake up the ESP32 if any of the pins you’ve selected are high;
- Wake up the ESP32 if all the pins you’ve selected are low.
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(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.
Note: with this wake up source, you can only use pins that are RTC GPIOs.
The following figure shows the ESP32 pinout and its RTC GPIOs highlighted in light orange color (for the ESP32 V1 DOIT board). You can also take a look at our ESP32 GPIO reference guide.
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
To program the ESP32 we’ll use Arduino IDE. So, you need to make sure you have the ESP32 add-on installed. Follow the next tutorials to install the ESP32 add-on, if you haven’t already:
To show you how to use the external (ext0) wake up source, we’ll 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 <[email protected]>
*/
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
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 doesnt need
peripherals to be powered on.
Note that using internal pullups/pulldowns also requires
RTC peripherals to be turned on.
*/
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
//If you were to use ext1, you would use it like
//esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
//Go to sleep now
Serial.println("Going to sleep now");
delay(1000);
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.
Save Data on RTC Memories
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;
Wake Up Reason
Then, the code defines the print_wakeup_reason() function, that prints the reason by which the 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;
}
}
setup()
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.
ext0
In this example, the ESP32 wakes up when the GPIO 33 is triggered to high:
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
Instead of GPIO 33, you can use any other RTC GPIO pin.
Schematic
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.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/external_wake_up_button_bb.png?resize=828%2C423&quality=100&strip=all&ssl=1)
(This schematic uses the ESP32 DEVKIT V1 module version with 30 GPIOs – if you’re using another model, please check the pinout for the board you’re using.)
Note: only RTC GPIOs can be used as a wake up source. Instead of GPIO 33, you can use any RTC GPIO pins to connect your button.
Testing the Example
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.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/serial-monitor.png?resize=745%2C109&quality=100&strip=all&ssl=1)
Press the pushbutton to wake up the ESP32.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/external_wakeup_button.jpg?resize=614%2C394&quality=100&strip=all&ssl=1)
Try this several times, and see the boot count increasing in each button press.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/external-wake-up-serial-monitor_f.png?resize=598%2C373&quality=100&strip=all&ssl=1)
Using this method is useful to wake up your ESP32 using a pushbutton, for example, to make a certain task. However, with this method you can only use one GPIO as wake up source.
What if you want to have different buttons, all of them wake up the ESP, but do different tasks? For that you need to use the ext1 method.
ext1
The ext1 allows you to wake up the ESP using different buttons and perform different tasks depending on the button you pressed.
Instead of using the esp_sleep_enable_ext0_wakeup() function, you use the esp_sleep_enable_ext1_wakeup() function. In the code, that function is commented:
//If you were to use ext1, you would use it like
//esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
Uncomment that function so that you have:
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
The first argument of the 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.
In this example we’re using the variable BUTTON_PIN_BITMASK, that was defined at the beginning of the code:
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
This is only defining one pin as a wake up source, GPIO 33. You need to modify the bitmask to configure more GPIOs as a wake up source.
GPIOs Bitmask
To get the GPIOs bitmask, follow the next steps:
- Calculate 2^(GPIO_NUMBER). Save the result in decimal;
- Go to rapidtables.com/convert/number/decimal-to-hex.html and convert the decimal number to hex;
- Replace the hex number you’ve obtained in the BUTTON_PIN_BITMASK variable.
Mask for a single GPIO
For you to understand how to get the GPIOs bitmask, let’s go through an example. In the code from the library, the button is connected to GPIO 33. To get the mask for GPIO 33:
1. Calculate 2^33. You should get 8589934592;
2. Convert that number (8589934592) to hexadecimal. You can go to this converter to do that:
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/decimal_to_hex.png?resize=488%2C453&quality=100&strip=all&ssl=1)
3. Copy the Hex number to the BUTTON_PIN_BITMASK variable, and you should get:
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
Mask for several GPIOs
If you want to use GPIO 2 and GPIO 15 as a wake up source, you should do the following:
- Calculate 2^2 + 2^15. You should get 32772
- Convert that number to hex. You should get: 8004
- Replace that number in the BUTTON_PIN_BITMASK as follows:
#define BUTTON_PIN_BITMASK 0x8004
Identifying the GPIO used as a wake up source
When you use several pins to wake up the ESP32, it is useful to know which pin caused the wake up. For that, you can use the following function:
esp_sleep_get_ext1_wakeup_status()
This function returns a number of base 2, with the GPIO number as an exponent: 2^(GPIO). So, to get the GPIO in decimal, you need to do the following calculation:
GPIO = log(RETURNED_VALUE)/log(2)
External Wake Up – Multiple GPIOs
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.
Schematic
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.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/external_wake_up_2buttons_bb.png?resize=828%2C504&quality=100&strip=all&ssl=1)
Code Multiple GPIOs – External Wake Up
You need to make some modifications to the example code we’ve used before:
- create a bitmask to use GPIO 15 and GPIO 2. We’ve shown you how to do this before;
- enable ext1 as a wake up source;
- use the esp_sleep_get_ext1_wakeup_status() function to get the GPIO that triggered wake up.
The next sketch has all those changes implemented.
/*
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]>
*/
#define BUTTON_PIN_BITMASK 0x8004 // GPIOs 2 and 15
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;
}
}
/*
Method to print the GPIO that triggered the wakeup
*/
void print_GPIO_wake_up(){
uint64_t 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);
}
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();
//Print the GPIO used to wake up
print_GPIO_wake_up();
/*
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 doesnt need
peripherals to be powered on.
Note that using internal pullups/pulldowns also requires
RTC peripherals to be turned on.
*/
//esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_15,1); //1 = High, 0 = Low
//If you were to use ext1, you would use it like
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
//Go to sleep now
Serial.println("Going to sleep now");
delay(1000);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop(){
//This is not going to be called
}
You define the GPIOs mask at the beginning of the code:
#define BUTTON_PIN_BITMASK 0x8004 // GPIOs 2 and 15
You create a function to print 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);
}
And finally, you enable ext1 as a wake up source:
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
Testing the Sketch
Having two buttons connected to GPIO 2 and GPIO 15, you can upload the code provided to your ESP32. Make sure you have the right board and COM port selected.
The ESP32 is in deep sleep mode now. You can wake it up by pressing the pushbuttons.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/DSC01884.jpg?resize=700%2C364&quality=100&strip=all&ssl=1)
Open the Serial Monitor at a baud rate of 115200. Press the pushbuttons to wake up the ESP32.
You should get something similar on the serial monitor.
![](https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/external-wake-up-serial-monitor_2.png?resize=757%2C374&quality=100&strip=all&ssl=1)
Wrapping Up
The ESP32 can be awaken from sleep using a timer, the touch pins, or an external wake up. In this article we’ve shown you how to wake up the ESP32 using an external wake up. This means that you can wake up the ESP32 when the value of a GPIO changes (from HIGH to LOW, or LOW to HIGH).
To learn more about deep sleep with the ESP32, take a look at our complete guide: ESP32 Deep Sleep with Arduino IDE and Wake Up Sources.
If you like ESP32, you may also like the following resources:
When waking from deep sleep, where does the program begin execution within the loop or does it go through all the initialization and setup again?
Thanks
Hi Erik.
The code goes through all the initialization and setup again. Like if you had restarted the ESP32.
Regards,
Sara
there is a bug in:
int GPIO_reason = esp_sleep_get_ext1_wakeup_status();
you must use:
uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
otherwise GPIO port numbers higher than 31 will not be
printed correctly
‘ESP_EXT1_WAKEUP_ANY_LOW’ was not declared in this scope
Is wake up by any LOW possible?
Hi.
Unfortunately not.
These are the only options:
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.
Regards,
Sara
Hi many thanks for this tutorial. Would it be possible to use a vibration sensor such as the sw-420 to wake the esp32 from sleep?
Hi Tim.
Yes, that sensor outputs a signal when vibration occurs. So, yes.
Regards,
Sara
Can this be placed in loop?
Hi.
Yes. You can place it in the loop().
Once it finds the instructions to go to sleep, the loop() is interrupted.
When the ESP32 wakes up, it will run the code from the start.
Regards,
Sara
A few remarks:
Calculating the mask can be much easier :
define WAKEBUTTON 27 // button connected to GPIO27
define BUTTON_PIN_BITMASK (1<<WAKEBUTTON)
If ESP_EXT1_WAKEUP_ALL_LOW is used no information is available on what buttons are pressed when esp_sleep_get_ext1_wakeup_status() is called.
Use of an external pull-up or pull-down resistor (e.g. 10k) on the pin prevents random wakeups.
Also, all the conversion from decimal to hex is totally unnecessary. A number is a number, it does not matter which base one uses to display it as long as the proper prefixes are there.
Hello Sara,
I need to restart esp (ESP.restart() ) after External Wake Up (ext0). I know how to do it with Touch wake up but not with external wake up. Do you have any hint on how to execute a function after external wake up?
Thank you in advance.
Said
Hi.
I don’t know if that’s possible with external wake up. Because you would have to put the function at the beginning of your code, which would result in the board constantly restarting.
Regards,
Sara
Hello
First of all, I want to thank you a lot for the great tutorial.
I have implemented this and it is working great.
However, after multiple sleep and wakeups, the device goes to sleep and does not get back up with the trigger. The only way to fix it is by pressing the reset button.
The number of iterations seems to be random, sometimes after 50, and other times less.
Is there any solution for this?
please let me know
Thank you!
Hi,
Thanks so much for this tutorial, it is really quite helpful. I have a question about RTC wakeup though. It seems like ESP_SLEEP_WAKEUP_EXT0 uses a signal over the RTC pin, however I cannot sort out how to use the RTC to wake it up. Ultimately I need to set the RTC, and then put the ESP32 into DeepSleep until a specific time, wake up, run the program, and go back to sleep until the next specific time. The answer might be to set the RTC, calculate a timer for the specific wake time, and use the timer wake up, but that seems unnecessary if it is possible (and more accurate) to use the RTC itself. Any suggestions?
Instead of doing the calculations and conversions for bitmask, why not letting the C compiler do the job?
This uses the left bit shift operator (<<) and the bitwise or operator (|):
1<<n will evaluate to 2^n
Instead of:
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
use:
#define BUTTON_PIN_BITMASK (1<<33)
Instead of:
#define BUTTON_PIN_BITMASK 0x8004
use:
#define BUTTON_PIN_BITMASK (1<<15 | 1<<2)
Is it possible to wake it up with ds3221?
Hi!
There is a bug in section “External Wake Up – Multiple GPIOs”
As “pietro pizzi” noticed, “int” must be replaced with “uint64_t” in line: “int GPIO_reason = esp_sleep_get_ext1_wakeup_status();”
“int” is 32 bit. So if we use pins 32, 33… and higher, we will get result: “GPIO that triggered the wake up: GPIO inf”, where GPIO is “inf” instead of number.
Also, it can be a solution for https://rntlab.com/question/esp32-deep-sleep-with-external-wake-up/
Thanks for great tutorial!
Hi Alex.
Thanks for pointing that out.
We’ll fix the example.
Thanks for taking the time to let us know.
Regards,
Sara
Hi,
I’m having quite a problem with random wake ups that I cannot fix. My script is configured for 3 ext1 triggers on pins 33,13 and 4. All connections are made exactly like on the picture. Pushing buttons wakes up esp32 like it should, showing correct pin numbers, however every now and then (sometimes constantly) console says that there was a wakeup caused by external signal using RTC_CNTL on GPIO inf. Could this be happening because of faulty cable connections, broken breadboard or maybe power supply? I’m using power from pc through usb cable. I’ve tested it on 3 different esp32 modules, 2 different usb cables with different pins and it’s happening on all of them regardless of setup.
Hello, Thank you first for the support you can give me. I need help developing a code with ESP32 that allows me to use two or more pins with the ESP_SLEEP_WAKEUP_EXT0 function, so far it works fine with one, but when trying to do it with another it no longer works. The initial code that works with the GPIO2, if I do change it by GPIO4 also works, but not works together. In both GPIO 2 and 4, I have connected static buttons and they can be in High or LOW by minutes, hours, days, or weeks independallity. Sorry if something it’s wrong, it’s my first topic in this forum. Please see the follwing functional code with GPIO2 Pin:
https://docs.google.com/document/d/158TqtkX-o9dMnx2a2PK4MNxyE71c9w4-vAD6SS8frbI/edit?usp=sharing
Hi Freddy.
To use more than one pin, you need to use the ESP_SLEEP_WAKEUP_EXT1 function.
Scroll down the article to the “ext1” heading. Everything is explained of how to use multiple GPIOs.
Regards,
Sara
Hello Sara, and thanks for your prompt answer. I also thought of the EXT1, and I tried with this function, but but the problem is that I need to work with static switches, and they can be in Low or High State each one anytime and for a long time ie: Can be one minute, one hour, or until one month in the same position (low or HigH), and this EXT1 not work because all time are waking up, EXT1 it’s good to work for pulsed switches.
Can two different Interrupt methods, i.e. Timer & EXT(0) , be combined in one program to wake the esp32 up if either condition is met
Is this terminal output wrong or no?
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
Boot number: 7
Wakeup caused by external signal using RTC_IO
Going to sleep now
It seems ok to me.
Regards,
Sara
In ext1 mode, after pressing the button the pin goes to HIGH. how to make the pin go back to LOW?
(Turn on the led, turn off the led. On the same button)
Thanks….
Hi, you can disable the sleep mode with an interrupt either on the falling or rising edge
It is possible activate or deactivate the sleep mode from Telegram ?
You can activate from Telegram by calling a sleep function upon reeiving a message.
However, when the ESP32 is sleeping, it won’t be able to receive any messages from Telegram.
Regards,
Sara
Hello!
is it possible to use the wake up with timer and wake up with pin at the same time?
Hi Stefano
Yes, on CodeProjec site there is a my article:
codeproject.com/Articles/5300124/Power-Reduction-and-Wake-Up-Techniques-on-ESP32-Bo
Where you can download the .ino project.
You cam also get it, with other Arduino/Esp32 .ino(s) projects from my site:
condorinformatique.com
Regards
Hi,
I suppose that the GPIO used for the wakeup must be declare as input somewhere. It is not in the main code so I suppose it is in the function:
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1);
Does it mean that we could use this GPIO as output during the life of the program and we have to call the esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); just before call the deepsleep ?
Could it work like this ?
Thanks,
Laurent
Yes. That’s right.
Regards,
Sara
In the article we read: “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.”
With the same logic, why RTC Peripherals are not automatically kept on in ext1 wake up source option as well?
(I remind what is mentioned in the article about the ext1 wake up source: “with this wake up source, you can only use pins that are RTC GPIOs.” namely exactly what is mentioned about ext0)
Hi,
Great article but leaves out using analog ADC inputs to wake, specifically hal and voltage detect.
Can you do an article covering using Input Signals Measured Using the ADC Instruction as outlined on page 659-660 of the esp32 technical reference manual?
my application is automotive so sleep below 13.5v and wake above 14v, I am using pin35 for voltage sense already as indicated in Table 304. the datasheet is incomprehensible to me still though. I’m sure battery powered applications would like to be able to wake and transmit a low battery warning before the battery dies among other ADC wake usages.
Thanks for your consideration!
Hi,
I m trying to use deep sleep while preserving a string in RTC memory but heavily failing as all the data from RTC memory dissapear upon waking up. At the end, I stuck with the example for Deep sleep for ESP32 (in my case Xiao ESP32-C£ and S3 modules, several different units), getting the next output on serial terminal. As you can notice, the bootcount variable does not increment and I am getting a 0 as a reboot reason, which is not fully clear:
14:27:26.849 -> ESP-ROM:esp32c3-api1-20210207
14:27:26.849 -> Build:Feb 7 2021
14:27:26.849 -> rst:0x15 (USB_UART_CHIP_RESET),boot:0x8 (SPI_FAST_FLASH_BOOT)
14:27:26.849 -> Saved PC:0x4200e85e
14:27:26.849 -> SPIWP:0xee
14:27:26.849 -> mode:DIO, clock div:1
14:27:26.849 -> load:0x3fcd5810,len:0x438
14:27:26.849 -> load:0x403cc710,len:0x918
14:27:26.849 -> load:0x403ce710,len:0x25f4
14:27:26.849 -> entry 0x403cc710
14:27:33.929 -> Boot number: 1
14:27:33.929 -> Wakeup was not caused by deep sleep: 0
14:27:33.929 -> Setup ESP32 to sleep for every 1 Seconds
14:27:33.929 -> Going to sleep now
14:27:46.373 -> ESP-ROM:esp32c3-api1-20210207
14:27:46.373 -> Build:Feb 7 2021
14:27:46.373 -> rst:0x15 (USB_UART_CHIP_RESET),boot:0x8 (SPI_FAST_FLASH_BOOT)
14:27:46.373 -> Saved PC:0x4200e85e
14:27:46.374 -> SPIWP:0xee
14:27:46.374 -> mode:DIO, clock div:1
14:27:46.374 -> load:0x3fcd5810,len:0x438
14:27:46.374 -> load:0x403cc710,len:0x918
14:27:46.374 -> load:0x403ce710,len:0x25f4
14:27:46.415 -> entry 0x403cc710
14:27:53.481 -> Boot number: 1
14:27:53.481 -> Wakeup was not caused by deep sleep: 0
14:27:53.481 -> Setup ESP32 to sleep for every 1 Seconds
14:27:53.481 -> Going to sleep now…
Any suggestion?
Hi.
What is the example that you’re following?
Regards,
Sara
Hi Sara,
and thank you for replying.
The example I am trying to run is
…Arduino15\packages\esp32\hardware\esp32\2.0.10\libraries\ESP32\examples\DeepSleep\TimerWakeUp
So TimeWakeUp.ino for ESP32.
So, my friend and me have tried this code to run on beforementioned XIO products but both failed in the same way as the Boot number does not increase with the time.
I have just downloaded good ol’ trial version of Hypeterminal and immediately got the right results – The boot counter is regularly incrementing. As soon as I use the Arduino IDE serial monitor something else is causing this unregular waking up, thus clearing the counter. Main concern here is clearing the value stored in RTC memory which I would use in my project to retain relevant data during deep sleep.
Now I am confused…
Hi.
That’s weird. I just tested the code on my ESP32 boards and everything is working as expected.
Do you have any peripherals connected to your board?
What is the model that you’re using?
Regards,
Sara
These two, without having anything connected.
https://files.seeedstudio.com/wiki/SeeedStudio-XIAO-ESP32S3/img/4.jpg
https://files.seeedstudio.com/wiki/XIAO_WiFi/board-pic.png
Yesterday I have left one of these for some time on the Hyperterminal and there also a strange behaviour after some time. 2 different laptops, 3 different boards and the same behaviour…
Hi again.
Unfortunately, I’m not familiar with those models.
Maybe it’s something related to the hardware of that specific model…
Regards,
Sara
Thank you for the tutorial, it was very helpful. I was able to set up an on/off system with a long press of a push button thanks to it. Well, I’m not sure if it’s entirely streamlined, but it works.
Hi
When I power a LIVE D1 Mini ESP32 through the USB either from PC or USB-PSU, I see that the deep sleep works and I see bootCount increasing and wakeup reason is 4 (timer).
bootCount,wakeupreason
1,0
2,4
3,4
4,4
…
However if I power the ESP32 card VCC pin directly from a 4.2 LiOn cell everything works and the card wakes up by timer as requested BUT bootCount does not increment and wakeup reason is 0
bootCount,wakeupreason
1,0
1,0
1,0
1,0
…
What is the reason for this ? I would like to run the card on the battery…
The text description for External Wake Up (ext0) states “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”.
while …
The text description for External Wake Up (ext1) states “This wake up source is implemented by the RTC controller. So, RTC peripherals and RTC memories can be powered off in this mode”.
Yet the example code for BOTH modes uses RTC memory to save the boot count:
RTC_DATA_ATTR int bootCount = 0;
That tells me that RTC memory is powered on in BOTH modes. What am I missing?
Hi!
Can I use another esp32 for example and trigger the wakeup by using pins of the other esp32.
For example, esp32-A will have a GPIO output pin connected to esp32-B and will serves as input pin. and I will just put output pin to HIGH, so that the other esp32 will wake up.
Hi.
Yes.
That should work.
Regards,
Sara