Fiverr gig
$15 โ€” I will help you with your project and simulate it for you
Hire me
๐ŸŒ IoT Projects

Interrupt function

EmbedLab TeamDecember 23, 20244 min read
Interrupt function

In Arduino, the interrupt function allows the microcontroller to pause its normal program execution and immediately run a special function (called an Interrupt Service Routine or ISR) in response to a specific event, like a change in a pin's state or the expiration of a timer.

How Interrupts Work:

  • Interrupts are like alarms that tell the processor to stop what it's doing and pay attention to something important.

  • When an interrupt occurs, the processor stops the current task, saves its state, and executes the ISR.
  • Once the ISR completes, the processor resumes its normal tasks.

Key Points About Arduino Interrupts:

  1. Pin-based Interrupts:

    • Arduino supports hardware interrupts on specific pins. For example:
      • On an Arduino Uno, hardware interrupts are available on pins 2 and 3.
      • Other boards may support interrupts on additional or all pins.
    • These pins are associated with specific interrupt numbers.
  2. Function to Attach an Interrupt:

    • Use the attachInterrupt() function to define an interrupt and associate it with a pin and ISR:
      attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
      • pin: The pin you want to monitor for an interrupt.
      • ISR: The name of the Interrupt Service Routine to execute.
      • mode: Defines the event that triggers the interrupt:
        • LOW: Triggers when the pin is LOW.
        • CHANGE: Triggers when the pin changes state (HIGH to LOW or LOW to HIGH).
        • RISING: Triggers when the pin changes from LOW to HIGH.
        • FALLING: Triggers when the pin changes from HIGH to LOW.
  3. Disabling and Re-enabling Interrupts:

    • Use detachInterrupt(interruptNumber) to disable the interrupt.
  4. Characteristics of an ISR:

    • Keep it Short: The ISR should execute quickly to avoid delaying other interrupts.
    • No delay() or Serial Communication: These functions depend on interrupts themselves and will not work inside an ISR.
    • Variables in ISRs: Use the volatile keyword for variables shared between the ISR and the main program.
Schematic:

Schematic

Code:

#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 resistor
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), toggleLED, FALLING); // Trigger on button press
}

void loop() {
  // Nothing to do here; the LED state is handled by the interrupt
}

// Interrupt Service Routine (ISR)
void toggleLED() {
  ledState = !ledState;          // Toggle the LED state
  digitalWrite(LED_PIN, ledState); // Update the LED
}
Project link:
https://drive.google.com/file/d/1hrUh5jpxibwFkGIcW8VUMIbI_7tpASS6/view?usp=drive_link


ET

EmbedLab Team

Embedded systems engineer and educator. Writes weekly tutorials at EmbedLab to help beginners ship real hardware.

Related projects

Top 5 IoT projects with Arduino UNO and ESP32 โ€” Blynk app, Wi-Fi and smart home
๐ŸŒ IoT Projects

Top 5 IoT Projects with Arduino UNO & ESP32 โ€” Blynk, Wi-Fi & Smart Home Tutorials (2026)

The top 5 IoT projects every Arduino UNO and ESP32 maker should build in 2026 โ€” Blynk IoT control, Wi-Fi blink, ESP-WROOM-32 dev board guide, ESP32 Proteus 8 simulation and an Arduino smart home parking system. Each pick links to the full tutorial with free Arduino IDE code, library and wiring.

May 30 8 min
Blynk IoT app controlling Arduino UNO and ESP32 over Wi-Fi
๐ŸŒ IoT Projects

Blynk IoT with Arduino UNO & ESP32 โ€” Free Arduino IDE, Library & App Tutorial

Complete Blynk IoT tutorial for Arduino UNO and ESP32 Arduino: install the Arduino IDE, add the Blynk Arduino library, wire LEDs, LCD and sensors, and control everything from the Blynk app over Wi-Fi โ€” a free, beginner-friendly Arduino IoT project.

Nov 19 6 min
Arduino UNO and ESP32 Blink IoT project with Blynk app
๐ŸŒ IoT Projects

Arduino UNO Blink IoT Guide โ€” ESP32 Arduino, Blynk App & Arduino IDE Code

Complete Arduino UNO Blink IoT guide using ESP32 Arduino and the Blynk app: free Arduino IDE code, Arduino library and LED wiring โ€” the easiest Arduino IoT project for beginners to download and try online.

Oct 27 9 min
How to Control LEDs Using Blynk and Arduino Uno: A Complete Guide
๐ŸŒ IoT Projects

How to Control LEDs Using Blynk and Arduino Uno: A Complete Guide

  How to Control LEDs Using Blynk and Arduino Uno: A Complete Guide Controlling LEDs remotely using Blynk and an Arduino Uno is a simple yet powerful project for IoT (Internet of Things) enthusia

Apr 4 4 min
Proteus 8 Blynk
๐ŸŒ IoT Projects

Proteus 8 Blynk

Connect Proteus 8 with Blynk Using Arduino: A Complete Step-by-Step Guide Looking to seamlessly connect Proteus 8 with Blynk using Arduino ? This detailed tutorial covers everything from setting up a

Apr 4 7 min
Browse all 50+ projects