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:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
Table of Contents
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
- Minimize Power Usage: Disable any peripherals or sensors before entering deep sleep.
- 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.
- Optimize Wake-Up Task: Keep tasks performed after wake-up as short as possible to conserve energy.
Read more:
- Play chess with a microcontroller
- Remote control with the security of user verification using NodeMCU v3
- Building an IoT Weather Station with NodeMCU v3.0 and DHT11 Sensor
- WiFi Controlled Scheduled Relay Using NodeMCU with Timed On/Off
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:
0 Comments