Hi there,
5ven and welcome,
I have this code working with a button on D0, Blinks 10 times , Sleeps and wakes up If you press the button.
Here’s the code and the Output and a picture of the hardware setup
#define LED 10 // or use LED_BUILTIN for on-board LED
#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("Going to blink the LED 10 times");
blink(10); // Allow time for wakeup
Serial.println("Going to sleep now");
delay (250);
// esp_sleep_enable_bt_wakeup(void)
esp_deep_sleep_enable_gpio_wakeup(1 << INTERRUPT_PIN, ESP_GPIO_WAKEUP_GPIO_HIGH);
delay (250);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() { }
void blink(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
Serial.print("Blink ");
Serial.println(i + 1);
}
}
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;
}
}
The Serial Out.
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4200f3a4
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5810,len:0x438
load:0x403cc710,len:0x918
load:0x403ce710,len:0x25f4
entry 0x403cc710
Boot number: 1
Wakeup was not caused by deep sleep: 0
Going to blink the LED 10 times
Blink 1
Blink 2
Blink 3
Blink 4
Blink 5
Blink 6
Blink 7
Blink 8
Blink 9
Blink 10
Going to sleep now
push the button it wakes up.
HTH
GL PJ