20  Deep Sleep

Deep sleep is a power-saving mode on the ESP32 microcontroller that allows it to significantly reduce its power consumption by shutting down most of its components and functionalities. This mode is particularly useful for battery-powered applications where conserving energy is crucial.

20.1 Key Features of Deep Sleep

  1. Low Power Consumption:
    In deep sleep mode, the ESP32 consumes only a few microamps of current, drastically reducing power usage compared to its normal operating modes.
  2. Preservation of Data:
    The ESP32 can save the state of its peripherals and the values of certain variables in RTC (Real-Time Clock) memory, which is retained during deep sleep. This allows the microcontroller to resume tasks efficiently upon waking up.
  3. Wake-Up Sources:
    The ESP32 can be awakened from deep sleep by various events:
    • Timer: The internal RTC can be programmed to wake the device after a specified time interval.
    • External Pin: A change in the state of a designated GPIO pin can trigger a wake-up.
    • Touch Sensor: Touch pad sensors can be used to wake up the device.
    • ULP Coprocessor: The Ultra-Low-Power (ULP) coprocessor can run while the main CPU is in deep sleep and trigger a wake-up event based on sensor readings or other conditions.
  4. Application Examples:
    • Battery-Powered Sensors: Environmental sensors that need to periodically send data can spend most of their time in deep sleep, waking up only to take measurements and transmit data.
    • IoT Devices: Devices that need to conserve power, such as remote weather stations or smart home devices, can use deep sleep to extend battery life.

20.2 Deep Sleep code

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


void setup(){
  Serial.begin(115200);
  Serial.println("Good morning!");
  delay(2000); //Take some time to open up the Serial Monitor

  // turn on LED
  Serial.println("turn on LED");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000); //Take some time to open up the Serial Monitor

  /*
  First we configure the wake up source
  We set our ESP32 to wake up every TIME_TO_SLEEP 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");

  
  Serial.println("Going to sleep now");
  Serial.flush(); 
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

void loop(){
  //This is not going to be called
}