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)
Right
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
← Previous
ESP32 eFuse MAC Address Read Failure
Next →
ESP32 GPIO Input with Pull-Up Reads Wrong Values
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro