Using the Arduino IDE, we’ll learn about the ESP32 deep sleep mode and wake up sources in this article. We’ll look at how to put the ESP32 development board into deep sleep mode in this complete guide. We’ll also go through several wake-up sources that ESP32 can use to transition from sleep to normal execution mode, such as timers, external events, touch pins, and so on.

OPERATING MODES IN ESP32

  • Active mode
  • Modem Sleep mode
  • Light Sleep mode
  • Deep Sleep mode
  • Hibernation mode

DEEP SLEEP MODE

Deep sleep mode is implemented in ESP32 to reduce power consumption and extend battery life. The module is generally quite power hungry, drawing up to 75mA in regular consumption or inactive mode.However, if WiFi is also used, the current usage can reach 240mA. Thus, if your ESP boards are powered by batteries, it is inconvenient to utilise ESP32 in active mode when we only want to do certain events after a certain amount of time has passed.

We can reduce power usage by putting the module into deep sleep mode. When we successfully trigger deep sleep in the ESP32, the current consumption will be in the micro Amperes (A) region, and the battery life will be extended.

The RTC memory on the ESP32 remains switched on while it is in deep sleep mode, allowing us to write a programme for the ULP co-processor and put it in the RTC memory, allowing us to access peripheral devices, internal timers, and internal sensors.

WAKE UP SOURCES

After putting the ESP32 into deep sleep mode, there are several ways to wake it up:

  1. You can use the timer, waking up your ESP32 using predefined periods of time;
  2. You can use the touch pins;
  3. You can use two possibilities of external wake up: you can use either one external wake up, or several different external wake ups;

 

WAKE UP SOURCES:TIMER WAKE UP

We can put the ESP32 into sleep mode and wake it up with timer wake up. A timer is embedded into the real-time controller (RTC). We must first set a timer for a specific amount of time, after which the chip will be awakened from its deep sleep mode by the built-in timer. This is accomplished by the use of a function. For a set number of milliseconds, we’ll put the board into deep sleep mode.

Enable Timer Wake Up

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)

//CODE

#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,
  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
}

HOW CODE WORKS

When you use timer wake up, the parts that will be powered on are RTC controller, RTC peripherals, and RTC memories.

Define the Sleep Time

These first two lines of code define the period of time the ESP32 will be sleeping.

#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) */

This example uses a conversion factor to convert microseconds to seconds, allowing you to set the sleep time in seconds in the TIME_TO_SLEEP variable. The example will send the ESP32 into deep sleep mode for 5 seconds in this scenario.

Save Data on RTC Memories

You can save data on the RTC memory with the ESP32. The RTC component of the ESP32 features 8kB SRAM, which is referred to as RTC fast memory. During deep sleep, the data kept here is not erased. When you touch the reset button, though, it is erased (the button labelled EN on the ESP32 board).

Simply add RTC_DATA_ATTR before a variable definition to save data on the RTC memory. The bootCount variable is saved on the RTC memory in this example. This variable will keep track of the number of times the ESP32 has awoken from a deep slumber.

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 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;

  }

}

The setup()

In the setup() is where you should put your code. In deep sleep, the sketch never reaches the loop() statement. So, you need to write all the sketches in the setup().

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));

The code then uses the print_wakeup_reason() method, but you can use any function to accomplish your goal. You could want to wake up your ESP32 once a day, for example, 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 an 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 esp goes to sleep by calling the following function:

esp_deep_sleep_start()

The loop()

The loop() section is empty because the ESP32 will go to sleep before reaching this part of the code. So, you need to write all your sketches in the setup().

Upload the example sketch to your ESP32. Make sure you have the right board and COM port selected.

TESTING THE TIMER WAKE UP

Open the Serial Monitor at a baud rate of 115200. Every 5 seconds, the ESP wakes up, prints a message on the serial monitor, and goes to deep sleep again.

Every time the ESP wakes up the bootCount variable increases. It also prints the wake up reason if you press the EN button on the ESP32 board, it resets the boot count to 1 again.

We 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.

TOUCH WAKE UP

We can wake up the ESP32 from deep sleep using the touch pins.

Enabling the ESP32 to wake up using a touchpin is simple. In the Arduino IDE, We need to use the following function:

esp_sleep_enable_touchpad_wakeup()

CODE

Open your Arduino IDE, and go to File > Examples > ESP32 > Deep Sleep, and open the TouchWakeUp sketch.

#define Threshold 40 /* Greater the value, more the sensitivity */

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();

  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;
  }
}

void callback(){
  //placeholder callback function
}

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();

  //Setup interrupt on Touch Pad 3 (GPIO15)
  touchAttachInterrupt(T3, callback, Threshold);

  //Configure Touchpad as wakeup source
  esp_sleep_enable_touchpad_wakeup();

  //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
}

HOW CODE WORKS

Setting the Threshold

The first thing you need to do is setting a threshold value for the touch pins. In this case we’re setting the Threshold to 40. 

