Three things to mention about deep sleep.
esp_deep_sleep_start
shuts down the processor, so any code you write beneath this line won't be executed.- When the ESP32 awakes from the deep sleep, it will rerun your setup function.
- When you enter deep sleep, all data stored in memory is lost.
The easiest way to wake the ESP32 up from sleep is to use an RTC to wake it up after a certain amount of time. This sketch puts ESP32 into deep sleep until the RTC triggers it after every 1 minute. It also prints the reason for wakeup.
When someone touches the touchpin, you can wake the ESP32 up by doing the following:-
- Configure a threshold value (once this value is crossed, the ESP32 will wakeup)
- Enable the touch interrupt (so the ESP32 knows that it has to keep that powered on)
- Actually go into deep sleep.
When the ESP32 awakes from the deep sleep, it will rerun your setup function and all data from the memory is lost. Hence, I have also used RTC memory to store the counter values.
The ext0 wake up source option uses RTC GPIOs to wake up. So, RTC peripherals will be kept on during deep sleep if this wake up source is requested.
esp_sleep_enable_ext0_wakeup(GPIO_NUM_X, level)
- This function accepts as first argument the pin you want to use. X is the GPIO number. With this wake up source, you can only use pins that are RTC GPIOs.
- The second argument, level, can be either 1 or 0. This represents the state of the GPIO that will trigger wake up.
- https://lastminuteengineers.com/esp32-deep-sleep-wakeup-sources/ (Reason for wakeup method is wrong)
- https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/
- https://savjee.be/videos/programming-esp32-with-arduino/esp32-power-modes-deep-sleep-explained/
This schematic helps us to control deep sleep in Arduino Uno using just one push button.
Schematic
Truth Table
- https://thekurks.net/blog/2018/1/24/guide-to-arduino-sleep-mode
- https://github.com/RalphBacon/Arduino-Deep-Sleep
- https://www.electroniclinic.com/arduino-sleep-modes-automatic-and-manual-to-save-power-arduino-deep-sleep/
- https://forum.arduino.cc/t/arduino-atmega328pu-wake-up-with-a-long-button-press/400968
- https://www.theengineeringprojects.com/2015/11/reset-arduino-programmatically.html : Reset after wake up