
Timezone: Europe/London
The deepsleep function of the ESP32 allows you to put the board into hibernation, where it draws significantly less power than when running code, sleeping or idling.
From the specifications, in normal usage the board consumes around 20mA with WiFi and Bluetooth turned off. When WiFi and Bluetooth are active, consumption can be 240 mA and up to 750mA when the radios are transmitting or receiving.
In deepsleep, all radios are turned off, the RAM, CPUs and periphials are turned off, and only the RTC components are active. In this state it consumes around 10uA. This is extremely useful if your project runs on batteries.
A bonus from the above, emerging from hibernation is very similar to a power on reset, so the RAM memeory is 'cleared'. If your program calculates a lot of data, and you do not specifically clear memory when it is no loger needed
this can mount up. A program running for several weeks may, at the least, slow down somewhat, or at worst crash with a memory error of some kind.
So even a short deepsleep amounts to a pseudo system reset, and the CPU starts running your code from the beginning, not where it left off, so it is worth noting no code written after
the deepsleep command will be executed. The deepsleep function is contained in the machine module, which needs to be imported into your program.
There are also deepsleep wakup sources which can be implemented. These reside in the esp32 module.
Timer
Use the command machine.deepsleep(number of microseconds) to enter deepsleep. When [microseconds] time has elapsed, the ESP wakes up and starts running the code from the beginning.
External wakeup sources
There are two of these, ext0 and ext1. The ext1 mode allows you to use a GPIO pin as a wakeup source, ext0 mode allows you to use more than one pin.
Note, only RTC GPIO pins can be used for this. You can find out which pins these are from your boards specification.
The processor can be programmed to wake when the assigned pin goes either high or low depending on your requirements. The ext1 mode is the same as the above, but using multiple pins.
The code below demonstrates the deepsleep and wake functions described above.
Set up two buttons, each with a 10Kohm pull down resistor to the ESP 3volt pin, configured so when the button is pressed, the GPIO pin goes high.
When the ESP has entered deepsleep, it will wake either after the allotted time has elapsed, or if a button is pressed first.
If you only want to use one button, use ext0, esp32.wake_on_ext1(pins = (button1, button2), level = esp32.WAKEUP_ANY_HIGH). Note that currently if using multiple buttons, micropython currently
does not have a way to tell which button was used to wake the ESP. The line button1.irq(trigger=machine.Pin.WAKE_HIGH, wake=machine.DEEPSLEEP) only works for one button. If you try
to configure more than one you will receive an error "no available resources".
IDE editors for programming ESP boards.
Micropython editors
Micropython functionality and libraries.
Micropython.org
Find the uPython firmware for your board.
uPython firmware for development boards.
ESP32 uPython modules and function examples.
Quick reference for the ESP32
Scanning and connecting to multiple SSIDs
Using an SD card reader breakout board
The Deepsleep function
OTA Updating running software without interuption
Future articles
Communication between ESPs
Using an Adafruit GPS module
Hassle free web communication with urequests.py.