ESP32, a highly integrated low-power microcontroller from Espressif, is known for its robustness and rich functionality. One of the key features of the ESP32 microcontroller is its capability for power saving, which it achieves through different types of sleep modes, including the deep sleep mode.
In this article, we're going to learn how to program a deep sleep mode in ESP32, delve into the various types of sleep modes it has, explore which ESP32 chips support deep sleep, and answer some common questions associated with its usage.
In the deep sleep mode, ESP32 only powers up the RTC (Real-Time Clock) and the ULP (Ultra-Low Power) co-processor in order to minimize the power consumption. Deep sleep is especially useful for battery-powered applications. Upon triggering a wakeup event, the microcontroller restarts execution from the beginning, similar to a regular startup.
You can program ESP32 to automatically enter deep sleep mode on certain conditions. To do this:
#include "esp_deep_sleep.h"
// Configure ESP32 to enter deep sleep at the end of the setup function
void setup(){
// ...
// Go to sleep. The program will not go past this point.
esp_deep_sleep_start();
}
ESP32 can be programmed to wake up after a certain time. The command esp_deep_sleep accepts microseconds as the parameter.
// To make ESP32 sleep for 10 seconds
esp_deep_sleep(10000000UL);
You can also set ESP32 to be woken up by certain external signals or conditions. To do this:
// To wake up ESP32 when GPIO 16 falls low
esp_sleep_enable_ext0_wakeup(GPIO_NUM_16, LOW);
// To wake up ESP32 from a touch on touch sensor 0
esp_sleep_enable_touchpad_wakeup();
touchAttachInterrupt(T0, touched, threshold);
ESP32 has four types of sleep modes:
Modem Sleep: This mode is used to power down the ESP32’s Wi-Fi, and it’s used when you're running the ESP on batteries.
Light Sleep: This mode powers down the CPUs and most of the system clocks.
Deep Sleep: This mode powers down everything except the RTC clock and RTC memory, resulting in maximum power saving.
Hibernate: This mode powers down everything, including the RTC clock, achieving the most power saving possible.
All ESP32 chips support deep sleep, including but not limited to:
Q: Can ESP32 maintain its state during deep sleep?
A: ESP32 loses all its memory during deep sleep. However, ESP32's RTC memory is not cleared during deep sleep and can be used to store and retrieve information across deep sleep cycles.
Q: Can ESP32 be woken up wirelessly during deep sleep?
A: While the Wi-Fi and Bluetooth are turned off in deep sleep mode, it can be woken up by an external interrupt. Using this, you could potentially wake up the ESP32 via a low-level wireless signal through a dedicated hardware module.
Q: How much power does ESP32 consume during deep sleep?
A: ESP32's deep sleep current is typically measured to be less than 10uA, making ESP32 a great choice for battery driven applications.
That's all for this tutorial. We covered what deep sleep is in an ESP32, how to program it to go to deep sleep mode, the various types of sleep modes in ESP32, which chips support deep sleep, and some common questions. Happy coding!