Skip to content

Ard Interrupt Detach

DodaTech 1 min read

In this tutorial, you'll learn about Arduino Interrupt Continues After detachInterrupt. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

The ISR keeps running even after calling detachInterrupt().

Quick Fix

Wrong

detachInterrupt(0);  // But ISR may still fire if flag is pending
ISR fires one more time after detach.
volatile bool processFlag = false;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), myISR, FALLING);
}

void loop() {
  if (processFlag) {
    processFlag = false;
    detachInterrupt(digitalPinToInterrupt(2));
    Serial.println("Interrupt detached");
    // Process data here
    attachInterrupt(digitalPinToInterrupt(2), myISR, FALLING);
  }
}

void myISR() {
  processFlag = true;
}
"Interrupt detached" prints. No further ISR calls after detach.

Prevention

Pending interrupt flags may trigger one more ISR after detachInterrupt(). The ISR flag is cleared after execution. Re-attach interrupts only after processing is complete. Use a flag pattern to handle one-shot interrupts cleanly.

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

FAQ

### Can I re-attach after detach?

Yes. Call attachInterrupt() again with the same parameters to re-enable the interrupt. There is no limit on re-attachment.

Does detachInterrupt clear pending flags?

No. Pending interrupt flags in the hardware remain. They are cleared by the ISR execution. Call detachInterrupt() after the flag is processed.

Should I disable interrupts globally during critical sections?

Use noInterrupts() and interrupts() for critical sections. detachInterrupt() is per-pin, noInterrupts() disables all interrupts globally.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro