├── .gitattributes ├── ExternalWakeUp └── ExternalWakeUp.ino ├── Frequency_Counter_with_Timer_Interrupt └── Frequency_Counter_with_Timer_Interrupt.ino ├── Frequency_Counter_without_Interrupt └── Frequency_Counter_without_Interrupt.ino ├── Frequency_Counterr_with_Interrupt └── Frequency_Counterr_with_Interrupt.ino ├── README.md └── TimerWakeUp └── TimerWakeUp.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /ExternalWakeUp/ExternalWakeUp.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 0x200000000 // 2^33 in hex 25 | 26 | RTC_DATA_ATTR int bootCount = 0; 27 | 28 | /* 29 | Method to print the reason by which ESP32 30 | has been awaken from sleep 31 | */ 32 | void print_wakeup_reason(){ 33 | esp_sleep_wakeup_cause_t wakeup_reason; 34 | 35 | wakeup_reason = esp_sleep_get_wakeup_cause(); 36 | 37 | switch(wakeup_reason) 38 | { 39 | case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; 40 | case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; 41 | case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break; 42 | case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break; 43 | case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break; 44 | default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break; 45 | } 46 | } 47 | 48 | void setup(){ 49 | Serial.begin(115200); 50 | delay(1000); //Take some time to open up the Serial Monitor 51 | 52 | //Increment boot number and print it every reboot 53 | ++bootCount; 54 | Serial.println("Boot number: " + String(bootCount)); 55 | 56 | //Print the wakeup reason for ESP32 57 | print_wakeup_reason(); 58 | 59 | /* 60 | First we configure the wake up source 61 | We set our ESP32 to wake up for an external trigger. 62 | There are two types for ESP32, ext0 and ext1 . 63 | ext0 uses RTC_IO to wakeup thus requires RTC peripherals 64 | to be on while ext1 uses RTC Controller so doesnt need 65 | peripherals to be powered on. 66 | Note that using internal pullups/pulldowns also requires 67 | RTC peripherals to be turned on. 68 | */ 69 | esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low 70 | 71 | //If you were to use ext1, you would use it like 72 | //esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH); 73 | 74 | //Go to sleep now 75 | Serial.println("Going to sleep now"); 76 | esp_deep_sleep_start(); 77 | Serial.println("This will never be printed"); 78 | } 79 | 80 | void loop(){ 81 | //This is not going to be called 82 | } 83 | -------------------------------------------------------------------------------- /Frequency_Counter_with_Timer_Interrupt/Frequency_Counter_with_Timer_Interrupt.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | volatile int count = 0; 5 | volatile int frequency; 6 | hw_timer_t * timer = NULL; 7 | 8 | #define INPUTPIN 15 9 | #define OSCPIN 17 10 | 11 | // set the LCD number of columns and rows 12 | int lcdColumns = 20; 13 | int lcdRows = 4; 14 | 15 | // set LCD address, number of columns and rows 16 | // if you don't know your display address, run an I2C scanner sketch 17 | LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); 18 | 19 | portMUX_TYPE synch = portMUX_INITIALIZER_UNLOCKED; 20 | portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; 21 | 22 | 23 | void IRAM_ATTR isr() { 24 | portENTER_CRITICAL(&synch); 25 | count++; 26 | digitalWrite(OSCPIN, 1); 27 | digitalWrite(OSCPIN, 0); 28 | portEXIT_CRITICAL(&synch); 29 | } 30 | 31 | void IRAM_ATTR onTimer() { 32 | portENTER_CRITICAL_ISR(&timerMux); 33 | frequency=count; 34 | count=0; 35 | portEXIT_CRITICAL_ISR(&timerMux); 36 | 37 | } 38 | 39 | void setup() { 40 | WiFi.mode(WIFI_OFF); 41 | btStop(); 42 | lcd.init(); 43 | Serial.begin(115200); 44 | Serial.print("setup() running on core "); 45 | Serial.println(xPortGetCoreID()); 46 | // turn on LCD backlight 47 | lcd.backlight(); 48 | pinMode(INPUTPIN, INPUT_PULLUP); 49 | pinMode(OSCPIN, OUTPUT); 50 | attachInterrupt(INPUTPIN, isr, RISING); 51 | 52 | timer = timerBegin(0, 80, true); 53 | timerAttachInterrupt(timer, &onTimer, true); 54 | timerAlarmWrite(timer, 1000000, true); 55 | timerAlarmEnable(timer); 56 | } 57 | 58 | void loop() { 59 | display(0, "frequency=", frequency); 60 | delay(1000); 61 | } 62 | 63 | void display(int line, String tex, int val) { 64 | // set cursor to first column,first row 65 | 66 | lcd.setCursor(0, line); 67 | // print message 68 | lcd.print(tex); 69 | 70 | lcd.setCursor(11, line); 71 | // clear 72 | lcd.print(" "); 73 | lcd.setCursor(11, line); 74 | // set cursor to first column, second row 75 | lcd.print(String(val, DEC)); 76 | } 77 | -------------------------------------------------------------------------------- /Frequency_Counter_without_Interrupt/Frequency_Counter_without_Interrupt.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int count = 0; 4 | unsigned long lastEntry; 5 | int stat = 0; 6 | 7 | #define INPUTPIN 15 8 | #define OSCPIN 17 9 | 10 | // set the LCD number of columns and rows 11 | int lcdColumns = 20; 12 | int lcdRows = 4; 13 | 14 | // set LCD address, number of columns and rows 15 | // if you don't know your display address, run an I2C scanner sketch 16 | LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); 17 | 18 | void setup() { 19 | // initialize LCD 20 | lcd.init(); 21 | Serial.begin(115200); 22 | // turn on LCD backlight 23 | lcd.backlight(); 24 | pinMode(INPUTPIN, INPUT_PULLUP); 25 | pinMode(OSCPIN, OUTPUT); 26 | } 27 | 28 | void loop() { 29 | display(count); 30 | count = 0; 31 | lastEntry = millis(); 32 | while (millis() < lastEntry + 1000) { 33 | 34 | bool inputSignal = (digitalRead(INPUTPIN)); 35 | switch (stat) { 36 | case 0: 37 | if (inputSignal) { 38 | count++; 39 | digitalWrite(OSCPIN, 1); 40 | digitalWrite(OSCPIN, 0); 41 | stat = 1; 42 | } 43 | break; 44 | 45 | case 1: 46 | if (!inputSignal) stat = 0; 47 | break; 48 | 49 | default: 50 | break; 51 | } 52 | } 53 | } 54 | 55 | void display(int val) { 56 | lcd.clear(); 57 | // set cursor to first column,first row 58 | lcd.setCursor(0, 0); 59 | // print message 60 | lcd.print("Frequency="); 61 | 62 | // set cursor to first column, second row 63 | lcd.setCursor(11, 0); 64 | lcd.print(String(val, DEC)); 65 | } 66 | -------------------------------------------------------------------------------- /Frequency_Counterr_with_Interrupt/Frequency_Counterr_with_Interrupt.ino: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | 4 | volatile int count = 0; 5 | unsigned long lastEntry, lastDisplay; 6 | int frequency, tst; 7 | 8 | #define INPUTPIN 15 9 | #define OSCPIN 17 10 | 11 | // set the LCD number of columns and rows 12 | int lcdColumns = 20; 13 | int lcdRows = 4; 14 | 15 | // set LCD address, number of columns and rows 16 | // if you don't know your display address, run an I2C scanner sketch 17 | LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); 18 | 19 | portMUX_TYPE synch = portMUX_INITIALIZER_UNLOCKED; 20 | 21 | 22 | void IRAM_ATTR isr() { 23 | portENTER_CRITICAL(&synch); 24 | digitalWrite(OSCPIN, 1); 25 | digitalWrite(OSCPIN, 0); 26 | count++; 27 | portEXIT_CRITICAL(&synch); 28 | } 29 | 30 | 31 | void setup() { 32 | // initialize LCD 33 | lcd.init(); 34 | //WiFi.mode(WIFI_OFF); 35 | // btStop(); 36 | Serial.begin(115200); 37 | Serial.print("setup() running on core "); 38 | Serial.println(xPortGetCoreID()); 39 | // turn on LCD backlight 40 | lcd.backlight(); 41 | pinMode(INPUTPIN, INPUT_PULLUP); 42 | pinMode(OSCPIN, OUTPUT); 43 | attachInterrupt(INPUTPIN, isr, RISING); 44 | lastEntry = millis(); 45 | } 46 | 47 | void loop() { 48 | if (millis() > lastEntry + 1000) { 49 | frequency = count; 50 | display(0, "frequency=", frequency); 51 | lastEntry = millis(); 52 | count = 0; 53 | } 54 | if (millis() > lastDisplay + 500) { 55 | lastDisplay = millis(); 56 | display(2, "Other=", tst++); 57 | } 58 | } 59 | 60 | void display(int line, String tex, int val) { 61 | // set cursor to first column,first row 62 | 63 | lcd.setCursor(0, line); 64 | // print message 65 | lcd.print(tex); 66 | 67 | lcd.setCursor(11, line); 68 | // clear 69 | lcd.print(" "); 70 | lcd.setCursor(11, line); 71 | // set cursor to first column, second row 72 | lcd.print(String(val, DEC)); 73 | } 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-Interrupts-deepsleep 2 | 3 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------