0
0
AutocadHow-ToBeginner · 3 min read

Arduino Uno Interrupt Pins: Which Pins Support Interrupts?

On the Arduino Uno, only pins 2 and 3 support external interrupts using attachInterrupt(). These pins correspond to interrupt numbers 0 and 1 respectively.
📐

Syntax

The attachInterrupt() function connects an interrupt to a specific pin on the Arduino Uno. It has three parts:

  • interrupt: The interrupt number, not the pin number.
  • ISR: The function to run when the interrupt happens.
  • mode: When to trigger the interrupt (e.g., on a rising edge).

On Arduino Uno, interrupt 0 is pin 2 and interrupt 1 is pin 3.

arduino
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
💻

Example

This example shows how to use an interrupt on pin 2 to toggle the built-in LED when a button is pressed.

arduino
volatile bool ledState = false;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT_PULLUP); // Button on pin 2
  attachInterrupt(digitalPinToInterrupt(2), toggleLED, FALLING);
}

void loop() {
  digitalWrite(LED_BUILTIN, ledState);
}

void toggleLED() {
  ledState = !ledState;
}
Output
The built-in LED toggles ON and OFF each time the button connected to pin 2 is pressed.
⚠️

Common Pitfalls

Common mistakes when using interrupts on Arduino Uno include:

  • Using pin numbers directly instead of interrupt numbers or digitalPinToInterrupt().
  • Trying to use interrupts on pins other than 2 or 3, which won't work on Uno.
  • Writing long or blocking code inside the interrupt service routine (ISR).
  • Not declaring variables shared with ISR as volatile.
arduino
/* Wrong way: Using pin number directly without digitalPinToInterrupt */
attachInterrupt(2, ISR, RISING); // Incorrect

/* Right way: Use digitalPinToInterrupt */
attachInterrupt(digitalPinToInterrupt(2), ISR, RISING); // Correct
📊

Quick Reference

PinInterrupt NumberNotes
20Supports external interrupt 0
31Supports external interrupt 1

Key Takeaways

Only pins 2 and 3 on Arduino Uno support external interrupts.
Use digitalPinToInterrupt(pin) to get the correct interrupt number.
Keep interrupt service routines short and fast.
Declare variables shared with ISRs as volatile.
Do not try to use interrupts on other pins on Arduino Uno.