Example Code for Arduino-Deep-sleep Mode

Last revision 2026/01/31

This article offers example code for putting FireBeetle 2 ESP32-E in deep-sleep mode with wake-up methods, optimizing Arduino power management.

Hardware Preparation

Other Preparation Work

This section describes how to put the FireBeetle 2 ESP32-E into low power Deep_sleep mode through Arduino code.

The ESP32 deep_sleep mode wake-up methods are as follows:

  • Timer wake-up
  • Two-pin wake-up method
  • Touch button wake-up
  • ULP wake-up
  • Return: Wake-up reason code
Reason Code Reason Description
2 ESP_SLEEP_WAKEUP_EXT0 Wake-up by RTC_GPIO
3 ESP_SLEEP_WAKEUP_EXT1 Wake-up by change in RTC_CNTL pin set
4 ESP_SLEEP_WAKEUP_TIMER Wake-up by ESP timer
5 ESP_SLEEP_WAKEUP_TOUCHPAD Wake-up by touch
6 ESP_SLEEP_WAKEUP_ULP Wake-up by ULP (Ultra Low Power)

Sample Code

Set FireBeetle 2 ESP32-E to enter deep sleep mode, with timer as wake up source, and wake up every 5 seconds.

#define uS_TO_S_FACTOR 1000000ULL   // Conversion factor from microseconds to seconds
#define TIME_TO_SLEEP  5             // Time for ESP32-E to enter deep sleep
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) {             // Check the wake-up reason and print the corresponding message
    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\n",wakeup_reason); break;
  }
}

void setup(){
  Serial.begin(115200);    
  delay(1000); 
  ++bootCount;             
  Serial.println("Boot number: " + String(bootCount));   
  print_wakeup_reason();   // Print the wake-up 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");  // We have set the wake up reason. Now we can start go to sleep of the peripherals need to be in deep sleep. If no wake-up source is provided, but deep sleep is initiated, it will sleep forever unless a hardware reset occurs.
  Serial.flush(); 
  esp_deep_sleep_start();   
  Serial.println("This will never be printed");  
}

void loop(){
}

Result

DFR0654-S Deep-sleep Mode

Was this article helpful?

TOP