Why should I call detachInterrupt at the start of an interrupt service routine? 

 September 29, 2018

By  Peter

To understand the explanation below, please consider this example sketch. This sketch is used to debounce a rotary encoder:

(Original is sourced from the Arduino Step by Step Your Complete Guide Github repository)

//Original sketch: https://bigdanzblog.wordpress.com/2014/08/16/using-a-ky040-rotary-encoder-with-arduino/
//Modified by Peter Dalmaris, July 2015
const int PinCLK=2;                   // Used for generating interrupts using CLK signal
const int PinDT=3;                    // Used for reading DT signal
const int PinSW=8;                    // Used for the push button switch
volatile long     virtualPosition  =0;  // must be volatile to work with the isr
void isr0 ()  {
  if (!digitalRead(PinDT))
  attachInterrupt (0,isr0,RISING);
  } // ISR0
void setup ()  {
  attachInterrupt (0,isr0,RISING);   // interrupt 0 is always connected to pin 2 on Arduino UNO
  Serial.begin (9600);
void loop ()  {
  int lastCount = 0;
  while (true) {
      if (!(digitalRead(PinSW))) {        // check if pushbutton is pressed
        virtualPosition = 0;            // if YES, then reset counter to ZERO
        while (!digitalRead(PinSW)) {}  // wait til switch is released
      if (virtualPosition != lastCount) {
        lastCount = virtualPosition;
   } // while

When an interrupt “fires”, the Arduino goes into the interrupt service routine (isr).

Imagine that the Arduino is already executing code in the isr, when another interrupt fires. What do you think it is that the Arduino will do next?

The Arduino will stop what its doing and jump back at the start of the ISR.

But that’s not what we want. Normally, the ISR function contains code that is critical to our application, and we want that code to complete its execution always, regardless if another interrupt arrives.

To prevent repeated calls to the ISR before a previous ISR has finished, we must turn off interrupts as soon as one is received. That is what we do in line 12 with the “detachInterrupt” function.

But then, just before the Arduino finished the execution of the ISR, we turn interrupts back on so that the next interrupt is captured. Thats what the call to attachInterrupt does in line 23.


Peter Dalmaris is an educator, electrical engineer, electronics hobbyist, and Maker. Creator of online video courses on DIY electronics and author of three technical books, and has recently released his book Maker Education Revolution.   As a Chief Tech Explorer since 2013 at Tech Explorations, the company he founded in Sydney, Australia, Peter’s mission is to explore technology and help educate the world.  Tech Explorations offers educational courses and Bootcamps for electronics hobbyists, STEM students and STEM teachers. A life-long learner, Peter’s core skill is in explaining difficult concepts through video and text. With over 15 years of tertiary teaching experience, Peter has developed a simple yet comprehensive style in teaching that students from all around the world appreciate.  His passion for technology and in particular for the world of DIY open source hardware has been a dominant driver that has guided his personal development and his work through Tech Explorations. Peter’s current online courses have helped over 60,000 people from around the world to be better Makers. 

Peter Dalmaris

    • Hi David, this is not a feature specific to the Arduino (or the Atmega microcontrollers it is using).

      Using interrupts, and attaching/detaching them to as needed during operation is common across anything with a CPU.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Subscribe to our newsletter now!