Using Deep Sleep Mode in ESP32 for Low Power Applications

Introduction
The ESP32 microcontroller is a powerful and versatile device, capable of a wide range of tasks. However, for battery-powered or low-power applications, it's crucial to optimize power consumption. One effective way to achieve this is by utilizing the deep sleep mode provided by the ESP32. In this blog post, we'll explore how to use deep sleep mode to significantly reduce power consumption.

What is Deep Sleep Mode?
Deep sleep mode is a power-saving state that allows the ESP32 to shut down most of its internal circuitry while keeping enough functionality to wake up the device when needed. By using deep sleep mode, you can extend the battery life of your ESP32 projects by orders of magnitude.

Code Implementation
Let's dive into the code that demonstrates how to put the ESP32 into deep sleep mode for a specified amount of time. We'll set it to sleep for 5 seconds and then wake up.


#define TIME_TO_SLEEP  5 // Define the time in seconds for ESP32 to sleep

void setup() {
    Serial.begin(115200); // Start Serial communication
    delay(1000); // Wait for a moment to ensure Serial communication is ready
    print_wakeup_reason(); // Print the reason for waking up from sleep

    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000); // Enable timer wakeup for deep sleep, setting it to the specified time
    Serial.println("ESP32 will go to sleep and wakeup after " + String(TIME_TO_SLEEP) + " seconds"); // Print a message indicating when the ESP32 will go to sleep and for how long
    delay(1000); // Wait for a moment to ensure the message is sent before sleep
    Serial.flush(); // Flush the Serial buffer to ensure all data is sent
    esp_deep_sleep_start(); // Start deep sleep mode
    Serial.println("This will never be printed"); // This line will never be reached because the ESP32 is in deep sleep
}

void loop() {
    // This is not going to be called
}

void print_wakeup_reason(){
    esp_sleep_wakeup_cause_t wakeup_reason; // Variable to hold the wakeup reason
    wakeup_reason = esp_sleep_get_wakeup_cause(); // Get the wakeup reason
    switch(wakeup_reason) { // Print the reason based on the 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("Device boot"); break;
    }
}

Explanation

  1. Setting the Sleep Time: The TIME_TO_SLEEP constant is defined to set the duration of deep sleep in seconds. In this example, it's set to 5 seconds.
  2. Enabling Timer Wakeup: esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000) enables the timer wakeup for deep sleep, setting it to the specified time in microseconds.
  3. Entering Deep Sleep: esp_deep_sleep_start() puts the ESP32 into deep sleep mode for the specified time.

Wakeup Reasons
The print_wakeup_reason() function prints the reason for waking up from sleep. The ESP32 provides various wakeup reasons, such as external signals, timers, touchpad, etc.

Running the Code

  1. Upload the code to your ESP32.
  2. Open the Serial Monitor at a baud rate of 115200.
  3. You'll see the initial boot message, followed by the ESP32 going to sleep.
  4. After 5 seconds, the ESP32 will wake up and print the wakeup reason.
  5. The process repeats.

Conclusion
Using deep sleep mode in your ESP32 projects can significantly reduce power consumption, making it ideal for battery-powered applications where power efficiency is crucial. Experiment with different sleep times to find the optimal balance between functionality and power saving.

Feel free to modify the TIME_TO_SLEEP constant to adjust the sleep duration according to your project's requirements.

This is just the beginning of what you can achieve with the ESP32's deep sleep mode. Explore further, combine it with sensors, actuators, and networking capabilities to create efficient and long-lasting IoT devices.

Happy tinkering!


ADVERTISEMENT

Previous

ESP32 Light Sleep



Explore these related topics for more information



ADVERTISEMENT
ADVERTISEMENT


ADVERTISEMENT