Esp32

ESP32 Timer Wake Up from Deep Sleep Using Arduino IDE

vESP32 is a microcontroller based IoT platform. Power consumption is one of the main concerns while working with a microcontroller board. If we are powering ESP32 with a DC supply we don’t care more about power consumption but with battery backup projects for the long run we need to optimize overall power.

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, ULP (UltraLowPower) Coprocessor can still read data from sensors and wake up the CPU whenever it is needed.

This application of ESP32 comes handy when 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.

ESP32 can be woken up from deep sleep using different sources.

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

The RTC controller that comes with ESP32 contains a timer module that can wake the device up after a certain period of inactivity. This feature has vast applications where we need time stamping or need to execute instructions at specific times while maintaining optimum power consumption.

Following command can configure the ESP32 timer as a wake-up source. It accepts time in microseconds as an argument.

esp_sleep_enable_timer_wakeup(time_in_micro-s)

Example Code

If you have ESP32 board installed in Arduino IDE, then ESP32 comes with a deep sleep example which we will be using in this tutorial. In Arduino IDE deep sleep timer wakeup example can be opened by going to: File > Examples > ESP32 > Deep Sleep > TimerWakeUp

A new window will open with below sketch:

#define uS_TO_S_FACTOR 1000000ULL
#define TIME_TO_SLEEP  5        
RTC_DATA_ATTR int bootCount = 0;
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 reason external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup reason 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("Deep sleep did not caused wake up: %d\n",wakeup_reason); break;
  }
}
void setup(){
  Serial.begin(115200);
  delay(1000);
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));
  print_wakeup_reason();
  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");
  Serial.println("Going to sleep now");
  Serial.flush();
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}
void loop(){
}

Define Deep Sleep: Code starts by describing the time for which ESP32 will go in sleep mode. This can be modified depending upon the required time. Here time is converted from microseconds to seconds, so we have set 5 sec for ESP32 deep sleep mode. It will wake up after every 5 sec.

RTC Data Memories: Next using RTC_DATA_ATTR we will save data on RTC memory. This example includes the bootCount variable that is stored inside the RTC memory and counts the number of times ESP32 wakes up after every deep sleep.

RTC memory is not flashed when ESP32 is in deep sleep mode. 8kB SRAM is included inside the ESP32 RTC part, also known as RTC fast memory.

ESP32 Wake Up Reason: Next using the print_wakeup_reason() function we printed the wakeup cause from deep sleep.

In setup() part baud rate is defined for serial communication and the ++bootCount variable is incremented by 1 every time ESP32 wakes up from deep sleep. The total count is printed on the serial monitor.

Finally using the function esp_deep_sleep_start(), the ESP32 is put in sleep mode.

Upload code to ESP32 using Arduino IDE.

Output
Following output can be observed on the serial monitor of Arduino IDE. Here we can see that after every 5 sec ESP32 wakes up from deep sleep and boot number is incremented every time it wakes up.

Note: If we pressed the EN button ESP32 will reset the boot count to 0.

Conclusion

Here in this tutorial, we have configured ESP32 to wake up from deep sleep using a timer program. We have simply printed a message once the ESP32 wakes up; however, using this article one can execute any task once the ESP32 wakes up from deep sleep.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.