Arduino Interrupts Guide

Understanding and using interrupts in Arduino for efficient programming.

Introduction

Interrupts allow a microcontroller to respond immediately to important external or internal events.

Interrupt Circuit Diagram

Types of Interrupts

Interrupt Modes

Mode Description
RISING Triggered when signal goes from LOW to HIGH
FALLING Triggered when signal goes from HIGH to LOW
CHANGE Triggered when signal changes state
LOW Continuously triggers while signal is LOW

Example Code


const int interruptPin = 2;
volatile int counter = 0;

void setup() {
    pinMode(interruptPin, INPUT_PULLUP);
    Serial.begin(9600);
    attachInterrupt(digitalPinToInterrupt(interruptPin), countInterrupts, FALLING);
}

void loop() {
    Serial.println(counter);
    delay(1000);
}

void countInterrupts() {
    counter++;
}
            

Common Issues & Solutions

Ensure debouncing for mechanical buttons to avoid false triggers. Do not use delay() inside ISRs.