Search code examples
esp32arduino-ide

Deep Sleep with EXT0 or EXT1 on ESP32-C3-MINI-1


I am trying to implement code for a EXT0 and/or EXT1 wake-up strategy for the ESP32-C3-MINI-1. I am using the Wokwi simulator to test since I have not yet received the Dev kit. However, I am unable to even get the board to wake-up from EXT0 strategy let alone EXT1. I am not sure what is wrong within the code. Any help is appreciated, thank you!

Pic of Breadboard setup & Outputs generated

#define INTERRUPT_PIN 2
RTC_DATA_ATTR int bootCount = 0;

void setup() {
  Serial.begin(115200);
  delay(1000);

  //pinMode(LED, OUTPUT);
 // digitalWrite(LED, LOW);

  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  print_wakeup_reason();

  Serial.println("Starting");
  delay(2000);
  
  for(int i=3; i!=0; i--){
      Serial.println(i);
      delay(1000);
  }

  Serial.println("Going to sleep now");

  esp_deep_sleep_enable_gpio_wakeup(1 << INTERRUPT_PIN,
    ESP_GPIO_WAKEUP_GPIO_HIGH);
  esp_deep_sleep_start();

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

void loop() { }

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;
  }
}

I have looked through multiple forums that use the EXT0 strategy as a wake-up but have found no luck in getting my own code to work. I would really appreciate any help in trying to get this push button to wake-up the esp32-c3. As of now, the code successfully puts the esp32-c3 into deep sleep, but fails to interpret the button being pushed.


Solution

  • I'm curious enough to try your sketch and test it myself after seeing your comment saying what I suggested doesn't work. I test it on my custom ESP32-C3 board.

    First, GPIO2 is one of the those strapping pins, which has an external pull-up resistor and also likely connect to the blinking LED, so you should connect the button between the pin and GND instead as shown in your connection diagram.

    Secondly, wakeup by ext0 and ext1 is not available for ESP32-C3, but esp_deep_sleep_enable_gpio_wakeup() should work for RTC pin GPIO0 - GPIO5.

    When using GPIO pin other than GPIO2 (which already has a pull-up resistor), it requires an external pull-up resistor or setup the pin as pinMode(WAKEUP_PIN, INPUT_PULLUP);.

    I tested the following code on GPIO2 (and other pin with pull-up mode) and it work.

    #define WAKEUP_PIN 2
    RTC_DATA_ATTR int bootCount = 0;
    
    void setup() {
      // pinMode(WAKEUP_PIN, INPUT_PULLUP);  // when the pin does not have external pull-up resistor
    
      Serial.begin(115200);
      delay(1000);
    
      print_wakeup_reason();
    
      Serial.printf("Going to sleep, to be wakeup by GPIO %d\n", WAKEUP_PIN); 
      delay(3000);
    
      esp_deep_sleep_enable_gpio_wakeup(1 << WAKEUP_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
      esp_deep_sleep_start();
    
      Serial.println("This will never be printed");
    }
    
    void loop() { }
    
    void print_wakeup_reason() {
    
      Serial.printf("Boot number: %d\n", ++bootCount);
    
      esp_sleep_wakeup_cause_t 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_GPIO:    // 05 - this is used by ESP32-C3 as EXT0/EXT1 does not available in C3
          Serial.println("Wakeup by GPIO");
          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;
      }
    }
    

    Reference:

    Sleep Mode (ESP32-C3)