Language: EN

esp32-light-sleep

How to use Light Sleep mode on ESP32

The Light Sleep mode is one of the low power consumption modes available on the ESP32, similar to a computer’s “suspend” mode.

In this mode, the ESP32 consumes less than 1mA (for example, 800mA on the ESP32 and 240uA on the ESP32-S3)

In this mode, the CPU, RAM, and digital peripherals are disconnected from the clock and their voltage is reduced. When disconnected from the clock they stop working, but remain on and active.

When the ESP32 is in Light Sleep, it stops executing the program and enters a suspended state. To exit Light Sleep mode, the available WakeUp Sources are:

  • Timer
  • GPIO Wakeup
  • UART Wakeup
  • WIFI Wakeup
  • Touchpad
  • ULP Coprocessor Wakeup
  • External Wakeup (Ext0 and Ext1)

Upon exiting Light Sleep, the program continues execution from the same point where it was. Any information or variable stored in memory is retained.

Using Light Sleep mode in the Arduino IDE

The necessary functions to use Light Sleep mode are defined in the “esp_sleep.h” file. This library contains the functions and macros needed to configure and control the Light Sleep mode of ESP32.

With this, it is very easy to use the ESP32’s Light Sleep mode in the Arduino environment. First, define a WakeUp Source with the functions seen in What are sleep modes on ESP32.

Next, entering Light Sleep mode is as simple as using the esp_light_sleep_start function

/**
 * @brief Enter light sleep with the configured wakeup options
 */
esp_err_t esp_light_sleep_start(void);

Code Examples

Using Light Sleep mode

Let’s see a complete example of how to use Light Sleep mode to put the ESP32 into deep sleep for 10 seconds and then wake up:

void setup() 
{
    Serial.begin(115200);
    delay(5000);
}

int counter = 0;

void loop() {
  Serial.println(counter);
  counter ++;

  esp_sleep_enable_timer_wakeup(2 * 1000000); //light sleep for 2 seconds
  esp_light_sleep_start();  
}