IoT/ESP32 B.Tech/Diploma Beginner

ESP32 Deep Sleep with Arduino IDE and Wake Up Sources

Complete guide for the ESP32 Deep Sleep mode with Arduino IDE and different wake up sources: timer wake up, touch wake up, and external wake up (with examples).

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

This article is a complete guide for the ESP32 Deep Sleep mode with Arduino IDE. We’ll show you how to put the ESP32 into deep sleep and take a look at different modes to wake it up:
timer wake up
touch wake up
, and
external wake up
. This guide provides practical examples with code, code explanation, and circuit diagrams.
Related Content:
ESP8266 Deep Sleep with Arduino IDE
This article is divided into 4 different parts:
Introducing Deep Sleep Mode
Timer Wake Up
Touch Wake Up
External Wake Up
Introducing Deep Sleep Mode
The ESP32 can switch between different power modes:
Active mode
Modem Sleep mode
Light Sleep mode
Deep Sleep mode
Hibernation mode
You can compare the five different modes on the following table from the ESP32 Espressif datasheet.
The
ESP32 Espressif datasheet
also provides a table comparing the power consumption of the different power modes.
And here’s also
Table 10
to compare the power consumption in active mode:
Why Deep Sleep Mode?
Having your ESP32 running on active mode with batteries is not ideal since batteries’ power will drain very quickly.
If you put your ESP32 in deep sleep mode, it will reduce the power consumption, and your batteries will last longer.
Having your ESP32 in deep sleep mode means cutting with the activities that consume more power while operating but leaving just enough activity to wake up the processor when something interesting happens.
In deep sleep mode, neither CPU nor Wi-Fi activities take place, but the Ultra Low Power (ULP) co-processor can still be powered on.
While the ESP32 is in deep sleep mode, the RTC memory also remains powered on, so we can write a program for the ULP co-processor and store it in the RTC memory to access peripheral devices, internal timers, and internal sensors.
This mode of operation is useful if you need to wake up the main CPU by an external event, timer, or both while maintaining minimal power consumption.
RTC_GPIO Pins
During deep sleep, some of the ESP32 pins can be used by the ULP co-processor, namely the RTC_GPIO pins, and the
Touch Pins
. The ESP32 datasheet provides a table identifying the RTC_GPIO pins. You can find that table
here
on page 14.
You can use that table as a reference, or take a look at the following pinout to locate the different RTC_GPIO pins. The RTC_GPIO pins are highlighted with an orange rectangular box.
You might also like reading:
ESP32 Pinout Reference: Which GPIO pins should you use?
Wake Up Sources
After putting the ESP32 into deep sleep mode, there are several ways to wake it up:
You can use the
timer
, waking up your ESP32 using predefined periods of time;
You can use the
touch pins
;
You can use
two possibilities of external wake up
: you can use either one external wake up, or several different external wake-up sources;
You can use the
ULP co-processor
to wake up – this won’t be covered in this guide.
Writing a Deep Sleep Sketch
To write a sketch to put your ESP32 into deep sleep mode, and then wake it up, you need to keep in mind that:
First, you need to configure the wake up sources. This means configure what will wake up the ESP32. You can use one or combine more than one wake up source.
You can decide what peripherals to shut down or keep on during deep sleep. However, by default, the ESP32 automatically powers down the peripherals that are not needed with the wake up source you define.
Finally, you use the
esp_deep_sleep_start()
function to put your ESP32 into deep sleep mode.
Timer Wake Up
The ESP32 can go into deep sleep mode, and then wake up at predefined periods of time. This feature is specially useful if you are running projects that require time stamping or daily tasks, while maintaining low power consumption.
The ESP32 RTC controller has a built-in timer you can use to wake up the ESP32 after a predefined amount of time.
Enable Timer Wake Up
Enabling the ESP32 to wake up after a predefined amount of time is very straightforward. In the Arduino IDE, you just have to specify the sleep time in microseconds in the following function:
esp_sleep_enable_timer_wakeup(time_in_us)
Code
To program the ESP32 we’ll use Arduino IDE. So, you need to make sure you have the ESP32 Arduino core installed. Follow the next tutorial to install the ESP32 add-on, if you haven’t already:
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
Let’s see how this works using an example from the library. Open your Arduino IDE, and go to
File
>
Examples
>
ESP32
Deep Sleep
, and open the
TimerWakeUp
sketch.
/* Simple Deep Sleep with Timer Wake Up
ESP32 offers a deep sleep mode for effective power saving as power is an important factor for IoT applications. In this mode CPUs, most of the RAM, and all the digital peripherals which are clocked
from APB_CLK are powered off. The only parts of the chip which can still be powered on are: RTC controller, RTC peripherals, and RTC memories This code displays the most basic deep sleep with a timer to wake it up and how to store data in RTC memory to use it over reboots This code is under Public Domain License.
Author: Pranav Cherukupalli <
[email protected]
> */

#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 5 // Time ESP32 will go to sleep (in seconds)

RTC_DATA_ATTR int bootCount = 0;

// Method to print the reason by which ESP32 has been awaken from sleep
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;

wakeup_reason = esp_sleep_get_wakeup_cause();

switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println(\"Wakeup caused by external signal using RTC_IO\"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println(\"Wakeup caused by external signal using RTC_CNTL\"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println(\"Wakeup caused by timer\"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println(\"Wakeup caused by touchpad\"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println(\"Wakeup caused by ULP program\"); break;
default : Serial.printf(\"Wakeup was not caused by deep sleep: %d\
\",wakeup_reason); break;
}
}

void setup(){
Serial.begin(115200);
delay(1000); // Take some time to open up the Serial Monitor

// Increment boot number and print it every reboot
++bootCount;
Serial.println(\"Boot number: \" + String(bootCount));

// Print the wakeup reason for ESP32
print_wakeup_reason();

// First we configure the wake up source We set our ESP32 to wake up every 5 seconds
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println(\"Setup ESP32 to sleep for every \" + String(TIME_TO_SLEEP) +
\" Seconds\");

/*
Next we decide what all peripherals to shut down/keep on
By default, ESP32 will automatically power down the peripherals
not needed by the wakeup source, but if you want to be a poweruser
this is for you. Read in detail at the API docs
http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
Left the line commented as an example of how to configure peripherals.
The line below turns off all RTC peripherals in deep sleep.
*/
//esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
//Serial.println(\"Configured all RTC Peripherals to be powered down in sleep\");

// Now that we have setup a wake cause and if needed setup the peripherals state in deep sleep, we can now start going to deep sleep.
// In the case that no wake up sources were provided but deep sleep was started, it will sleep forever unless hardware reset occurs.
Serial.println(\"Going to sleep now\");
delay(1000);
Serial.flush();
esp_deep_sleep_start();
Serial.println(\"This will never be printed\");
}

void loop(){
// This is not going to be called
}
Let’s take a look at this code. The first comment describes what is powered off during deep sleep with timer wake up.
In this mode CPUs, most of the RAM,
and all the digital peripherals

Technologies used

ESP32ESP8266ArduinoBLEAngularArduino IDEHTML

Hardware components

  • ESP32
  • ESP8266
  • Arduino

Software & tools

  • Arduino IDE

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.