Interupt ESP32 wake up from sleep

Hi

I use an ESP32 Wroom for a scale.
After reading scale the system goes to sleep in 15 minutes
For info, no void loop in my code due to esp_sleep

I need an interrupt in my code to wake eps32 and start calibration process.

I tried this code with no success:

#include <esp_sleep.h>
const int switchCalibrate = 36;

void setup() {
  Serial.begin(115200); delay(500);
  pinMode(switchCalibrate, INPUT_PULLUP);
  // Attach interrupt to GPIO 36, set it to trigger on a rising edge
  attachInterrupt(digitalPinToInterrupt(switchAlarm), alarmWakeUp, RISING);  
  Serial.println("Setup loop done, go to sleep");
 
  // ENABLE SLEEP TIMER AND GO TO SLEEP
  esp_sleep_enable_timer_wakeup(900* 1000000);                // 15 minutes
  esp_deep_sleep_start();

void alarmWakeUp() {
  // This function will be called when the interrupt is triggered
  Serial.println("Calibration starts");
  calibrate();
}

void loop(){
}

Can you have any tip?

M

You make the following request to ChatGPT; in responese, you will receive fantastic hints/codes which you may tailor to create a working sketch. If you can't manage to make it work, please come back to the Forum with your specific issue. (You need loop() function in your sketch.)

Sleep Mode operation of ESP32 and wakeup by external interrupt.

Forgot to past the loop, anyway, it does not work.

can something with:

esp_sleep_enable_ext0_wakeup(GPIO_NUM_36,1); //1 = High, 0 = Low

work?

May I assume that you are reluctatnt to consult ChatGPT to adjust your sketch of post #1?

1 Like

Not reluctant:

Here is after i added an if statement and reviced the code at chatgpt

#include <esp_sleep.h>

const int switchCalibrate = 35;

void setup() {
  Serial.begin(115200);
  delay(500);
  pinMode(switchCalibrate, INPUT_PULLUP);

  // check if switchCalibrate is pressed
  if (digitalRead(switchCalibrate) == LOW) {
    Serial.println("Button pushed");
  }

  // Attach interrupt to GPIO 35, set it to trigger on a falling edge
  attachInterrupt(digitalPinToInterrupt(switchCalibrate), alarmWakeUp, FALLING);
  Serial.println("Setup loop done, go to sleep");

  // ENABLE SLEEP TIMER AND GO TO SLEEP
  esp_sleep_enable_timer_wakeup(10 * 1000000); // 
  esp_deep_sleep_start();
}

void alarmWakeUp() {
  // This function will be called when the interrupt is triggered
  Serial.println("Calibration starts");
  // Perform any calibration logic here
}

void loop() {
  // Your main code here
}

Wake up did not kick in when push button once in sleep mode

Serial:

Pushed button once in sleep mode, no calibrate:

18:39:28.548 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
18:39:28.548 -> configsip: 0, SPIWP:0xee
18:39:28.548 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
18:39:28.548 -> mode:DIO, clock div:1
18:39:28.548 -> load:0x3fff0030,len:1344
18:39:28.548 -> load:0x40078000,len:13964
18:39:28.548 -> load:0x40080400,len:3600
18:39:28.548 -> entry 0x400805f0
18:39:29.079 -> Setup loop done, go to sleep
18:39:39.078 -> ets Jun 8 2016 00:22:57
18:39:39.078 ->

Now hold button after wakeup:

18:39:39.078 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
18:39:39.078 -> configsip: 0, SPIWP:0xee
18:39:39.078 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
18:39:39.078 -> mode:DIO, clock div:1
18:39:39.078 -> load:0x3fff0030,len:1344
18:39:39.078 -> load:0x40078000,len:13964
18:39:39.078 -> load:0x40080400,len:3600
18:39:39.078 -> entry 0x400805f0
18:39:39.595 -> Button pushed
18:39:39.595 -> Setup loop done, go to sleep

I tried this:
esp_sleep_enable_ext1_wakeup(BIT(switchCalibrate), ESP_EXT1_WAKEUP_ANY_HIGH);

No change

You may try the tutorial of the following link: (I have not tested.)
ESP32 Deep Sleep with Arduino IDE and Wake Up Sources | Random Nerd Tutorials.

1 Like

I tried the random neard, it seems to work, but it reports pin 37 to wake up, 37 is not used so probably need to be set low (or high):

Is there an option to set pint 37 low in the sketch?

in tutorial:

First we configure the wake up source
We set our ESP32 to wake up for an external trigger.
There are two types for ESP32, ext0 and ext1 .
ext0 uses RTC_IO to wakeup thus requires RTC peripherals
to be on while ext1 uses RTC Controller so doesnt need
peripherals to be powered on.
Note that using internal pullups/pulldowns also requires
RTC peripherals to be turned on.
// pin 35 and 36 calibrate and tare buttons:
//2^35 + 2^36 = 103079215104 = hex 1800000000

#define BUTTON_PIN_BITMASK 0x1800000000 // GPIOs 35 and 36

RTC_DATA_ATTR int bootCount = 0;
const int sleepTimeSec      = 10;


void setup() {
  Serial.begin(115200);
  delay(500); //Take some time to open up the Serial Monitor

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32
  print_wakeup_reason();

  //Print the GPIO used to wake up
  print_GPIO_wake_up();

  /*
    First we configure the wake up source
    We set our ESP32 to wake up for an external trigger.
    There are two types for ESP32, ext0 and ext1 .
    ext0 uses RTC_IO to wakeup thus requires RTC peripherals
    to be on while ext1 uses RTC Controller so doesnt need
    peripherals to be powered on.
    Note that using internal pullups/pulldowns also requires
    RTC peripherals to be turned on.
  */
  //esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_15,1); //1 = High, 0 = Low

  //If you were to use ext1, you would use it like
  esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH);

  //Go to sleep now

  esp_sleep_enable_timer_wakeup(sleepTimeSec * 1000000);                      // Convert seconds to microseconds
  Serial.println("Sleep in " + String(sleepTimeSec) + " sekunder");
  Serial.println("--------------------------------------");
  esp_deep_sleep_start();

  Serial.println("This will never be printed");
}

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

