Deep Sleep mode in the ESP32 microcontroller is a power-saving feature that significantly reduces power consumption by shutting down most of the chip’s components while retaining the state of essential peripherals. This guide explores Deep Sleep mode, providing example programs and practical applications to help you optimize your ESP32 projects for low power consumption.
Deep Sleep mode reduces the ESP32’s power consumption by turning off most of its components, retaining only the RTC memory and some peripherals. This is crucial for battery-powered devices, extending their operational life significantly.
ESP32 Deep Sleep Mode Overview
The ESP32 can enter Deep Sleep mode using various wake-up sources like timers, external signals, and touchpad interrupts. During Deep Sleep, the RTC (Real-Time Clock) memory retains data, and the chip consumes minimal power.
Using Deep Sleep in ESP32 is straightforward with the Arduino framework. Below is a simple example to put the ESP32 into Deep Sleep and wake it up after a specified time.
Example of ESP32 Deep Sleep
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 10 /* Time ESP32 will go to sleep (in seconds) */
void setup() {
Serial.begin(115200);
Serial.println("Setup ESP32 to sleep for " + String(TIME_TO_SLEEP) + " Seconds");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to sleep now");
delay(1000);
esp_deep_sleep_start();
}
void loop() {
// This will not be reached
}
Explanation:
- The ESP32 is set to wake up after a specified time (10 seconds in this example).
- The
esp_sleep_enable_timer_wakeup
function sets the timer wake-up source. - The
esp_deep_sleep_start
function puts the ESP32 into Deep Sleep mode.
Advanced Deep Sleep Mode Features
The ESP32 supports various wake-up sources and configurations for Deep Sleep mode, including GPIO wake-up, touchpad wake-up, and ULP (Ultra Low Power) co-processor wake-up. These features allow for more complex and efficient power-saving strategies.
Example: GPIO Wake-Up
This example demonstrates how to wake up the ESP32 from Deep Sleep using an external signal on a GPIO pin.
#define BUTTON_PIN GPIO_NUM_0 // Use the GPIO number of the button pin
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
esp_sleep_enable_ext0_wakeup(BUTTON_PIN, 0); // 0 = Low level
Serial.println("Going to sleep now");
delay(1000);
esp_deep_sleep_start();
}
void loop() {
// This will not be reached
}
Explanation:
- The ESP32 is configured to wake up when a button connected to GPIO0 is pressed (pulls the pin low).
- The
esp_sleep_enable_ext0_wakeup
function sets the external GPIO wake-up source.
Practical Applications and Examples
Example: Environmental Monitoring System
This example showcases an environmental monitoring system that collects data from sensors, transmits it, and then enters Deep Sleep mode to save power.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 300 /* Time ESP32 will go to sleep (in seconds) */
#define WIFI_SSID "your_SSID"
#define WIFI_PASS "your_PASSWORD"
Adafruit_BME280 bme; // BME280 sensor
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0F;
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" hPa");
// Transmit data over WiFi here
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to sleep now");
delay(1000);
esp_deep_sleep_start();
}
void loop() {
// This will not be reached
}
Explanation:
- The ESP32 reads data from a BME280 sensor and transmits it over WiFi.
- The
esp_sleep_enable_timer_wakeup
function sets a timer to wake the ESP32 after 5 minutes. - The ESP32 enters Deep Sleep mode to save power.
The ESP32’s Deep Sleep mode offers powerful features to significantly reduce power consumption, making it ideal for battery-powered and energy-efficient applications. By understanding and utilizing Deep Sleep mode, you can extend the operational life of your projects and enhance their performance. For more detailed information, refer to the ESP32 Deep Sleep documentation.