Skip to content

Ard Interrupt Debounce

DodaTech 1 min read

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.
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

### Why is delay() dangerous in ISRs?

delay() relies on timer interrupts (Timer0). Inside an ISR, other interrupts are disabled, so delay() never completes, hanging the program.

What debounce time should I use?

20 ms minimum for quality switches, 50 ms for generic push buttons, 100 ms for noisy environments or relays.

Can I debounce in hardware?

Yes. An RC low-pass filter (10 kOhm + 1 uF) followed by a Schmitt trigger (74HC14) provides clean debounced edges.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro