There is no doubt that the ESP32 is a worthy competitor to many WiFi/MCU SoCs, often beating it both on performance and price. But, depending on what mode it is in, the ESP32 can be a relatively power-hungry device.
When your IoT project is powered by a plug in the wall, you don’t care too much about power consumption. But if you’re going to power your project from a battery, every mA counts.
The solution here is to take advantage of one of its sleep modes to reduce the ESP32’s power usage. This is actually a great strategy for dramatically increasing the battery life of a project that doesn’t need to be active all the time.
What is ESP32 Sleep Mode?
ESP32 sleep mode is a power-saving mode that the ESP32 can enter when not in use, saving all data in RAM. At this time, power to any unnecessary peripherals is cut off while the RAM receives enough power to be able to retain its data.
Inside ESP32 chip
To understand how ESP32 achieves power saving, first we need to know what is inside the chip. The following illustration shows the function block diagram of the ESP32 chip.
At the heart of the ESP32 chip is a dual-core 32-bit microprocessor with 448 KB of ROM, 520 KB of SRAM, and 4MB of flash memory.
The chip also contains a WiFi module, a Bluetooth module, a cryptographic accelerator (a co-processor specifically designed to perform cryptographic operations), an RTC module, and a number of peripherals.
ESP32 Power Modes
Thanks to the ESP32’s advanced power management, it offers five configurable power modes. According to the power requirement, the chip can switch between different power modes. These modes are:
- Active Mode
- Modem Sleep Mode
- Light Sleep Mode
- Deep Sleep Mode
- Hibernation Mode
Each mode has its own unique features and power saving capabilities. Let us look at them one by one.
ESP32 Active Mode
Normal mode is also known as Active Mode. In this mode all the features of the chip remain active.
Since active mode keeps everything (especially the WiFi module, processing core and bluetooth module) running at all times, over 240mA of current is required to operate the chip. It has also been observed that sometimes high power spikes appear (as large as 790mA) if you use both WiFi and Bluetooth together.
According to the ESP32 datasheet the power consumption during active power mode, with RF working, is as follows:
|Wi-Fi Tx packet 13dBm~21dBm||160~260mA|
|Wi-Fi/BT Tx packet 0dBm||120mA|
|Wi-Fi/BT Rx and listening||80~90mA|
Obviously this is the most inefficient mode and dissipates the most current. So, if you want to conserve power you have to disable unusable features by taking advantage of another power mode.
ESP32 Modem Sleep
In modem sleep mode everything is active except wifi, bluetooth and radio. It also keeps the CPU running and the clock is configurable.
In this mode the chip consumes about 3mA at slow speed and 20mA at high speed.
To keep the WiFi/Bluetooth connection alive, Wi-Fi, Bluetooth and the radio are woken up at predefined intervals. This is known as the Association Sleep Pattern.
During this sleep pattern, ESP32 switches between active mode and modem sleep mode.
To achieve this the ESP32 connects to the router in station mode using the DTIM beacon mechanism.
To conserve power, the ESP32 deactivates the Wi-Fi module between two DTIM beacon intervals and automatically wakes up before the next beacon arrival.
The sleeping time is decided by the router’s DTIM beacon interval time which is usually 100ms to 1000ms.
What is the DTIM Beacon Mechanism?
DTIM is an acronym for Delivery Traffic Indication Message.
In this mechanism, the access point (AP)/router periodically broadcasts beacon frames. Each frame contains all the information about the network. It is used to announce the presence of a wireless network and to synchronize all connected members.
ESP32 Light Sleep
Light sleep is similar to modem sleep. The chip follows the Association Sleep Pattern in this too.
The only difference is that during light sleep mode, the digital peripherals, most of the RAM and CPU are clock-gated.
What is Clock Gating?
Clock gating is a technique to reduce dynamic power consumption.
In this, parts of the circuitry are disabled by turning off the clock pulses, so that the flip-flops in them do not have to switch states. Since switching states consumes power, when not switched, the power consumption drops to zero.
During light sleep mode the CPU is paused by turning off its clock pulse, while the RTC and ULP-coprocessor are kept active. This results in a lower power consumption than the modem sleep mode which is around 0.8mA.
Before entering light sleep mode the ESP32 stores its internal state in RAM and resumes operation upon waking from sleep. This is known as Full RAM Retention.
Once the wake-up sources are configured, the
esp_light_sleep_start() function is used to enter light sleep.
ESP32 Deep Sleep
In deep sleep mode, the CPUs, most of the RAM and all digital peripherals are powered off. The only parts of the chip that remain operational are:
- ULP Coprocessor
- RTC Controller
- RTC Peripherals
- RTC fast and slow memory
The chip consumes around 0.15 mA (if the ULP coprocessor is on) to 10µA.
During deep sleep mode the main CPU is shut down, while the UltraLowPower (ULP) Coprocessor can take sensor readings and wake up the CPU whenever necessary. This sleep pattern is known as the ULP sensor-monitored pattern. This is useful for designing applications where the CPU needs to be woken by an external event, or timer, or a combination of both, while maintaining minimal power consumption.
Along with the CPU, the main memory of the chip is also disabled. As a result everything stored in that memory is erased and cannot be accessed.
Because RTC memory is kept on, its contents are preserved even during deep sleep and can be retrieved after the chip is woken up. This is why the chip stores Wi-Fi and Bluetooth connection data in RTC memory before entering deep sleep.
If you want to use the data after reboot, store it in RTC memory by defining a global variable with
RTC_DATA_ATTR attribute. For example,
RTC_DATA_ATTR int myVar = 0;
After coming out of deep sleep the chip restarts with a reset and starts program execution from the beginning.
ESP32 supports running a deep sleep wake stub when coming out of deep sleep. This function runs immediately as soon as the chip wakes up – before any normal initialisation, bootloader, or ESP-IDF code has run. After the wake stub runs, the chip can go back to sleep or continue to start ESP-IDF normally.
Unlike other sleep modes, the system cannot automatically go into deep-sleep mode. The
esp_deep_sleep_start() function is used to enter deep sleep immediately after configuring wake-up sources.
To learn more about ESP32 Deep Sleep and its wake-up sources, please visit the tutorial below.
ESP32 Hibernation mode
Unlike deep sleep mode, in hibernation mode the chip disables the internal 8 MHz oscillator as well as the ULP-coprocessor. The RTC recovery memory is also powered off, which means we cannot preserve any data while in hibernation mode.
Everything else is turned off except for just one RTC timer (on slow clock) and a few RTC GPIOs. They are responsible for waking the chip from hibernation mode.
This further reduces the power consumption. The chip only consumes about 2.5μA in hibernation mode.
This mode is especially useful if you’re building a project that doesn’t need to be active all the time.