Skip to content

ESP32 GPIO Button Debounce Not Working

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 GPIO Button Debounce Not Working. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 reading a mechanical button registers multiple presses due to contact bounce.

Quick Fix

Wrong

if (digitalRead(4) == LOW) {
  count++;  // Increments many times per single press
}
count = 7 (single press registers as 7 presses due to bounce)
const int debounceDelay = 50;
unsigned long lastDebounce = 0;
int lastState = HIGH;
void loop() {
  int reading = digitalRead(4);
  if (reading != lastState) lastDebounce = millis();
  if ((millis() - lastDebounce) > debounceDelay) {
    if (reading == LOW) {
      count++;
      Serial.printf("Press #%d\n", count);
    }
  }
  lastState = reading;
}
Press #1
Press #2
Press #3
(One increment per physical press)

Prevention

Use 50ms debounce delay for mechanical buttons. Track state change time with millis(). Add a hardware capacitor (100nF) for better noise immunity. Use a state machine for complex button handling (long press, double-click).

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What debounce delay should I use?

Mechanical buttons typically bounce for 5-20ms. 50ms is safe for most buttons. Use 100ms for old or worn switches.

Does hardware debounce replace software?

Hardware (RC filter + Schmitt trigger) is more reliable. Software can handle most cases. Combine both for critical applications.

Can I debounce in an ISR?

Yes, but set a flag and handle timing in loop(). ISRs should be as short as possible. Do not use millis() in ISR for timing.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro