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 change | your loop(), when it checks | the chip's hardware |
| Can it miss a fast pulse? | yes, if loop() is busy | no |
| Best for | slow things, like a button pressed by a person | fast 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:
| Mode | Fires when the pin… |
|---|---|
FALLING | goes from HIGH to LOW (a button with INPUT_PULLUP being pressed) |
RISING | goes from LOW to HIGH |
CHANGE | changes either way |
LOW | is 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.

The rules for an ISR
An ISR is special. Break these rules and you get strange bugs:
- Keep it very short. Set a flag or add one to a counter, and leave.
- No
delay(). It does not work inside an ISR. millis()does not advance inside an ISR, but you can read it.- Avoid
Serial.print(). It can hang or lose data. Print fromloop()instead. - 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()orSerial.print()in the ISR. Set a flag and handle it inloop(). - Forgetting to debounce. One press may count 5 or 10 times.
- Using
RISINGwith a pull-up button. The press falls to LOW, so useFALLING. - Doing too much. While an ISR runs, other interrupts wait, and long ISRs cause missed events.
Check yourself
Which UNO pins support attachInterrupt()?
The UNO's ATmega328P chip has two external interrupt pins, 2 and 3.
Why mark a shared variable volatile?
Without volatile, the compiler may keep a stale copy and the main loop never sees the update.
What should an ISR do?
Keep ISRs tiny. Do the slow work in loop().
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.
Your progress stays on this device — no account, nothing sent anywhere.
Last updated 2026-12-01



