ESP32 Button Debounce with Arduino: Count One Press with millis()

Tactile button on a breadboard beside an ESP32 board, with an illustration of a signal settling after contact bounce.

If your ESP32 counts one button press two or three times, wait for the input to stay stable before counting it. Wire a momentary button between a suitable GPIO and GND, enable INPUT_PULLUP, and use millis() to confirm each press and release while your loop keeps running. The sketch below starts with a 25 ms debounce window.

Wire the button and check the raw input

On a classic ESP32 DevKit that exposes GPIO23, connect one side of the button to GPIO23 and the other to GND. For a different board, check its pinout and change buttonPin to a pin that supports the internal pull-up. You do not need to connect the switch to a supply pin. On a four-leg tactile switch, use terminals that only connect when pressed. A continuity check with the board powered off can identify them.

With INPUT_PULLUP, an open switch reads HIGH and a pressed switch reads LOW. If the input is always LOW, check whether you used permanently connected switch terminals. If it stays HIGH while pressed, recheck the pin number and ground connection. Espressif Arduino-ESP32 GPIO reference

Debounce both edges with a millis() timer

Mechanical contacts can bounce when you press or release a button. The sketch keeps separate variables for the latest raw reading and the accepted stable level. Each change in the raw reading restarts the timer. Once the raw reading stays different from the accepted level for 25 ms, it becomes the new stable level. Only the stable transition to LOW increments the counter. Debouncing the release prevents a brief bounce from prematurely rearming the counter. Arduino's pushbutton debounce example

constexpr int buttonPin = 23;  // Example for a classic ESP32 DevKit
constexpr uint32_t debounceMs = 25;

int rawLevel;
int stableLevel;
uint32_t rawChangedAt;
unsigned long pressCount = 0;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  rawLevel = digitalRead(buttonPin);
  stableLevel = rawLevel;
  rawChangedAt = millis();
}

void loop() {
  const int sample = digitalRead(buttonPin);
  const uint32_t now = millis();

  if (sample != rawLevel) {
    rawLevel = sample;
    rawChangedAt = now;
  }

  if (rawLevel != stableLevel && now - rawChangedAt >= debounceMs) {
    stableLevel = rawLevel;
    if (stableLevel == LOW) {
      Serial.print("Press ");
      Serial.println(++pressCount);
    }
  }

  // Put other short, non-blocking work here.
}

Open Serial Monitor at 115200 baud and press the button. Holding it down should produce one line. Release it long enough to register the stable HIGH state before the next press. A button already held at startup is taken as the initial state so it does not count until released and pressed again. The unsigned subtraction in the timer comparison handles the millisecond counter wrapping for an interval this short.

Adjust the debounce window without missing taps

Try slow presses, a two-second hold, and several quick taps. If a normal press still counts twice and the wiring is sound, increase the window in small steps. If quick taps disappear, check that polling remains frequent and try a shorter window. A pulse shorter than the configured window is intentionally ignored. The debounce check uses no delay(), but a slow network call elsewhere in loop() can still prevent it from sampling the input.

What was tested. The timing logic was exercised with simulated readings for press and release bounce, holding the button, a second press, and timer wraparound. The Arduino sketch was not flashed to an ESP32 for this article.

If you later want to forward a button event to a second board, see Programming Boss’s guide to Arduino–ESP32 serial communication.