Why and where
Our Uno spends almost all its time inside delay(1000). If loop() read the button once per pass, a quick press during the wait would be missed. An interrupt makes the hardware call a function the moment pin 2 changes.
On the Uno, attachInterrupt only works on pins 2 and 3. Inside the ATmega328P they are named INT0 and INT1. digitalPinToInterrupt(2) turns pin 2 into interrupt number 0, so the same code works on other boards.
Setting it up
In setup(), attachInterrupt stores the address of onPress, our interrupt service routine (ISR): the function that runs when the interrupt fires. It sets EICRA's sense bits, which choose which edge counts, to FALLING, and switches INT0 on.
INPUT_PULLUP turns on a resistor inside the chip (20 to 50 kΩ) that holds pin 2 at 5 V, HIGH. Pressing connects the pin to ground: 0 V, LOW. That drop is a falling edge. Other modes: RISING, CHANGE, or LOW.
One interrupt
The edge sets one bit, the INTF0 flag. The CPU is deep inside delay() and does not stop to look; the hardware checks the flag between instructions. Even a pulse just over one tick of the 16 MHz clock, 62.5 ns, is caught.
The CPU finishes its instruction and parks the return address (where to come back to) on the stack, a reserved strip of RAM. It clears the I bit and INTF0, then jumps through the INT0 vector, a fixed slot in flash at 0x0002, to onPress.
onPress() runs and presses goes from 0 to 1. Keep an ISR short: inside one, delay() does not work, millis() stops advancing and incoming serial data can be lost. presses is volatile: the compiler must reread it from memory every time.
The ISR ends with RETI (return from interrupt). It pops the return address off the stack and sets the I bit again. delay() carries on exactly where it stopped, and the next pass prints 1. The main code never noticed.
Latency
Latency: the time from the edge to your code. One tick is 62.5 ns. The hardware needs 4 ticks to save the place and 3 to jump: 0.44 µs. The Arduino wrapper then saves registers, the CPU's few working slots: about 3 µs in all.
Latency grows while interrupts are off: inside another ISR, or after noInterrupts(). The flag holds the press until the I bit is 1 again, then onPress runs. But a flag is one bit: two presses while waiting count as one.
What happens
;; step 1/10 · busy
loop() Serial.println(presses); delay(1000);
polling read the pin once per pass: a short press can fall between reads
interrupt the pin itself asks the CPU to run a function, at once
// Arduino Uno R3: a push button from pin 2 to GND
volatile byte presses = 0;
void onPress() { // ISR
presses++;
}
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
attachInterrupt(
digitalPinToInterrupt(2),
onPress, FALLING);
}
void loop() {
Serial.println(presses);
delay(1000);
}
The two cards are one sketch: the global presses and onPress() come first, then setup() and loop(). Arduino code runs init() before setup(), and init() turns interrupts on, so the I bit is already 1 when setup() starts. The 0.44 µs is the datasheet minimum (4 + 3 cycles at 16 MHz); a multi-cycle instruction in progress adds its remaining cycles, and waking from sleep adds 4 more cycles plus the sleep mode's start-up time. The wrapper figures (82 cycles, 2.9375 µs before and 2.1875 µs after) were counted by N. Gammon from the compiled code; another compiler version can differ a little.