ESP32 Deep Sleep Tutorial

35,956

45

19

Introduction: ESP32 Deep Sleep Tutorial

About: I am Nick Koumaris from Sparta, Greece. I'm extremely passionate about electronics, making things and design. I love teaching what I know and sharing my experiences with you. I put out new YouTube videos every…

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:

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.

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!

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    19 Discussions

    0
    pgeschwi
    pgeschwi

    7 days 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

    ESP32 weather.jpg
    1
    safi0001
    safi0001

    4 weeks 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

    0
    JeanP27
    JeanP27

    Question 4 months ago

    Is it possible to merge this Deep Sleep/Timer sketch with another sketch that I am using that contains a Loop section?

    0
    tispokes
    tispokes

    Question 7 months 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

    0
    andretnt
    andretnt

    Answer 5 months 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).

    0
    kamil47
    kamil47

    Question 1 year 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?

    IMG_6249.JPGIMG_6242.JPG
    0
    kimiho
    kimiho

    Answer 10 months ago

    Nice work!
    Can you tell me where did you get the Li-ion battery holder? :)

    0
    Vertigo1206
    Vertigo1206

    11 months 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

    0
    roopabj
    roopabj

    Question 1 year 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?

    1
    ltatianagarcia95
    ltatianagarcia95

    1 year 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?

    0
    CasperB2
    CasperB2

    Reply 1 year 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.

    0
    DaveVert
    DaveVert

    1 year ago

    Be aware that Bluetooth communications is not "yet" supported. Also, you will have to re-design your application if you are using Arduino as the "Loop" processing never takes place.

    0
    sdcharle
    sdcharle

    1 year ago

    One way to drop the power consumption on the board you show at the beginning is to get rid of that 'always on' LED. I scraped the LED off my board with an exacto knife, easy to do and things still work fine.

    0
    itsmescotty
    itsmescotty

    2 years ago

    And this is better than a 555, how?

    0
    sdcharle
    sdcharle

    Reply 1 year ago

    Post an instructable where you connect your 555 to your WiFi

    0
    venom.oca
    venom.oca

    2 years ago

    How can I read the adc using the ULP?
    0
    abraxas1
    abraxas1

    2 years ago

    this article is like cheating.

    thanks!

    you got me up to speed on the topic in a couple minutes.

    yet, there are many details, as always.