Index
Introduction
When your IoT project is connected to a wall adapter, power consumption may not be a major concern. However, if you plan to use a battery as the power source, minimizing power usage becomes crucial.
The ESP32 can consume a significant amount of power depending on its operational state. Under normal conditions, it typically draws around 75mA, but this can increase to approximately 240mA when transmitting data over WiFi.
To address this issue, one solution is to utilize Deep Sleep Mode on the ESP32.
This guide provides comprehensive instructions for utilizing the ESP32 Deep Sleep mode with the Arduino IDE. It covers the process of placing the ESP32 into deep sleep and explores various wake-up modes, including timer wake-up, touch wake-up, and external wake-up.
It’s worth noting that you can put the ESP32 into deep sleep mode without configuring any wake-up sources. In this scenario, the chip will remain in deep sleep indefinitely until an external reset is applied.
ESP32 Deep Sleep
In deep sleep mode, the ESP32 conserves power by shutting down most of its components. However, certain parts of the chip remain operational to allow it to wake up when necessary. The parts of the ESP32 that remain operational in deep sleep mode are:
RTC Memory:
- A small amount of memory (RTC Fast Memory) that can retain data across deep sleep cycles.
RTC Peripherals:
- These include the Real-Time Clock (RTC) itself, the RTC Timer, and the RTC GPIOs. These peripherals allow the chip to wake up from deep sleep based on timer events or external signals.
Ultra-Low-Power (ULP) Coprocessor:
- The ULP coprocessor can run while the main CPUs are in deep sleep. It can perform simple tasks such as monitoring sensors, and GPIOs, or performing ADC measurements.
Touch Pad Controller:
- The touch pad controller can stay active to wake up the chip based on touch events.
RTC Watchdog Timer:
- It can reset the system if it doesn’t wake up from deep sleep within a specified period.
Wake-Up Sources:
The ESP32 can be woken from deep sleep mode using multiple wake-up sources. These sources include:
- Timer Wake-Up:
- The ESP32 has a built-in RTC (Real-Time Clock) timer that can wake up the device after a specified period. This is commonly used for periodic wake-ups to perform tasks like sensor readings or data transmissions.
- Function:
esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
Example Code:
#include <esp_deep_sleep.h>
// Define the time to sleep in microseconds (here, 10 seconds)
#define SLEEP_TIME_US 10000000
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Configure the timer wake-up
esp_sleep_enable_timer_wakeup(SLEEP_TIME_US);
// Print a message to indicate setup completion
Serial.println("Setup complete. Entering deep sleep mode...");
// Enter deep sleep mode
esp_deep_sleep_start();
}
void loop() {
// This function will never be reached as the ESP32 is in deep sleep mode
}
- Touch Pad Wake-Up:
- The ESP32 can be woken up by touch events on specific touch-sensitive GPIOs. This is useful for touch-based user interfaces.
- Function:
esp_sleep_enable_touchpad_wakeup()
Example Code:
#include <esp_deep_sleep.h>
#include <esp32-hal-touch.h>
// Define the touch pin
#define TOUCH_PIN T0
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Configure the touch wake-up
esp_sleep_enable_touchpad_wakeup();
touchAttachInterrupt(TOUCH_PIN, touch_callback, 40); // Set threshold
// Print a message to indicate setup completion
Serial.println("Setup complete. Entering deep sleep mode...");
// Enter deep sleep mode
esp_deep_sleep_start();
}
void loop() {
// This function will never be reached as the ESP32 is in deep sleep mode
}
// Define the touch callback function
void touch_callback() {
// Print a message when touch is detected
Serial.println("Touch detected!");
}
- External Wake-Up (EXT0):
- The EXT0 wake-up source uses RTC GPIOs to wake up the ESP32 when a specific GPIO pin transitions from a high state to a low state. This is typically used for push-button wake-ups.
- Function:
esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
Example Code:
#include "esp_sleep.h"
// Define the GPIO for the wake-up source
#define WAKEUP_PIN 33
void setup() {
Serial.begin(115200);
delay(1000); // To ensure serial output is complete before sleep
// Configure wake-up source: external pin (EXT0)
esp_sleep_enable_ext0_wakeup(WAKEUP_PIN, 0); // Wake up on low signal
Serial.println("Entering deep sleep. Press the button to wake up...");
esp_deep_sleep_start();
}
void loop() {
// This will not be executed
}
- External Wake-Up (EXT1):
- The EXT1 wake-up source allows the ESP32 to wake up when multiple RTC GPIOs are in a predefined logic level. This is useful for more complex wake-up scenarios involving multiple pins.
- Function:
esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)
Example Code:
#include "esp_sleep.h"
// Define the GPIOs for the wake-up source
#define WAKEUP_PIN_1 GPIO_NUM_32
#define WAKEUP_PIN_2 GPIO_NUM_33
void setup() {
Serial.begin(115200);
delay(1000); // To ensure serial output is complete before sleep
// Configure wake-up source: multiple pins (EXT1)
esp_sleep_enable_ext1_wakeup((1ULL << WAKEUP_PIN_1) | (1ULL << WAKEUP_PIN_2), ESP_EXT1_WAKEUP_ANY_HIGH);
Serial.println("Entering deep sleep. Wake up on any high signal...");
esp_deep_sleep_start();
}
void loop() {
// This will not be executed
}
- GPIO Wake-Up:
- The ESP32 can wake up from deep sleep when a certain GPIO pin level changes. This is configured through the ULP (Ultra-Low Power) coprocessor.
- Function: Typically used in conjunction with ULP programming to monitor GPIO states.
Example Code:
- ULP Wake-Up:
- The Ultra-Low-Power (ULP) coprocessor can wake up the ESP32 from deep sleep after performing simple tasks, such as ADC measurements or monitoring GPIOs.
- Function: Requires custom ULP code to be written and uploaded.
Example Code