Mastering Arduino Interrupts: Button-LED Project Tutorial
Published on January 1, 2025 | By ArduinoUnoProjex
Unlock the power of Arduino interrupts with this beginner-friendly button-LED project! Learn how to use interrupts to toggle an LED with a button press, leveraging the attachInterrupt()
function and Interrupt Service Routines (ISRs). This step-by-step guide covers wiring, code, and best practices for Arduino interrupt projects. Start building with our recommended products and elevate your Arduino skills!
Note: Amazon product links are included for convenience (not ours, just recommendations). ELEGOO boards are not Arduino but are compatible with Arduino IDE and much more affordable.
Why Use Interrupts in Arduino Projects?
What are Arduino interrupts? Interrupts allow the Arduino to pause normal execution and run a special function (ISR) when an event occurs, like a button press. This Arduino interrupt tutorial demonstrates toggling an LED using a button on interrupt pin 2.
Benefits: Ensures immediate response to events, saves processing power, and simplifies event-driven programming.
Components Needed
To build this Arduino interrupt button-LED project, gather these components:
- ✅Arduino Uno or ELEGOO Uno R3 (Arduino IDE-compatible)
- ✅LED (with 220Ω resistor)
- ✅Push Button (with 10kΩ pull-up resistor)
- ✅Breadboard & Jumper Wires
- ✅USB Cable for Arduino
Optional: USB-to-Serial module (e.g., CH340) for debugging.
Wiring the Interrupt Circuit
How do I wire an Arduino interrupt project? Follow this schematic and instructions to connect the button and LED.
Connection Table
Component | Arduino Pin | Details |
---|---|---|
LED | 13 | Anode to pin 13 via 220Ω resistor, cathode to GND |
Push Button | 2 | One side to pin 2, other to GND; 10kΩ pull-up to 5V |
Wiring Instructions
Connect LED: Wire the LED anode to Arduino pin 13 via a 220Ω resistor and cathode to GND.
Connect Button: Attach one side of the button to Arduino pin 2 (interrupt pin), the other to GND. Add a 10kΩ pull-up resistor from pin 2 to 5V.
Power Arduino: Connect the Arduino to your computer via a USB cable.
💡Tip: Use pin 2 or 3 on Arduino Uno for hardware interrupts, as they’re optimized for this purpose.
Arduino Code for Interrupt-Driven LED Toggle
What code enables Arduino interrupts for a button-LED project? This code uses attachInterrupt()
to toggle an LED on button press.
#define LED_PIN 13 // LED connected to pin 13
#define BUTTON_PIN 2 // Button connected to interrupt pin 2
volatile bool ledState = false; // Variable to track LED state
void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), toggleLED, FALLING); // Trigger on button press
}
void loop() {
// Nothing to do here; LED state is handled by interrupt
}
// Interrupt Service Routine (ISR)
void toggleLED() {
ledState = !ledState; // Toggle LED state
digitalWrite(LED_PIN, ledState); // Update LED
}
Code Breakdown
- Pin Definitions:
LED_PIN
(13) for the LED,BUTTON_PIN
(2) for the interrupt-capable button pin. - Volatile Variable:
ledState
tracks LED state, markedvolatile
for ISR compatibility. - Setup: Configures pins and attaches interrupt to pin 2, triggering
toggleLED
onFALLING
edge. - ISR (toggleLED): Toggles
ledState
and updates the LED without delays or Serial calls.
Keywords: Arduino interrupt code, button LED interrupt project, attachInterrupt Arduino tutorial, ISR Arduino example.
Testing the Interrupt Project
How do I test an Arduino interrupt project? Follow these steps to verify functionality.
Upload Code: Connect Arduino to your computer and upload the code via Arduino IDE.
Check Wiring: Ensure LED and button connections are secure, especially the pull-up resistor on pin 2.
Test Button: Press the button. The LED on pin 13 should toggle (on/off) with each press.
Expected Result: LED toggles instantly on button press, demonstrating interrupt-driven control.
💡Tip: If the LED doesn’t toggle, verify pin 2 is used and the pull-up resistor is correctly wired.

The ultimate Raspberry Pi 5 starter kit—perfect for advanced DIY projects, programming, and STEM education. Includes 8GB RAM, 128GB storage, and quality components. Ideal for makers, students, and educators!

Best value Arduino Mega compatible board for complex robotics, 3D printing, and automation projects. Massive I/O, robust ATmega2560 microcontroller, and seamless compatibility with Arduino IDE.

Explore 37 different sensors and modules—ideal for learning coding, IoT, robotics, and all levels of Arduino experimentation. Includes tutorials and code samples.

Quality jumper wires for all breadboard and prototyping needs. Durable, flexible, and color-coded—essential for every electronics toolkit.

Connect your microcontrollers to your computer fast! CH340 USB to Serial modules are ideal for flashing, debugging, and uploading code to Arduino, ESP32, and more.
Interrupt-Capable Pins Across Arduino Boards
Board | Interrupt Pins |
---|---|
Arduino Uno | 2, 3 |
Arduino Mega | 2, 3, 18, 19, 20, 21 |
Arduino Leonardo | 0, 1, 2, 3, 7 |
Note: Always use digitalPinToInterrupt(pin)
to map pins correctly.
Recommended Products
Start your Arduino interrupt project with these high-quality components from Amazon (affiliate links). ELEGOO boards are Arduino IDE-compatible and budget-friendly.
Best Practices for Arduino Interrupts
- ✅Keep ISRs Short: Execute minimal code to avoid delaying other interrupts.
- ✅Avoid Delays/Serial: Don’t use
delay()
or Serial functions in ISRs, as they rely on interrupts. - ✅Use Volatile Variables: Mark shared variables as
volatile
to ensure proper updates. - ✅Debounce Buttons: Add software or hardware debouncing to prevent multiple triggers.
Troubleshooting Tips
- ⚠️LED Not Toggling: Verify button is connected to pin 2 and pull-up resistor is in place.
- ⚠️Multiple Triggers: Add a 10ms delay in the ISR or use a capacitor for hardware debouncing.
- ⚠️Interrupt Not Firing: Ensure pin 2 or 3 is used and
FALLING
mode matches button wiring. - ⚠️Code Not Working: Check for conflicting interrupts or ensure
volatile
is used forledState
.
Advanced Project Ideas
- 🚀Add a counter to track button presses and display on an LCD.
- 🚀Use timer interrupts to blink the LED at precise intervals.
- 🚀Integrate multiple buttons with different ISRs for complex controls.
- 🚀Connect to Wi-Fi (e.g., ESP8266) to log button presses to a cloud server.
Frequently Asked Questions
How do I use interrupts in Arduino?
Use attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
to link an ISR to a pin (e.g., 2 or 3 on Uno). The ISR runs when the pin state changes based on mode (e.g., FALLING
).
Why is my Arduino interrupt not working?
Ensure you’re using an interrupt-capable pin (e.g., 2 or 3 on Uno), the pull-up resistor is correct, and the ISR is short without delays or Serial calls.
Can I use ELEGOO boards for interrupt projects?
Yes, ELEGOO boards like Uno R3 or MEGA R3 are fully compatible with Arduino IDE and support interrupts on the same pins as Arduino boards.
Conclusion
This Arduino interrupt tutorial equips you with the skills to build responsive projects like the button-LED toggle. You’ve learned to wire interrupt pins, write efficient ISRs, and apply best practices. Download the project files here, use our recommended products, and explore our Arduino tutorials for more inspiration. Share your interrupt projects in the comments and join the maker community! 🔧
0 Comments