/*
  Method to print the reason by which ESP32
  has been awaken from sleep
*/
void print_wakeup_reason() {
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason)
  {
    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;
  }
}

/*
  Method to print the GPIO that triggered the wakeup
*/
void print_GPIO_wake_up() {
  uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
  Serial.print("GPIO that triggered the wake up: GPIO ");
  Serial.println((log(GPIO_reason)) / log(2), 0);
}

Serial output reports GPIO37, not 35 and 36:

ets Jun 8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1344
load:0x40078000,len:13964
load:0x40080400,len:3600
entry 0x400805f0
Boot number: 128 -----------------------------------------------------------> no delays, it restarts again and again
Wakeup caused by external signal using RTC_CNTL
GPIO that triggered the wake up: GPIO 37
Sleep 10 sekunder

Hi

I have tested this "random nerd" code, it wakes up but I need to know witch button was pressed to make if sentences do different jobs according to button pressed.

Any tip to make code reply with witch button pressed?

Code is for 1 of 3 buttons, gpio34

Serial print gives
>GPIO that triggered the wake up: GPIO inf
Witch is not what I expected

Schematic:
image

Testing 34:
image

Code:

/*
  https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/#:~:text=You%20can%20decide%20what%20peripherals,ESP32%20into%20deep%20sleep%20mode

  Deep Sleep with External Wake Up
  =====================================
  This code displays how to use deep sleep with
  an external trigger as a wake up source and how
  to store data in RTC memory to use it over reboots
  This code is under Public Domain License.
  Hardware Connections
  ======================
  Push Button to GPIO 34 pulled up with a 10K Ohm resistor
  NOTE:
  ======
  Only RTC IO can be used as a source for external wake
  source. They are pins: 0,2,4,12-15,25-27,32-39.
  Author:  Pranav Cherukupalli <cherukupallip@gmail.com>
*/

#include <esp_sleep.h>

// All RTC IO pins used, 3 buttons, high when not pressed, aka Falling Edge
// GPIOs 4(alarm), 34(tare) and 35(calibrate) 

//  GPIO   Dec         HEX 9 places
//  2^4    16          000000010
//  2^34   17179869184 400000000
//  2^35   34359738368 800000000
//  SUM    51539607568 C00000010

//Only gpio34 for test reason
#define BUTTON_PIN_BITMASK 0x400000000    //  2^4=10   2^34=400000000, all three:C00000010 

void setup() {
  Serial.begin(115200);
  delay(50); //Take some time to open up the Serial Monitor
  print_wakeup_reason(); //Print the wakeup reason for ESP32
  print_GPIO_wake_up();  //Print the GPIO used to wake up
  
  /*
    First we configure the wake up source
    We set our ESP32 to wake up for an external trigger.
    There are two types for ESP32, ext0 and ext1 .
    ext0 uses RTC_IO to wakeup thus requires RTC peripherals
    to be on while ext1 uses RTC Controller so doesnt need
    peripherals to be powered on.
    Note that using internal pullups/pulldowns also requires
    RTC peripherals to be turned on.
  */
  //esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_34,0); //1 = High, 0 = Low (falling edge)

  esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ALL_LOW);   
  
  //Go to sleep now
  Serial.println("Sleep");
  Serial.println("-------------------------");
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

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

/*
  Method to print the reason by which ESP32
  has been awaken from sleep
*/

void print_wakeup_reason() {
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason)
  {
    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;
  }
}

/*
  Method to print the GPIO that triggered the wakeup
*/
void print_GPIO_wake_up() {
  uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
  Serial.print("GPIO that triggered the wake up: GPIO ");
  Serial.println((log(GPIO_reason)) / log(2), 0);
  Serial.println(GPIO_reason);
}

Serial, wake up by button connected to gpio34:

/*
08:19:11.809 -> Sleep
08:19:11.809 -> -------------------------

08:19:11.809 -> ets Jun 8 2016 00:22:57
08:19:11.809 ->
08:19:11.809 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
08:19:11.809 -> configsip: 0, SPIWP:0xee
08:19:11.809 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
08:19:11.809 -> mode:DIO, clock div:1
08:19:11.809 -> load:0x3fff0030,len:1344
08:19:11.809 -> load:0x40078000,len:13964
08:19:11.809 -> load:0x40080400,len:3600
08:19:11.809 -> entry 0x400805f0

08:19:11.903 -> Wakeup caused by external signal using RTC_CNTL
08:19:11.903 -> GPIO that triggered the wake up: GPIO inf
08:19:11.903 -> 0
08:19:11.903 -> Sleep

*/type or paste code here

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.