The following code works. It does the following:

  • wake up when pin GPIO0 is connected to GND (the pulse duration is 400 milliseconds)
  • once every 10 wakings, I send the current index to a server with a HTTP request over WiFi
  • then go to deep sleep

Since GPIO0 is connected to GND during 400 milliseconds, it can reboot multiple times during these 400 milliseconds, which I don't want. So I added delay(500); at the end of the code.

Problem: this delay(500); is probably a waste of power, since the ESP32 is battery powered.

Question: by what could I replace this delay(500) to avoid that the ESP32 reboots multiple times while pin 0 is connected to GND during 400 milliseconds, with the least power consumption possible?

#include "WiFi.h"
#include "HTTPClient.h"
#include "esp_sleep.h"
byte WIFI_BSSID[] =         {0x01, 0x23, 0x45, 0x67, 0x89, 0x00};
String URL =                "http://example.com/submit?bootcount=";
RTC_DATA_ATTR int bootcount = 0;
HTTPClient http;
void setup() {
    pinMode(0, INPUT_PULLUP);
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_0, 0);
    if (bootcount % 10 == 0) {
        WiFi.begin("MySSID", "password", 0, WIFI_BSSID);
        while (WiFi.status() != WL_CONNECTED) {
        URL += bootcount;
void loop() {
  • it happens during 400 milliseconds ... what does this mean? ... does the pin interrupt happen every 400 ms?
    – jsotola
    Commented Dec 13, 2022 at 21:01
  • @jsotola It's like a switch: usually pin 0 is not connected to GND. Then when a pulse happens, during 400 ms pin 0 is connected to GND, it's the pulse duration.
    – Basj
    Commented Dec 13, 2022 at 21:04
  • so, you want the ESP32 to wake only on the transition of GPIO0 ... perhaps a simple circuit could be used to modify the input pulse ... maybe the ULP processor could be used
    – jsotola
    Commented Dec 13, 2022 at 22:40
  • @jsotola Yes, since the pulse lasts 400ms, if I don't add the delay(500), multiple "wake_up+increment_bootcount+deep_sleep" sequences can happen during one signe pulse. I want to wake up only on the transition. Do you think there is a purely software way (without additional hardware components), other than my delay(500) solution? And possibly that doesn't require to stay fully powered during the 500ms?
    – Basj
    Commented Dec 13, 2022 at 23:03
  • If the chip cannot be waked by the transition, you might consider to use a timer. Start it when the delay shall start, disable the pin interrupt, and go to sleep. Let the timer overflow/interrupt wake up the chip again, stop the timer, enable the pin interrupt, and go to sleep. Please be aware that a running timer might not allow a deep sleep. Commented Dec 14, 2022 at 10:17


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.