#define Threshold 40

The value read by the sensor lowers when you touch a touch-sensitive GPIO. As a result, when contact is detected, you can set a threshold value that causes something to happen.The ESP32 should wake up when the value read by the touch-sensitive GPIO is less than 40, according to the threshold value defined above. You can change that value to achieve the desired level of sensitivity.

Attach Interrupts

Interrupts must be connected to the touch sensitive pins. A callback function is called when touch is detected on a specific GPIO. For example, take a look at the following line:

//Setup interrupt on Touch Pad 3 (GPIO15)

touchAttachInterrupt(T3, callback, Threshold);

The ESP32 wakes up and executes the callback function when the value read on T3 (GPIO 15) is less than the value set on the Threshold variable..

The callback() function will only be executed if the ESP32 is awake.

  • If the ESP32 is asleep and you touch T3, the ESP will wake up – the callback() function won’t be executed if you just press and release the touch pin;
  • If the ESP32 is awake and you touch T3, the callback function will be executed. So, if you want to execute the callback() function when you wake up the ESP32, you need to hold the touch on that pin for a while, until the function is executed.In this case the callback() function is empty.

void callback(){

//placeholder callback function

SCHEMATIC

To test this example, wire a cable to  GPIO 15, as shown in the schematic below.

TESTING THE EXAMPLE

Upload the code to your ESP32, and open the Serial Monitor at a baud rate of 115200. The ESP32 goes into deep sleep mode. 

When you touch the pin, the ESP32 displays on the Serial 

Monitor: the boot number, the wake up cause, and in which GPIO touch was detected.

EXTERNAL WAKEUP

There are two types of external triggers to wake ESP32 up from deep sleep.

  • ext0 – Use it when you want to wake-up the chip by one particular pin only.
  • ext1 – Use it when you have several buttons for the wake-up.

External Wake-up Source (ext0)

When one of the pins on the RTC controller is set to a predetermined logic level, the controller will wake up. RTC GPIOs 0,2,4,12-15,25-27,32-39 can be used as that pin.To enable this wake-up source, use the esp sleep enable ext0 wakeup (GPIO PIN,LOGIC LEVEL) function. There are two parameters to the function. The first is the pin number to which the button is linked, and the second is whether we want to wake up by a LOW or HIGH state of the pin.

RTC peripherals will be kept switched on during deep sleep if this wake-up source is requested because ext0 uses RTC IO to wake up.

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.

SOURCE CODE

Let’s explore the example that comes with the ESP32 library. Go to File > Examples > ESP32 > Deep Sleep > ExternalWakeUp:

//CODE

#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
}

HOW CODE WORKS

The code example demonstrates how to use both ext0 and ext1 methods. This code is fairly similar to the ones that came before it in this article. The serial communication is first initialised in setup():

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 put this example to the test, connect a pushbutton to your ESP32 using the schematic below. A pull down 10K Ohm resistor is used to link the button to GPIO 33.

TESTING THE 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.

esp32 deep sleep

Try this several times, and see the boot count increasing in each button press.

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:

  1. Calculate 2^(GPIO_NUMBER). Save the result in decimal;
  2. Go to rapidtables.com/convert/number/decimal-to-hex.html and convert the decimal number to hex;
  3. Replace the hex number you’ve obtained in the BUTTON_PIN_BITMASK variable.

MASK FOR SINGLE GPIO

Let’s look at an example to see how to retrieve the bitmask for the GPIOs. The button is wired to GPIO 33 in the library’s code. To obtain the mask for GPIO 33, follow these steps:

1. Calculate 2^33. You should get 8589934592;

2. Convert that number (8589934592) to hexadecimal

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 GPIO

If you want to use GPIO 2 and GPIO 15 as a wake up source, you should do the following:

  1. Calculate 2^2 + 2^15. You should get 32772
  2. Convert that number to hex. You should get: 8004
  3. Replace that number in the BUTTON_PIN_BITMASK as follows:

#define BUTTON_PIN_BITMASK 0x8004

#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 WAKEUP-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  

esp32 connected with resistor
esp32 connected with resistor

 

Follow the steps for some modifications

  • 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.

CODE

#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
}

 

HOW CODE WORKS

#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 

You can upload the code provided to your ESP32 by connecting two buttons to GPIO 2 and GPIO 15. Make sure you’re using the correct board and COM port.

The ESP32 is in deep sleep mode now. 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.

 ESP32 deep sleep
TESTING 

CONCLUSION

We’ve taught you how to employ deep sleep with the ESP32 and how to wake it up in this article. A timer, touch pins, or a change in GPIO status can all be used to wake up the ESP32.

author avatar
Amith G Nair
Experience as a product developer, innovation coach, and electronics lecturer,a seasoned professional driven by passion for designing projects.expertise extends to 3D modelling, hardware designing, and web development using HTML, WordPress, and Django.