├── .gitattributes ├── .gitignore ├── ESP32.xlsx ├── ExternalWakeUp_EXT0 └── ExternalWakeUp_EXT0.ino ├── ExternalWakeUp_EXT1 └── ExternalWakeUp_EXT1.ino ├── README.md ├── TimerWakeUp └── TimerWakeUp.ino └── TouchWakeUp └── TouchWakeUp.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /ESP32.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SensorsIot/ESP32-Deep-Sleep/7ae1079d1684cc4ed64076c7d45558b9db8fd699/ESP32.xlsx -------------------------------------------------------------------------------- /ExternalWakeUp_EXT0/ExternalWakeUp_EXT0.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Deep Sleep with External Wake Up 3 | ===================================== 4 | This code displays how to use deep sleep with 5 | an external trigger as a wake up source and how 6 | to store data in RTC memory to use it over reboots 7 | 8 | This code is under Public Domain License. 9 | 10 | Hardware Connections 11 | ====================== 12 | Push Button to GPIO 33 pulled down with a 10K Ohm 13 | resistor 14 | 15 | NOTE: 16 | ====== 17 | Only RTC IO can be used as a source for external wake 18 | source. They are pins: 0,2,4,12-15,25-27,32-39. 19 | 20 | Author: 21 | Pranav Cherukupalli 22 | */ 23 | 24 | RTC_DATA_ATTR int bootCount = 0; 25 | 26 | /* 27 | Method to print the reason by which ESP32 28 | has been awaken from sleep 29 | */ 30 | void print_wakeup_reason() { 31 | esp_sleep_wakeup_cause_t wakeup_reason; 32 | 33 | wakeup_reason = esp_sleep_get_wakeup_cause(); 34 | 35 | Serial.println(""); 36 | Serial.println(""); 37 | Serial.println(""); 38 | Serial.println("EXT0 Test"); 39 | 40 | switch (wakeup_reason) 41 | { 42 | case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; 43 | case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; 44 | case 3 : Serial.println("Wakeup caused by timer"); break; 45 | case 4 : Serial.println("Wakeup caused by touchpad"); break; 46 | case 5 : Serial.println("Wakeup caused by ULP program"); break; 47 | default : Serial.println("Wakeup was not caused by deep sleep"); break; 48 | } 49 | } 50 | 51 | void setup() { 52 | Serial.begin(115200); 53 | delay(1000); //Take some time to open up the Serial Monitor 54 | 55 | //Increment boot number and print it every reboot 56 | ++bootCount; 57 | Serial.println("Boot number: " + String(bootCount)); 58 | 59 | //Print the wakeup reason for ESP32 60 | print_wakeup_reason(); 61 | 62 | /* 63 | First we configure the wake up source 64 | We set our ESP32 to wake up for an external trigger. 65 | There are two types for ESP32, ext0 and ext1 . 66 | ext0 uses RTC_IO to wakeup thus requires RTC peripherals 67 | to be on while ext1 uses RTC Controller so doesnt need 68 | peripherals to be powered on. 69 | Note that using internal pullups/pulldowns also requires 70 | RTC peripherals to be turned on. 71 | */ 72 | Serial.println(esp_sleep_enable_ext0_wakeup(GPIO_NUM_39, 1)); //1 = High, 0 = Low 73 | 74 | //Go to sleep now 75 | Serial.println("Going to sleep now"); 76 | Serial.flush(); 77 | esp_deep_sleep_start(); 78 | Serial.println("This will never be printed"); 79 | } 80 | 81 | void loop() { 82 | //This is not going to be called 83 | } 84 | -------------------------------------------------------------------------------- /ExternalWakeUp_EXT1/ExternalWakeUp_EXT1.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Deep Sleep with External Wake Up 3 | ===================================== 4 | This code displays how to use deep sleep with 5 | an external trigger as a wake up source and how 6 | to store data in RTC memory to use it over reboots 7 | 8 | This code is under Public Domain License. 9 | 10 | Hardware Connections 11 | ====================== 12 | Push Button to GPIO 33 pulled down with a 10K Ohm 13 | resistor 14 | 15 | NOTE: 16 | ====== 17 | Only RTC IO can be used as a source for external wake 18 | source. They are pins: 0,2,4,12-15,25-27,32-39. 19 | 20 | Author: 21 | Pranav Cherukupalli 22 | */ 23 | 24 | //#define BUTTON_PIN_BITMASK 0x0200000000 // 2^33 in hex 25 | #define BUTTON_PIN_BITMASK 0xFF00000000 // all pins 26 | 27 | RTC_DATA_ATTR int bootCount = 0; 28 | 29 | /* 30 | Method to print the reason by which ESP32 31 | has been awaken from sleep 32 | */ 33 | void print_wakeup_reason() { 34 | esp_sleep_wakeup_cause_t wakeup_reason; 35 | 36 | wakeup_reason = esp_sleep_get_wakeup_cause(); 37 | 38 | Serial.println(""); 39 | Serial.println(""); 40 | Serial.println("EXT1 Test"); 41 | 42 | switch (wakeup_reason) 43 | { 44 | case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; 45 | case 2 : { 46 | Serial.print("Wakeup caused by external signal using RTC_CNTL "); 47 | Serial.println((uint32_t)esp_sleep_get_ext1_wakeup_status(),HEX); 48 | print64(BUTTON_PIN_BITMASK); 49 | Serial.println(); 50 | 51 | break; 52 | } 53 | case 3 : Serial.println("Wakeup caused by timer"); break; 54 | case 4 : Serial.println("Wakeup caused by touchpad"); break; 55 | case 5 : Serial.println("Wakeup caused by ULP program"); break; 56 | default : Serial.println("Wakeup was not caused by deep sleep"); break; 57 | } 58 | } 59 | 60 | void print64(uint64_t number) { 61 | for (int i = 0; i < 64; i++) { 62 | bool bitt = number & 0x8000000000000000; 63 | Serial.print(bitt); 64 | number = number << 1; 65 | } 66 | } 67 | 68 | void setup() { 69 | Serial.begin(115200); 70 | delay(1000); //Take some time to open up the Serial Monitor 71 | 72 | //Increment boot number and print it every reboot 73 | ++bootCount; 74 | Serial.println("Boot number: " + String(bootCount)); 75 | 76 | //Print the wakeup reason for ESP32 77 | print_wakeup_reason(); 78 | 79 | /* 80 | First we configure the wake up source 81 | We set our ESP32 to wake up for an external trigger. 82 | There are two types for ESP32, ext0 and ext1 . 83 | ext0 uses RTC_IO to wakeup thus requires RTC peripherals 84 | to be on while ext1 uses RTC Controller so doesnt need 85 | peripherals to be powered on. 86 | Note that using internal pullups/pulldowns also requires 87 | RTC peripherals to be turned on. 88 | */ 89 | 90 | //If you were to use ext1, you would use it like 91 | Serial.println(esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH)); 92 | 93 | //Go to sleep now 94 | Serial.println("Going to sleep now"); 95 | Serial.flush(); 96 | esp_deep_sleep_start(); 97 | Serial.println("This will never be printed"); 98 | } 99 | 100 | void loop() { 101 | //This is not going to be called 102 | } 103 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-Deep-Sleep 2 | Material for video #149 3 | 4 | 5 | Video: 6 | -------------------------------------------------------------------------------- /TimerWakeUp/TimerWakeUp.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Simple Deep Sleep with Timer Wake Up 3 | ===================================== 4 | ESP32 offers a deep sleep mode for effective power 5 | saving as power is an important factor for IoT 6 | applications. In this mode CPUs, most of the RAM, 7 | and all the digital peripherals which are clocked 8 | from APB_CLK are powered off. The only parts of 9 | the chip which can still be powered on are: 10 | RTC controller, RTC peripherals ,and RTC memories 11 | 12 | This code displays the most basic deep sleep with 13 | a timer to wake it up and how to store data in 14 | RTC memory to use it over reboots 15 | 16 | This code is under Public Domain License. 17 | 18 | Author: 19 | Pranav Cherukupalli 20 | */ 21 | 22 | #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ 23 | #define TIME_TO_SLEEP 15 /* Time ESP32 will go to sleep (in seconds) */ 24 | 25 | RTC_DATA_ATTR int bootCount = 0; 26 | 27 | /* 28 | Method to print the reason by which ESP32 29 | has been awaken from sleep 30 | */ 31 | void print_wakeup_reason(){ 32 | esp_sleep_wakeup_cause_t wakeup_reason; 33 | 34 | wakeup_reason = esp_sleep_get_wakeup_cause(); 35 | 36 | switch(wakeup_reason) 37 | { 38 | case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; 39 | case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; 40 | case 3 : Serial.println("Wakeup caused by timer"); break; 41 | case 4 : Serial.println("Wakeup caused by touchpad"); break; 42 | case 5 : Serial.println("Wakeup caused by ULP program"); break; 43 | default : Serial.println("Wakeup was not caused by deep sleep"); break; 44 | } 45 | } 46 | 47 | void setup(){ 48 | Serial.begin(115200); 49 | delay(1000); //Take some time to open up the Serial Monitor 50 | 51 | //Increment boot number and print it every reboot 52 | ++bootCount; 53 | Serial.println("Boot number: " + String(bootCount)); 54 | 55 | //Print the wakeup reason for ESP32 56 | print_wakeup_reason(); 57 | 58 | /* 59 | First we configure the wake up source 60 | We set our ESP32 to wake up every 5 seconds 61 | */ 62 | esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); 63 | Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + 64 | " Seconds"); 65 | 66 | /* 67 | Next we decide what all peripherals to shut down/keep on 68 | By default, ESP32 will automatically power down the peripherals 69 | not needed by the wakeup source, but if you want to be a poweruser 70 | this is for you. Read in detail at the API docs 71 | http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html 72 | Left the line commented as an example of how to configure peripherals. 73 | The line below turns off all RTC peripherals in deep sleep. 74 | */ 75 | //esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); 76 | //Serial.println("Configured all RTC Peripherals to be powered down in sleep"); 77 | 78 | /* 79 | Now that we have setup a wake cause and if needed setup the 80 | peripherals state in deep sleep, we can now start going to 81 | deep sleep. 82 | In the case that no wake up sources were provided but deep 83 | sleep was started, it will sleep forever unless hardware 84 | reset occurs. 85 | */ 86 | Serial.println("Going to sleep now"); 87 | Serial.flush(); 88 | esp_deep_sleep_start(); 89 | Serial.println("This will never be printed"); 90 | } 91 | 92 | void loop(){ 93 | //This is not going to be called 94 | } 95 | -------------------------------------------------------------------------------- /TouchWakeUp/TouchWakeUp.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Deep Sleep with Touch Wake Up 3 | ===================================== 4 | This code displays how to use deep sleep with 5 | a touch as a wake up source and how to store data in 6 | RTC memory to use it over reboots 7 | 8 | This code is under Public Domain License. 9 | 10 | Author: 11 | Pranav Cherukupalli 12 | */ 13 | 14 | #define Threshold 30 /* Greater the value, more the sensitivity */ 15 | 16 | RTC_DATA_ATTR int bootCount = 0; 17 | touch_pad_t touchPin; 18 | /* 19 | Method to print the reason by which ESP32 20 | has been awaken from sleep 21 | */ 22 | void print_wakeup_reason(){ 23 | esp_sleep_wakeup_cause_t wakeup_reason; 24 | 25 | wakeup_reason = esp_sleep_get_wakeup_cause(); 26 | 27 | switch(wakeup_reason) 28 | { 29 | case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; 30 | case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; 31 | case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break; 32 | case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break; 33 | case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break; 34 | case ESP_SLEEP_WAKEUP_UNDEFINED : Serial.println("Wakeup caused is undefined"); break; 35 | default : Serial.println("Wakeup was not caused by deep sleep"); break; 36 | } 37 | } 38 | 39 | /* 40 | Method to print the touchpad by which ESP32 41 | has been awaken from sleep 42 | */ 43 | void print_wakeup_touchpad(){ 44 | touch_pad_t pin; 45 | 46 | touchPin = esp_sleep_get_touchpad_wakeup_status(); 47 | 48 | switch(touchPin) 49 | { 50 | case 0 : Serial.println("Touch detected on GPIO 4"); break; 51 | case 1 : Serial.println("Touch detected on GPIO 0"); break; 52 | case 2 : Serial.println("Touch detected on GPIO 2"); break; 53 | case 3 : Serial.println("Touch detected on GPIO 15"); break; 54 | case 4 : Serial.println("Touch detected on GPIO 13"); break; 55 | case 5 : Serial.println("Touch detected on GPIO 12"); break; 56 | case 6 : Serial.println("Touch detected on GPIO 14"); break; 57 | case 7 : Serial.println("Touch detected on GPIO 27"); break; 58 | case 8 : Serial.println("Touch detected on GPIO 33"); break; 59 | case 9 : Serial.println("Touch detected on GPIO 32"); break; 60 | default : Serial.println("Wakeup not by touchpad"); break; 61 | } 62 | } 63 | 64 | void callback2(){ 65 | //placeholder callback function 66 | } 67 | 68 | void callback3(){ 69 | //placeholder callback function 70 | } 71 | 72 | void setup(){ 73 | Serial.begin(115200); 74 | delay(1000); //Take some time to open up the Serial Monitor 75 | 76 | //Increment boot number and print it every reboot 77 | ++bootCount; 78 | Serial.println("Boot number: " + String(bootCount)); 79 | 80 | //Print the wakeup reason for ESP32 and touchpad too 81 | print_wakeup_reason(); 82 | print_wakeup_touchpad(); 83 | 84 | //Setup interrupt on Touch Pad 3 (GPIO15) 85 | touchAttachInterrupt(T3, callback3, Threshold); 86 | touchAttachInterrupt(T2, callback2, Threshold); 87 | 88 | //Configure Touchpad as wakeup source 89 | esp_sleep_enable_touchpad_wakeup(); 90 | 91 | //Go to sleep now 92 | Serial.println("Going to sleep now"); 93 | Serial.flush(); 94 | esp_deep_sleep_start(); 95 | Serial.println("This will never be printed"); 96 | } 97 | 98 | void loop(){ 99 | //This will never be reached 100 | } 101 | --------------------------------------------------------------------------------