Ard Interrupt Debounce
In this tutorial, you'll learn about Arduino Interrupt Debounce Not Working. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Button interrupts are debounced in software but still show multiple triggers.
Quick Fix
Wrong
void myISR() {
delay(50); // NEVER use delay() in ISR!
}
Program crashes or behaves erratically.
Right
volatile bool pressed = false;
volatile unsigned long lastTime = 0;
void myISR() {
unsigned long now = millis();
if (now - lastTime > 50) {
pressed = true;
lastTime = now;
}
}
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), myISR, FALLING);
}
void loop() {
if (pressed) {
pressed = false;
Serial.println("Button pressed");
}
}
"Button pressed" prints exactly once per press.
Prevention
Never use delay() inside an ISR — it blocks all interrupts and crashes. Use millis() with a timestamp approach instead. Debounce time of 20-50 ms works for most mechanical switches. For noisy environments, use 100 ms. External hardware debounce (RC filter + Schmitt trigger) is more reliable.
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro