The ESP32 is a powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities, making it an ideal choice for IoT projects. However, to maximize battery life, it’s crucial to utilize the ESP32’s deep sleep mode. This article explains how to configure the ESP32 to enter deep sleep mode, complete with an example code to help you get started.

Disclaimer:

Advertisements

What is Deep Sleep Mode?

Deep sleep mode is one of the low-power modes available on the ESP32. In this mode, most of the ESP32’s components are powered down, drastically reducing power consumption. The ESP32 can wake up from deep sleep via an external trigger (e.g., a button press) or a timer.

Benefits of Deep Sleep Mode

  • Extends battery life by significantly reducing power consumption when the device is idle.
  • Ideal for IoT projects where the device only needs to wake up occasionally to perform tasks like data collection or communication.

Configuring Deep Sleep Mode

1. Setting Up the Environment

Ensure you have the ESP32 development environment set up with Arduino IDE or another compatible platform.

2. Basic Deep Sleep Example

Here’s a simple example to put the ESP32 into deep sleep mode and wake it up after 10 seconds using the timer:

#include "esp_sleep.h"

#define uS_TO_S_FACTOR 1000000  // Conversion factor for microseconds to seconds
#define TIME_TO_SLEEP  10       // Time ESP32 will go to sleep (in seconds)

void setup() {
  Serial.begin(115200);
  delay(1000); // Give some time for the serial monitor to connect

  // Print a message before going to sleep
  Serial.println("Going to sleep now");

  // Configure deep sleep mode and set wakeup time
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  esp_deep_sleep_start();

  // This line will never be executed because the ESP32 will be in deep sleep mode
  Serial.println("This won't be printed");
}

void loop() {
  // Empty loop as the device will not reach this point
}

3. Explanation of the Code

  • esp_sleep_enable_timer_wakeup(): Configures the ESP32 to wake up after a set period. Here, the wake-up time is set to 10 seconds.
  • esp_deep_sleep_start(): Puts the ESP32 into deep sleep mode. Once in this mode, the ESP32 will stop executing the code and will only wake up when the timer expires.

4. Waking Up from Deep Sleep

When the ESP32 wakes up, it starts execution from the setup() function, similar to a reset. You can check if the wake-up was from deep sleep using the following code:

esp_sleep_wakeup_cause_t wakeup_reason;

void setup() {
  Serial.begin(115200);
  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Wakeup caused by timer"); break;
    default: Serial.println("Wakeup was not caused by timer"); break;
  }

  // Other setup code...

  // Put the ESP32 back to sleep
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  esp_deep_sleep_start();
}

void loop() {
  // Empty loop
}

This code checks the reason for waking up and prints it to the serial monitor.

Additional Tips for Using Deep Sleep

  1. Minimize Power Usage: Disable any peripherals or sensors before entering deep sleep.
  2. Use Wake-up Sources Judiciously: Apart from the timer, you can use external GPIO pins or touch sensors as wake-up sources, depending on your project requirements.
  3. Optimize Wake-Up Task: Keep tasks performed after wake-up as short as possible to conserve energy.

Read more:

Conclusion

Using deep sleep mode effectively can extend the battery life of your ESP32-based IoT projects. By configuring the device to wake up only when necessary, you can create energy-efficient systems that can run for extended periods without requiring frequent battery replacements.

With the example code provided, you should be able to implement deep sleep in your projects and start reaping the benefits of lower power consumption.

For Professional Designs or Help:

Loading
Categories: ElectronicsEmbedded ProductsESP32IoT Projects

MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

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