Sub topic 13
Interrupts for Button Input: When and Why to Avoid Them
Assume you know the basic Arduino sketch structure, digital voltage levels, pull-up resistors, and how to drive an LED with digitalWrite(). This chapter focuses on using interrupts to notice button changes. It also explains why ordinary polling is often the better choice.
What a button interrupt does
Normally, your loop() function repeatedly checks a pin:
if (digitalRead(buttonPin) == LOW) {
// The button is pressed.
}This method is called polling. The program asks for the pin state whenever it reaches that line.
An interrupt changes this order. You configure the Arduino to pause normal code when a selected pin changes. The Arduino then runs a special function called an interrupt service routine, or ISR. When the ISR finishes, the main program continues.
On an Arduino Uno, pins 2 and 3 support external interrupts. Other Arduino boards may support different pins, so check the board's documentation.
Keep the ISR short
An ISR should do the smallest possible amount of work. It should not call delay(), print with Serial, or perform a long calculation. These operations can depend on interrupts themselves or can prevent the program from responding to other tasks.
A common pattern is for the ISR to set a volatile flag. The word volatile tells the compiler that the value can change outside the normal flow of the current code.
This example uses the internal pull-up. Therefore, a pressed button connects the input to GND and produces LOW.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
const unsigned long debounceMs = 50;
volatile bool pressPending = false;
bool ledOn = false;
unsigned long lastAcceptedPress = 0;
void buttonISR() {
pressPending = true;
}
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
attachInterrupt(
digitalPinToInterrupt(buttonPin),
buttonISR,
FALLING
);
}
void loop() {
bool eventReady = false;
noInterrupts();
if (pressPending) {
pressPending = false;
eventReady = true;
}
interrupts();
unsigned long now = millis();
if (eventReady && now - lastAcceptedPress >= debounceMs) {
lastAcceptedPress = now;
ledOn = !ledOn;
digitalWrite(ledPin, ledOn ? HIGH : LOW);
}
}attachInterrupt() connects the pin change to the ISR. FALLING means the ISR runs when the signal changes from HIGH to LOW, which is the press transition for an internal pull-up.
The code briefly disables interrupts while it copies and clears the shared flag. This prevents the ISR from changing the flag halfway through that operation. The debounce check occurs in loop(), where timing and output control are safer.
Interrupts do not remove contact bounce
A mechanical button can produce several rapid electrical transitions during one physical press. An interrupt may run once for every transition. The example ignores events accepted within 50 milliseconds of the previous accepted press.
| Method | Strength | Common cost |
|---|---|---|
| Polling | Simple and easy to debounce | The loop must run often enough |
| Interrupt | Can notice changes while other code runs | More complexity and still needs debouncing |
For a button checked in a normal, fast loop(), polling is usually clearer. Use an interrupt when the program may spend a long time waiting, when the event must be noticed promptly, or when the input is part of a timing-sensitive system.
When to avoid interrupts
Avoid button interrupts when:
- The main loop already checks the button frequently.
- The button event is slow compared with the rest of the program.
- You need complicated logic inside the handler.
- You are still learning the basic input and debounce pattern.
- The board's interrupt pins or interrupt behavior are unclear.
Use polling first. Move to interrupts only when you can identify a real timing problem that polling cannot solve simply.
Exercise
Why does the example use an ISR to set a flag instead of toggling the LED directly?
Show answer
The ISR should finish quickly. The main loop can debounce the event and safely update the LED.