Here we will be discussing how we can set ESP32 in deep sleep mode at fixed time to save power. Before we learn how to wake up ESP32 from deep sleep using timer, let’s understand the concept of deep sleep:
What is Deep Sleep in ESP32
ESP32 can be a power-hungry device because of its integrated WiFi and Bluetooth module. ESP32 typically draws 75mA for nominal operations while it can go up to 240mA when transmitting data over WiFi. However, we can optimize this by enabling deep sleep mode.
In deep sleep mode, ESP32 digital peripherals, unused RAM and CPUs are turned off. Only following list of parts remains operational:
- RTC Controller
- ULP Coprocessor
- RTC fast and slow memory
- RTC Peripherals
When deep sleep mode is enabled, the main CPU is shut down; however, the ULP (UltraLowPower) Coprocessor can still read data from sensors and wake up the CPU whenever needed.
This application of ESP32 comes handy where we want to generate output at some specific time or when an external interrupt or event happens. This saves ESP32 power as its CPU remains off for the rest of the time and only turns on when it is called.
Along with CPU ESP32 main memory is also flashed or erased, so anything stored inside this memory will no longer be available. Only RTC memory is kept there. Therefore, ESP32 saves WiFi and Bluetooth data inside the RTC memory before going into deep sleep mode.
Once the deep sleep mode is reset or removed the ESP32 chip starts the execution of the program from the very beginning.
From deep sleep we can wake up ESP32 using different methods.
Wake Up Sources in ESP32
Multiple Sources are available to wake up ESP32 from deep sleep:
- Timer
- Touch pins
- External wakeup ext0
- External wakeup ext1
In this guide we will cover Timer wake up source for ESP32.
How to use Timer to Wake Up ESP32 from Deep Sleep
ESP32 comes with an RTC controller that has a built-in timer module that can wake up ESP32 after a predefined amount of time. This feature has vast applications where we need time stamping or need to execute instructions at specific times while maintaining optimum power consumption.
To put ESP32 in deep sleep mode using the MicroPython code deepsleep() function from the machine module will be used. Following is the syntax of deep sleep function in MicroPython:
This function takes 1 argument which is a predefined time in milliseconds.
To understand the use of ESP32 timer for wake up we will take an example that blinks LED after every set time passes and goes back to sleep once the task is done.
Example Code
Open any MicroPython editor and upload the below given code in ESP32 board. Here we will be using Thonny IDE for uploading MicroPython sketches.
from machine import deepsleep
from machine import Pin
from time import sleep
led = Pin (4, Pin.OUT) #PIN 4 defined for LED output
led.value(1) #Turn ON LED for 1 sec
sleep(1)
led.value(0) #Turn OFF LED for 1 sec
sleep(1)
print('Going to Sleep Now')
deepsleep(5000) #Sleep for 5 sec
Code started by importing necessary libraries such as we imported the deepsleep library.
After that a new object for ESP32 pin 4 is created. This pin will show the output every time ESP32 wakes up.
Below given commands will blink LED with delay of 1 sec.
sleep(1)
led.value(0)
sleep(1)
Here for demonstration purposes, we blink the LED. However, any other device can also be controlled.
Before going to sleep we printed a message that ESP32 is going in sleep mode.
Note: We can also add a delay of 5 or more seconds here before ESP32 goes into sleep mode. This helps us when building a project and writing a new script. While uploading a new code the board must be awake and not in sleep mode. If we don’t add the delay, it will be difficult for us to catch ESP32 in awake mode and upload a new script.
After writing the new script and once the final code is ready, we can remove this delay in the final version of script.
Finally, the ESP32 board is put in deep sleep for 5 sec (5000 ms).
Once the 5 seconds time passes the ESP32 wakes up and restarts the code similar to the EN button.
Output
Following output can be observed on the shell terminal of Thonny IDE. Here we can see that after every 5 sec ESP32 wakes up from deep sleep and blink the LED at GPIO pin 4.
The LED at GPIO 4 will turn ON for 1 sec.
After 1 sec the LED will turn OFF.
Now the ESP32 board will again go into sleep mode for 5 sec and after that the whole process repeats. So that’s it we have successfully controlled ESP32 deep sleep mode using the timer code.
Conclusion
Here in this tutorial, we have configured ESP32 to wake up from deep sleep using a timer program written in MicroPython. We uploaded the code using the Thonny IDE. We have simply printed a message once the ESP32 wakes up and blinks a LED; however, using this article one can execute any task once the ESP32 wakes up from deep sleep.