ESP32 Deep Sleep Tutorial

20,046

36

10

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 eve...

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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

    • Magnets Challenge

      Magnets Challenge
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    10 Discussions

    0
    kamil47
    kamil47

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

    Question 7 weeks 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?

    0
    ltatianagarcia95
    ltatianagarcia95

    6 months 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 3 months 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

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

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

    1 year ago

    And this is better than a 555, how?

    0
    sdcharle
    sdcharle

    Reply 8 months ago

    Post an instructable where you connect your 555 to your WiFi

    0
    venom.oca
    venom.oca

    1 year ago

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

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