ESP8266 is very popular for building IoT based Projects, but nowadays ESP32 is getting a lot of attention because of its BLE compatibility feature at a lowered cost. It also comes with 32 GPIO pins with a 32-bit dual-core CPU. Although, it offers a lot of features, but it seems to be power-hungry at a normal mode of use. When the applications are powered by the mains supply, there is no matter of hurry, but when they are powered by a battery, and then we must be very conscious regarding the ESP32 power consumption.
ESP32 offers different sleep modes, using which we can save a lot of power and can be able to run our IoT applications for a longer time using the battery source. In this article, we will discuss different sleep modes of ESP32 and will check the current consumption in normal as well as in deep sleep mode. To learn more about ESP32, check the various ESP32 based projects.
Understanding ESP32 Hardware
It can be seen in the figure above, ESP32 comes with a dual-core 32-bit Microprocessor with 448 KB of ROM, 520 KB of SRAM and 4MB of Flash memory. Additionally, it has an inbuilt Wi-Fi Module, Bluetooth, RTC, and a lot of other peripherals which makes it popular for several IoT applications using ESP32.
The ESP32 also has a low power coprocessor which is called ULP. ULP stands for Ultra-Low Power which is integrated into the ESP32 board. The specialty of this processor is that it can run independently from the main core processor and it also has access to GPIOs and some of the peripherals. It is also able to run when ESP32 is in deep sleep mode. This helps to save a lot of power when powered from the battery source.
ESP32 Sleep Modes
ESP32 can be configured at five different power modes. Each mode has different power consumption ratings. The different power modes of ESP32 are:
- Active Mode
- Modem Sleep Mode
- Light sleep Mode
- Deep sleep Mode
- Hibernation Mode
A comparison between different power modes of ESP32 as per Espressif datasheet is given below:
Power Mode |
Active |
Modem Sleep |
Light Sleep |
Deep Sleep |
Hibernation |
CPU |
ON |
ON |
PAUSE |
OFF |
OFF |
Bluetooth/Wi-Fi/Radio |
ON |
OFF |
OFF |
OFF |
OFF |
RTC & Peripherals |
ON |
ON |
ON |
ON |
OFF |
ULP |
ON |
ON |
ON |
ON/OFF |
OFF |
Current Consumption |
95-240 mA |
2-50 mA |
0.8 mA |
10-150 uA |
5 uA |
Active Mode:
In the active mode of ESP32, all the devices including CPU, Bluetooth, Wi-Fi, radio, RTC and ULP coprocessor are switched ON. This is the most inefficient power mode of ESP32. This mode is preferred, when the device is powered from the main supply. As per the Espressif datasheet, it claims a current consumption of 95-240 mA in this mode.
Modem Sleep Mode:
In this mode, everything is kept active except Wi-Fi, Bluetooth, Radio module and peripherals. This mode consumes around 2 mA at a slow speed where consumes around 50 mA at high speed.
When the module is kept in this mode, if we need to keep Bluetooth/Wi-Fi connections active, then we have to wake the module in between a certain interval, which will switch the module in between active and modem sleep modes. This sleep pattern is called as Association sleep pattern.
Light Sleep Mode:
In light sleep mode of ESP32, digital peripherals, most of the RAM, and CPUs are clock-gated. When light sleep mode exits, peripherals, and CPUs resume operation, their internal state is preserved. Clock gating is a technique to save power consumption in digital circuitry by disabling the clock pulses to flip-flops, which in turn disables the switching states. As switching states consume a lot of power, hence if you disable it, we can save a lot of power.
This power mode of ESP32 consumes around 0.8 mA of current.
Deep Sleep Mode:
Deep sleep mode of ESP32 is very efficient and consumes very less power when powered from the battery sources. In this mode, everything is switched off including CPU, Wi-Fi, Bluetooth, Peripherals, etc. Only the ULP coprocessor, RTC and RTC Memory are turned on in this mode.
We can write a program in ULP and store it in its RTC memory so that it can access the peripherals, timers, etc. When operating in deep sleep mode, ESP32 can be wake up by using events such as Timers, External interrupts, etc.
ESP32 deep sleep power consumption is around 10-150uA of current which is very impressive.
Hibernation:
In this mode of ESP32, everything is disabled except RTC timers and some RTC GPIO pins which will be useful when the module needs to be woken up. In this mode, there is no way to save any data, unlike the deep sleep mode, where we are able to save data using RTC memory.
This Power mode of ESP32 consumes a minimum amount of current, which is around 5 uA.
ESP32 Deep Sleep Mode Hardware Implementation
In this section, we will practically demonstrate the deep-sleep power mode of ESP32 and will compare its current consumption with the Active power mode. There are several methods to wake up the ESP32 module from deep sleep mode like Timer wakeup, touch wakeup, external wakeup, etc. In this tutorial, we will focus on the Timer wakeup.
In the Timer-based wakeup, ESP32 will go to deep-sleep mode, then after a certain time interval, it will automatically wake up and restored to its usual condition. Here we can also define some variables which can be stored in RTC memory and can be used on each reboot.
Here we have connected two LEDs with ESP32 and powered the ESP32 with the Breadboard power supply module. In the first boot, a Red LED will glow, which will indicate that ESP32 has been booted for the first time. After that, for all reboots, Green LED will glow, which indicates that it retains the value of boot number in RTC memory.
Programming for waking up ESP32 from Deep Sleep
Complete code for ESP32 wake up using Timer is given at the end. Here is the stepwise explanation of the code, which is to be uploaded to ESP32, in order to make ESP32 into the deep-sleep mode and then wake up using the Timer method.
First of all, we need to define the sleep time, for which the ESP32 will go to deep sleep. For this, first set the conversion factor to convert the seconds into microseconds. Then provide the sleep time interval in seconds. In my case, it is 6 seconds for which the module will go to deep sleep mode.
#define factor 1000000 #define sleep_time 6
Next, define any variable in RTC memory to save the data in the next reboot. For this, we need to declare this as follows. In my case, I have displayed the count of reboots of ESP32 in the Serial monitor.
RTC_DATA_ATTR int boot_number = 0;
Then create a condition to check whether it is the first boot or not. In the first boot, a Red LED will glow, which will indicate that ESP32 has been booted for the first time. After that, for all reboots, Green LED will glow, which indicates that it retains the value of boot number in RTC memory.
if(boot_number == 0) { digitalWrite(RED,HIGH); boot_number++; } else { digitalWrite(GREEN,HIGH); }
In the final step, configure the deep sleep timer parameters which we have defined earlier. This can be done using function esp_sleep_enable_timer_wakeup and then to start the deep sleep mode of ESP32, call the function esp_deep_sleep_start() as shown below.
esp_sleep_enable_timer_wakeup (sleep_time * factor); esp_deep_sleep_start();
ESP32 Sleep Modes Testing
Active Mode:
Here you can see the amount of current consumption in Active mode is around 60mA.
ESP32 Deep Sleep Mode:
Now after putting ESP32 in Deep Sleep mode, the current consumption is reduced significantly to 11mA.
Also, check the demonstration video given below.
#define factor 1000000
#define sleep_time 6
#define RED 2
#define GREEN 4
RTC_DATA_ATTR int boot_number = 0;
void setup()
{
Serial.begin(115200);
pinMode(RED,OUTPUT);
pinMode(GREEN,OUTPUT);
delay(500);
if(boot_number == 0)
{
digitalWrite(RED,HIGH);
boot_number++;
}
else
{
digitalWrite(GREEN,HIGH);
}
delay(4000);
digitalWrite(GREEN,LOW);
digitalWrite(RED,LOW);
Serial.println("ESP32 going to Deep Sleep");
esp_sleep_enable_timer_wakeup(sleep_time * factor);
esp_deep_sleep_start();
}
void loop()
{
}