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:

 

HARDWARE LIST
1 A Firebeetle ESP32 Board
1 Any ESP32 Board
1 Breadboard
1 LEDS
1 Multimeter
1 Wires

 

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.

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!

CODE
#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.

icon DeepSleepExample.zip 1KB Download(9)

 

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.

CODE
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!

 

 

The article was first published in Instructables on Apr 24th, 2018

cr: https://www.instructables.com/ESP32-Deep-Sleep-Tutorial/

author: educ8s

License
All Rights
Reserved
licensBg
0