Deep Sleep and Wake Up Methods in ESP32

Last Updated on November 15, 2024

If you are developing IoT or battery powered Embedded system application then you need to know about the Sleep mode of microcontrollers and this is the best way to extend the battery life and minimize the power consumption by the core microcontroller in your application. Here we will experiment about Deep Sleep and Wake up Methods in ESP32. As we all know ESP32 Microcontroller from Espressif Systems have all the elements to function as IoT controller, and all purpose low power microcontroller unit with a wide range of features like Wi-Fi, Bluetooth, RTC, ADC/DAC and multiple GPIOs etc..,




Additionally ESP32 have the ability to enter different low power modes. Those who are working in battery powered ES and Remote IoT solutions, know the importance of power management and saving. ESP32 gives cool features like, Sleep, Deep Sleep and Wake up functions. These are crucial features that allows the ESP32 to perform well battery powered sensor based applications, Remote data logging and IoT systems that require low power consumption.

ESP32 Power Modes

Power modes of ESP32 microcontroller

  • Active Mode
  • Modem Sleep
  • Light Sleep
  • Deep Sleep

Refer more about power modes here : Espressif ESP32 Sleep Modes Official Document

Active Mode

Normal operation mode of ESP32 microcontroller is called as Active mode, here all the Internal peripherals and CPU are active.

Modem Sleep

This method, just turn OFF Wi-Fi and Bluetooth blocks in ESP32 but the CPU and other parts remains operational.

Light Sleep

In this mode CPU and peripherals are paused from doing operations but memory and Wi-Fi remain active.

Deep Sleep

This is the lowest power mode of ESP32. In Deep Sleep mode, CPU, Wi-Fi, Bluetooth and other peripherals are turned OFF but only Essential components like Real Time Clock (RTC) and certain Wake UP sources are active. So the ESP32 power consumption reduced to 10µA and making it suitable for battery powered devices & applications.

Here we are going to make experiments with Deep Sleep mode and Wake Up methods.

ESP32 Wake Up Sources

Here is the list of sources and methods to wake up from Deep Sleep.

  1. Timer Wake Up: In this Method RTC Timer block is used to Wake up ESP32 core after a specified duration or delay.
  2. Touch Wake Up: In this Method capacitive touch Sensors detects changes and if that change is above threshold then it sends Wake Up signal to ESP32 core.
  3. GPIO Wake Up: ESP32 gets Wake Up by Detecting signal changes (high or low) on specific GPIO pins.
  4. ULP Coprocessor Wake Up: Utilizes the Ultra-Low Power (ULP) coprocessor to monitor sensors or other conditions.
  5. EXT1 Wake Up: Uses multiple GPIO pins with logic functions to wake up the ESP32.

Each wake up source is suitable for different types of applications, so selecting the right source depends on the use case.

Implementation of Deep Sleep and Wake Up in ESP32

The ESP32 Deep Sleep can be enabled using the esp_sleep_enable_deep_sleep() function. To set up deep sleep, include the ESP32-specific headers and use the esp_sleep.h library.

Timer Wake Up

The Timer Wake Up is one of the simplest wake up method and ideal for applications that require periodic wake ups that is like checking sensor data every few minutes.

Timer Wake Up Code

//Code from theoryCIRCUIT
#include "esp_sleep.h"
#include "esp_system.h"

#define SLEEP_DURATION 10 * 1000000  // 10 seconds in microseconds

void setup() {
    Serial.begin(115200);
    delay(1000);
    Serial.println("Woke up from deep sleep!");
    //Your action code here 
    delay (3000);
    esp_sleep_enable_timer_wakeup(SLEEP_DURATION); // Set wake-up timer
    Serial.println("Entering Deep Sleep for 10 seconds");
    esp_deep_sleep_start();  // Begin Deep Sleep
}

void loop() {
    // This will not be executed as ESP32 goes to sleep
}

Touch Wake Up

ESP32 Microcontroller capacitive touch sensors block can be used as wake up sources and making it useful for user interfaces or touch based events. To set a touch sensor block as a wake up source, we have to configure a threshold value. So test the value of touch event through serial monitor and then choose threshold value. We have tested and the value goes between 8 for touch and 73 for without touch.

Touch Wake Up Code

#include "esp_sleep.h"
#include "driver/touch_pad.h"

void call() {}

void setup() {
    Serial.begin(115200);
    delay(1000);
    Serial.println("Woke up from deep sleep by Touch!");
    //Your action code here 
    delay (3000);
    
    touchAttachInterrupt(T0, call, 40);  // Set threshold for touch pad T0
    esp_sleep_enable_touchpad_wakeup();  // Enable wake-up by touchpad

    Serial.println("Entering Deep Sleep, wake up on touch GPIO4 pin");
    esp_deep_sleep_start();  // Begin Deep Sleep
}

void loop() {
    // Code will not execute in deep sleep mode
}

GPIO Wake Up

In GPIO Pin wake up method, we can program any GPIO pin to respond for either HIGH or LOW level input signal and then Wake up ESP32 from Deep Sleep. This is useful for applications involving external devices or sensors.

GPIO Wake Up Code

#include "esp_sleep.h"

#define WAKE_UP_PIN GPIO_NUM_15

void setup() {
    Serial.begin(115200);
    
    pinMode(WAKE_UP_PIN, INPUT_PULLUP);  // Configure GPIO as input
    esp_sleep_enable_ext0_wakeup(WAKE_UP_PIN, 0);  // Wake up on LOW signal
    Serial.println("Woke up from deep sleep by GPIO 15 pin");
    //Your action code here 
    delay (3000);

    Serial.println("Entering Deep Sleep, wake up on GPIO 15");
    esp_deep_sleep_start();  // Begin Deep Sleep
}

void loop() {
    // Code will not execute in deep sleep mode
}

Combining Multiple Wake Up Sources (EXT1 Wake Up)

ESP32 also supports combining multiple wake up sources. For an example you can set the ESP32 to wake up with either a timer or a touchpad event. Here is the simple example.

Multiple Wake Up Sources (EXT1 Wake Up) Code

//Code from theoryCIRCUIT
#include "esp_sleep.h"
#include "driver/touch_pad.h"

#define WAKE_UP_TIMER 5 * 1000000  // 5 seconds
void call() {}
void setup() {
    Serial.begin(115200);
    delay(500);
    Serial.println("Woke up.....wait 3 seconds");
    //Your action code here 
    delay(3000);
    
    // Set up timer wake-up
    esp_sleep_enable_timer_wakeup(WAKE_UP_TIMER);

    // Set up touchpad wake-up
    touchAttachInterrupt(T0, call, 40); //GPIO4 as touch pin
    esp_sleep_enable_touchpad_wakeup();

    Serial.println("Entering Deep Sleep, wake up on timer or touch");
    esp_deep_sleep_start();  // Begin Deep Sleep
}

void loop() {
    // Code will not execute in deep sleep mode
}

Hope you have understand the ESP32 Deep Sleep and Wake up methods, make an experiment and let us know if you have any doubts, in the comment.




Leave a Reply

Your email address will not be published. Required fields are marked *