Learn Arduino Tutorials Open the 3D lab

Intermediate · 5 min read

Arduino interrupts explained — react instantly with attachInterrupt

Your loop() can only check a button when it gets round to it. An interrupt makes the Arduino stop what it is doing, run a tiny function straight away, and carry on. Perfect for fast events you must never miss.

An Arduino UNO wired to a push button on a breadboard, the circuit used to trigger an interrupt on pin 2
Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →

The short answer

An interrupt is a hardware alarm. When a pin changes, the Arduino pauses loop(), runs a short function called an interrupt service routine (ISR), then continues exactly where it stopped.

volatile int presses = 0;

void countPress() {          // the ISR: keep it very short
  presses++;
}

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

void loop() {
  Serial.println(presses);
  delay(500);                // presses are counted even during this delay
}

Press a button between pin 2 and GND. Even while delay(500) blocks the loop, every press is counted.

Why you need them

Polling means asking "is the button pressed?" every time round loop(). If loop() is busy, say inside a delay(), a short press falls between two checks and is missed. An interrupt does not wait for loop(): the hardware notices the change and calls your ISR within a few microseconds.

Polling (digitalRead)Interrupt
Who notices the changeyour loop(), when it checksthe chip's hardware
Can it miss a fast pulse?yes, if loop() is busyno
Best forslow things, like a button pressed by a personfast pulses, counting, wake-ups

For ordinary human button presses polling is usually fine, and easier. See millis() vs delay() for a way to keep polling responsive.

Which pins can do it

On an Arduino UNO only pin 2 and pin 3 support attachInterrupt(). (Other boards differ: the Mega has six.) Always wrap the pin in digitalPinToInterrupt(), so the same code works on other boards:

attachInterrupt(digitalPinToInterrupt(2), myFunction, FALLING);

The three arguments are the pin, the function to call, and the mode:

ModeFires when the pin…
FALLINGgoes from HIGH to LOW (a button with INPUT_PULLUP being pressed)
RISINGgoes from LOW to HIGH
CHANGEchanges either way
LOWis LOW (keeps firing while it stays LOW)

With INPUT_PULLUP the pin rests HIGH and drops to LOW when the button is pressed, so FALLING is the one to use. See pull-up and pull-down resistors.

A button wired to pin 2 and GND, with the internal pull-up resistor switched on
A button wired to pin 2 and GND, with the internal pull-up resistor switched on · open it full screen

The rules for an ISR

An ISR is special. Break these rules and you get strange bugs:

  1. Keep it very short. Set a flag or add one to a counter, and leave.
  2. No delay(). It does not work inside an ISR.
  3. millis() does not advance inside an ISR, but you can read it.
  4. Avoid Serial.print(). It can hang or lose data. Print from loop() instead.
  5. Mark shared variables volatile. This tells the compiler the value can change at any moment, outside the normal flow.

Without volatile, the compiler may keep the variable in a fast register and your loop() never sees the new value.

Sharing data safely

An int is 2 bytes, and the UNO's 8-bit chip copies them one at a time. If an interrupt fires between the two copies, loop() can read half-old, half-new data. The fix is to pause interrupts for a moment while copying:

volatile unsigned int presses = 0;

unsigned int readPresses() {
  noInterrupts();                // pause interrupts
  unsigned int copy = presses;   // copy safely
  interrupts();                  // turn them back on
  return copy;
}

For a single byte or bool this is not needed. Those are one byte, copied in one step.

Interrupts and button bounce

A real button bounces: the contacts chatter for a few milliseconds and one press can fire several interrupts. See button debounce. A simple fix inside the ISR is to ignore interrupts that arrive too soon after the last:

volatile unsigned int presses = 0;
volatile unsigned long lastTime = 0;

void countPress() {
  unsigned long now = millis();
  if (now - lastTime > 50) {     // ignore anything within 50 ms
    presses++;
    lastTime = now;
  }
}

Common mistakes

  • Forgetting volatile. The main loop does not see changes.
  • Using the wrong pin. On a UNO only 2 and 3 work. Any other pin does nothing.
  • Putting delay() or Serial.print() in the ISR. Set a flag and handle it in loop().
  • Forgetting to debounce. One press may count 5 or 10 times.
  • Using RISING with a pull-up button. The press falls to LOW, so use FALLING.
  • Doing too much. While an ISR runs, other interrupts wait, and long ISRs cause missed events.

Check yourself

  1. Which UNO pins support attachInterrupt()?

  2. Why mark a shared variable volatile?

  3. What should an ISR do?

Questions people ask

Can I use more than two interrupts on an UNO?

Not with attachInterrupt(). A separate feature called pin-change interrupts works on every pin, but it needs a library such as PinChangeInterrupt, or direct register code.

How fast does an interrupt respond?

A few microseconds on a 16 MHz UNO. A polled loop() that also runs a delay(500) can take half a second to notice the same event.

Do I need an interrupt for a normal button?

No. For a button pressed by a person, polling every few milliseconds is enough. Use interrupts for fast signals, such as a rotary encoder, a flow sensor or a wheel speed pulse.

How do I stop an interrupt?

Call detachInterrupt(digitalPinToInterrupt(2));. Use noInterrupts() and interrupts() to pause all of them for a moment.

What's next

Try the press counter, then compare it with and without the delay(500) to see the difference. Wheel-speed sensors, flow meters and rotary encoders all use exactly this idea. For the pin numbers, see the UNO pinout.

Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →
See all tutorials →

Your progress stays on this device — no account, nothing sent anywhere.

Last updated 2026-12-01

Read next