Introduction: ESP32 Deep Sleep Tutorial
Dear friends welcome to another Instructable! Today we are going to learn how to put the ESP32 chip into the Deep Sleep mode in order to conserve power and make our projects battery friendly. Have you imagined you project to last on regular AA batteries for almost 5 years? This is possible with the ESP32 chip.
The ESP32 chip is a fantastic new chip with great features. It offers a lot of processing power, two 32 bit cores, a lot of memory, Bluetooth and WiFi in a small and easy to use chip. One of the most interesting things about the ESP32 chip is that it offers a low-power deep sleep mode which is very easy to use. Let’s see how to use it.
Step 1: The Parts Needed
In this tutorial, I am going to use the following parts, so if you want to try this tutorial yourself you need the following:
- Any ESP32 Board ▶ http://educ8s.tv/part/ESP32
- A Firebeetle ESP32 Board ▶ http://educ8s.tv/part/FireBeetle
- Breadboard ▶ http://educ8s.tv/part/LargeBreadboard
- Wires ▶ http://educ8s.tv/part/Wires
- LEDS ▶ http://educ8s.tv/part/LEDs
- Multimeter ▶ http://educ8s.tv/part/Multimeter
Step 2: Inside the ESP32 Chip
Inside the ESP32 chip, we can find the two processing cores, the RAM and ROM memory, the WiFi module, the Bluetooth Module, a hardware acceleration module for cryptographic applications, the RTC module, and a lot of peripherals. Inside the RTC module, we can find a PMU (Phasor measurement unit) a small and very low power 32-bit co-processor, and 8Kbs of RAM memory. This small amount of memory is very useful as you are going to find out in a moment. Also note, even the RTC memory of the ESP32 chip is 4 times larger than the memory of the Arduino Uno.
The WiFi modules, the Processing Cores, and the Bluetooth module require a lot of current to operate. So, if we want to conserve power we have to disable them when don’t use them. This is what we are going to do now. We are going to put the ESP32 to Deep – Sleep mode where it disables everything except the RTC module. There is a light sleep mode and the Deep – Sleep mode. In Deep Sleep mode the ESP32 offers the lowest power consumption. It just needs 0.01 mAs of current in Deep Sleep mode and that’s why we are going to try today.
Step 3: The ESP32 Deep Sleep Mode
In this mode as I said earlier, everything is disabled. The CPU cores, the WiFI module, the Bluetooth Module, the Peripherals and so on. Along with the CPU, the main memory of the chip is also disabled, so everything stored in the memory is lost forever. The only module that still works when in deep-sleep mode is the RTC module, the ultra-low-power co-processor, and its memory! So, if we save the data we want to survive the Deep-Sleep mode into the RTC memory they will be intact when we wake the chip back up.
There are three ways to wake up the chip. We can use a timer, a GPIO pin, or the co-processor.
Today we are going learn how to use the timer to wake up the chip after a specific amount of time. Let’s see an example.
Step 4: A Deep Sleep Example
I have connected two LEDs to this ESP32 board. When the ESP32 boots up it light up the yellow LED for three seconds, and then it goes into Deep-Sleep mode for 3 seconds. When it wakes up, it lights up the Green led for 3 seconds and goes back to sleep. From now on it will only blink the green LED, so the chip remembers that it is not the first time it boots up because we are using the RTC memory to store an integer value.
Let’s take a quick look at the code of this project. As you can see the code is very simple. In order to put the ESP32 into Deep-Sleep mode, all we need is two lines of code.
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
We enable the timer with the esp_sleep_enable_timer_wakeup function, we enter the time to sleep in seconds, and then we call the esp_deep_sleep_start function. That’s it!
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 3 /* Time ESP32 will go to sleep (in seconds) */RTC_DATA_ATTR int bootCount = 0;
There is a small difference with the execution of the code though. When we use the deep-sleep function, each time the ESP32 wakes up, it executes the setup function again. The loop function is never called. All the variable values are lost, except if we save them in the RTC memory using this prefix. In this example, I save the bootCount int variable into the RTC memory in order the program to know if it is the first time it runs and turn on the correct LED. As always you can find the code of this example attached to this instructable.
Attachments
Step 5: The Power Consumption
Let’s now see the power consumption of the board. When the ESP32 is in active mode, it draws around 60mAs of current from the battery. When the ESP32 is in Deep Sleep mode it draws around 19mAs of current! This is a big reduction in current draw, but the creators of the chip claim that it needs 0.01mA of current in deep-sleep mode. What’s wrong with our setup?
The culprit is the board. I am using a DOIT ESP32 board, the first ESP32 board that appeared on the market about a year ago. The design of the board is not optimized for power consumption so, even in deep-sleep mode, it needs a lot of current. Luckily there are better designed ESP32 boards out there.
For example, the Firebeetle ESP32 board by DFrobot is better designed and can achieve a deep-sleep current of just 0.01mΑs when powered by a 3.3V power supply. If we power the board with the same battery pack we used before, which outputs around 4.8V, we can see that the current draw is 48mAs in active mode and just 0.05mAs Deep Sleep mode! Impressive isn’t it! We can further reduce the power consumption of the board if we use a 3.3V battery or power supply. I will try that in a future video. The 0.05mΑs of current that the board requires in Deep-Sleep mode is the lowest current draw I have ever seen in a fully featured ESP32 board, with USB to serial driver, regulator, and battery circuit.
If you have discovered any board that can achieve similar or better results than the Firebeetle board, please let me know in the comments section below, I would love to try it.
The power consumption of the Firebeetle ESP32 board in Deep-Sleep mode is extremely low. It needs around 1.44 mΑhs per day if powered by 4 AA rechargeable batteries. So, in theory, this power bank which has a capacity of 2.500mAhs can power the board for almost 5 years if we put it in Deep Sleep mode! Of course, we are going to wake up the board from time to time to perform a task which will require more power, so the battery life will be reduced greatly.
Step 6: A Deep Sleep Bug
Unfortunately, the ESP32 software & hardware is not mature yet. There is a software or hardware bug that appears in both the ESP32 boards I tried when using the deep sleep mode. After a random number of wakes up, the ESP32 goes to sleep and it won’t wake up again. This bug can happen after a couple of wake ups or after 100. It is just random.
One simple solution I discovered is to add a small delay of 500ms after waking up and before reading from the RTC memory. This way the project worked fine, but of course, the penalty we pay is reduced battery life, because the chip is in active mode for 500ms more in each wake-up. I think this bug will be resolved in the near future with a new software or hardware fix.
void setup(){
pinMode(GREEN_LED_PIN,OUTPUT); pinMode(YELLOW_LED_PIN,OUTPUT); delay(500); if(bootCount == 0) //Run this only the first time { digitalWrite(YELLOW_LED_PIN,HIGH); bootCount = bootCount+1; }else { digitalWrite(GREEN_LED_PIN,HIGH); } delay(3000);
digitalWrite(GREEN_LED_PIN,LOW); digitalWrite(YELLOW_LED_PIN,LOW);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); esp_deep_sleep_start(); }
Step 7: Final Thoughts
I have already used the Deep sleep functionality in a project. Do you remember the E-Paper thermometer Instructable I posted a few weeks ago? It needed 60mAs current when not updating. Now, using the deep sleep functionality of the chip, I managed to reduce current draw to 0.43mAs of current. So, with this power bank, we now have an estimated battery life of around 3 months. Great, isn’t it?
But I think there is a lot of room for improvements; there is a small current leak somewhere in my circuit. If we can reduce it, we can make this project run on batteries for over a year! I think this is amazing! We now have an extremely powerful board with very low power consumption. The best of all is that all we need to do to take advantage of this is to use just two lines of code! I will start using this feature a lot in my future projects.
I would love to know your opinion about the Deep Sleep mode that the ESP32 offers. Are you going to use it in any of your projects? Please post your ideas in the comments section below; I love reading your thoughts! Thanks!
26 Comments
2 years ago
Hello,
I'm trying to power an ESP32 with 2 AA alcaline batteries and using a step up DC-DC to make it to 5V. The red led blinks all the time non stop. This setup works for the NodeMCU ESP8266
It also works using a simple phone charger.
Any ideas?
Reply 18 days ago
Connect the batteries directly to the 3.3 v pin. It will work down to around 2.2 v. That way you don't waste power on that voltage step up.
Question 4 months ago on Step 7
Hi, i have a esp32 firebeetle. I followed exact same procedure, but my current stays at 900uA. Can you please let me know if there is any missing procedure which may not be mentioned in your post?
Answer 3 months ago
Hi,
On the firebeetle, there is a RGB addressable LED that always consumes. You can desolder a 0 ohm resistor to get less consumption :)
8 months ago
Hi,
nice article :)
regarding the bug you found, you might want to look at the docs for "Power down of Flash". It may be that your board doesn't wake up because esp_deep_sleep_start() forces power down of flash and you didn't leave enough time for it to power down before restarting. That would explain why your delay fixes it...
https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/system/sleep_modes.html#power-down-of-flash
(BTW, PMU is "Power Management Unit" :)
https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf S.3.6.1)
10 months ago
Since the board goes to sleep after 3 seconds, for updating the code, how do you connect with the Arduino IDE? Essentially, the board is almost always off. I can't make contact with it from the Arduino IDE before it shuts itself off.
Reply 10 months ago
After I posted this, I tried another host. On a linux box, the Arduino IDE doesn't see the USB ports while they are off. On a W10 box, the port is there. QED
2 years ago on Step 7
I wanted to run a weather display using e-ink display on battery and ran into some issues regarding the power consumption of additional components besides the ESP32. I found that the battery is depleting quickly because the dev-board I used (similar as above) was not switching off the power to the serial port chip and also did not have a true LDO power regulator. I already had removed the power-LED which was on all the time. In deep sleep, my board was pulling 9.8mA (yes milli Amps). This will deplete a LIR2032 within 6h. So I came across the EzSBC board designed to use an LDO and not to power the serial chip if USB is not plugged in. This brought down the consumption to 90 uA in deep sleep inclusive the e-paper display. Unfortunately, I used the 4.2in waveshare display with level shifters on board which are not switched off during sleep. The other issue I found is a 2x 220kOhm resistor voltage splitter I used for measuring the battery voltage. The voltage divider is using the same amount of current as the deep sleep mode of the ESP32 chip itself. To help reducing the idle power consumption for the resistor-voltage-divider and the display, I connected the display ground and the voltage divider ground to IO pins instead. I programmed these to go low when the chip is waking up and release them before going to sleep. This way I made the level shifters on the display and the voltage splitter have a floating ground during sleep and in turn not consuming power while being switched off. This reduced power consumption to about 10 uA which is a factor of roughly 1000. A pair LIR 2032s is now lasting about a 50 days. You could use a single LIR2032 to power this setup as it's voltage is about 4V when fully charged. The ESP32 is running stable down to an input voltage of 3.0 volts on the battery and 2.8v on the chip itself. I had a holder for two button cells, so I used this in the attached photo. The other plus for the EzSBC dev board is having a charging circuit on board and can charge a LiPo 1000mAh battery which will make this weather station last very long on one charge. The link to the EzSBC Dev board:
https://www.ezsbc.com/product-category/product-bundles/
Another very low power ESP32 board is the TTGO T7 especially if you need a small footprint. It is not breadboard friendly due to the double row of contacts on both sides.
https://www.aliexpress.com/item/32977375539.html
2 years ago on Step 4
I apologize for my faulty English.
A low-power ESP32 board is hard to find, but can be easily made from any board:
1. Replace the IC of the high current voltage regulator with HT7833 (low quiescent current 4 uA).
2. Program via UART, do not use USB / UART converter (desolder)
Plus other things to do before going to sleep (turning off peripherals, etc.).
Hi: safi
Question 3 years ago
Is it possible to merge this Deep Sleep/Timer sketch with another sketch that I am using that contains a Loop section?
Question 3 years ago
Hi, i tried to get my ESP32 into DeepSleep
Powered by a 1x18650 PowerBank by USB (directly to microUSB Port)
My Code:
void setup(){
......
adc_power_off();
esp_bluedroid_disable();
esp_bt_controller_disable();
esp_wifi_stop();
esp_sleep_enable_timer_wakeup(3600ul * 1000000ul); // uS_TO_S_FACTOR 1000000
esp_deep_sleep_start();
}
Current consumption: Starting and doing 240 mA, *DeepSleep 10 mA*
*Any suggestions why i have so much PowerConsumption?*
No external devices are connected during the measurement, only the esp32. Usual there are some moisture sensors connected.
Greetings
Lars
Answer 3 years ago
A bunch of ESP32 boards use the LD1117 as 3.3 Volts regulator. This component by itself drains at least 5 mA as quiescent current. A possible way to reduce consumption for those boards is replacing this voltage regulator by a low quiescent current one. Make sure you select one that can deal with the maximum drained current as well, like the TPS7A2033PDBVR from TI (max current 300mA).
Question 3 years ago
Hallo Nick,
I appreciate your projects, good topics and nice explanation.
I have made E-paper display thermometer. Now I am testing ESP 32 with Deep Sleep and with thermometer. It is working but thermometer is booting together with ESP-32. Probably memory problem, everything (code) should be in RTC? How to put all code of the E paper thermometer to RTC or what is not necessary to be in RTC? If you have function code for E paper thermometer with deep sleep I would be happy.
By the way, there is available nice Li-ion battery holder, for this project, including charger - on photos. It provide 5V or 3.3V from the battery 18650 and allow to charge battery inside instrument. Hope, battery would last for a half year?
Answer 3 years ago
Nice work!
Can you tell me where did you get the Li-ion battery holder? :)
Reply 3 years ago
Hi,
you can find it at any e-shop, like aliexpress, ebay or banggood. It is for 5V,6V, 12V output with name 18650 Lithium Battery Charging UPS boost module. Here is link for one of them. https://www.banggood.com/5V-218650-Lithium-Battery-Charging-UPS-Uninterrupted-Protection-Integrated-Board-Boost-Module-With-Battery-Holder-p-1444382.html?rmmds=search&cur_warehouse=CN
3 years ago
Hy!
Nice article, but is there a mistake with the comma at the measurement on the Firebeetle board?
I just tested mine in deep sleep mode: 8µA (0.008mA)
During startup is a peak around 100mA
Connecting to WiFi and MQTT: ~66mA
3 years ago
Hi Nick,
https://thingpulse.com/product/epulse-thingpulse-esp32-devboard/
This board from e-pulse requires 25-35 micro amp in deep sleep
Question 4 years ago
Hi all,
I am trying to program using ESP WROOM-32 BOARD.
entered deep sleep mode and measuring power consumption as 6mA .
how to reduce power consumption to 0.01mA?
4 years ago
Hello
I need use the deep sleep mode with a sensor, but it is run just one time for the void loop in each wake up, i need to stay almost 1 minute in the void loop to take a good measure, can you help me?
Reply 4 years ago
make a while loop in which you make your measurement, the condition to get out of it should be some kind of timer.
once it gets out of the loop you can ga to sleep.