The attachInterrupt() interrupt command

FunduinoLast updated on

Den laufenden Programmablauf sofort auf einen Tastendruck reagieren lassen

Ein Arduino-Programm arbeitet seine Schleife ständig von oben nach unten ab. Wer während eines langen delay auf einen Taster drückt, wartet vergeblich – der Tastendruck geht verloren, weil das Programm gerade woanders beschäftigt ist.

Genau dafür gibt es Interrupts. In dieser Anleitung lernst du den Befehl attachInterrupt() kennen und schaltest damit eine LED per Tastendruck um – unabhängig davon, was das Hauptprogramm gerade tut.

Breadboard with Arduino, an LED, and a pushbutton
Setup: Button on pin 2, LED with series resistor on pin 7

You need this

Master-Taster mit farbigem Knopf - 12*12*6mm / 12*12*9mm - F23106071 - Funduino 12 x 12 mm pushbutton - colored button, 24 V / 50 mA, various colors — 12*12*6mm - White 0,25 € *Item no. F23106071About the product — 12 x 12 mm pushbutton - colored button, 24 V / 50 mA, various colors — 12*12*6mm - White
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 6 parts together 19,70 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Material

For this project, you need:

  • 1× microcontroller with a suitable USB cable
  • 1× breadboard
  • 1× LED
  • 1× 220-ohm resistor
  • 1× pushbutton
  • Male-to-male jumper wires

What an interrupt is

So far, your program has processed everything in sequence: The microcontroller runs through the loop and only responds to a button when it reaches the right point. If it’s in a long delay, a brief button press is simply missed.

An interrupt solves this problem. It immediately interrupts the running program, executes a specified function—the interrupt service routine—and then returns to where it was interrupted.

You specify exactly when the interrupt is triggered using one of four modes:

  • LOW – while the pin is LOW
  • CHANGE – on every change in signal level, in either direction
  • RISING – only when changing from LOW to HIGH
  • FALLING – only when changing from HIGH to LOW
Table with the interrupt modes LOW, CHANGE, RISING, and FALLING
The four trigger modes of attachInterrupt() at a glance

Structure

The LED is connected to pin 7 with its series resistor, and the push button to pin 2. The push button switches to GND - you don't need an additional resistor because the microcontroller's internal pull-up resistor is enabled in the sketch.

On the Arduino UNO, only pins 2 and 3 can trigger an interrupt. Nothing happens on any other pin. Other boards have different or more interrupt-capable pins—for example, every GPIO on the ESP32.

Breadboard with Arduino, an LED, and a pushbutton
Setup: Button on pin 2, LED with series resistor on pin 7

The attachInterrupt() command

attachInterrupt(digitalPinToInterrupt(TASTER), LEDSchalten, FALLING);

The command has three parts:

  • digitalPinToInterrupt(TASTER) converts the pin number into the corresponding interrupt number.
  • LEDSchalten is the name of the function that is called when an interrupt occurs.
  • FALLING specifies when the interrupt is triggered - here, when the button is pressed, because the signal changes from HIGH to LOW.

Another important keyword is volatile. Any variable used in both the main program and the interrupt routine must be marked with it. Otherwise, the compiler may cache its value, and the main program may continue working with an outdated value:

volatile bool LEDStatus = LOW;

The sketch toggles the LED each time the button is pressed: press once to turn it on, and press again to turn it off.

With CHANGE instead of FALLING, the interrupt would also trigger when the button is released. The LED would therefore toggle twice and then return to its initial state, making it look as if the button wasn't working.

Mechanical buttons bounce: a single press often produces several edges in quick succession. As a result, the LED may toggle twice with one press. In that case, press and release the button cleanly and briefly, or ignore triggers in the routine that occur less than about 200 milliseconds apart.

Sketch: Toggle LED using Interrupt
#define LED 7
#define TASTER 2

/*
  Je nach Zustand der Variablen LEDStatus ist die LED ein- oder ausgeschaltet.
  Beim Start des Programms ist sie ausgeschaltet.
  volatile sorgt dafuer, dass Hauptprogramm und Interrupt-Routine
  immer denselben aktuellen Wert sehen.
*/
volatile bool LEDStatus = LOW;

void setup()
{
  pinMode(LED, OUTPUT);

  // Mit INPUT_PULLUP liegt der Taster im Ruhezustand auf HIGH
  // und wird beim Druecken auf LOW gezogen.
  pinMode(TASTER, INPUT_PULLUP);

  /*
    FALLING = der Interrupt wird genau dann ausgeloest,
    wenn der Pegel von HIGH nach LOW wechselt, also beim Druecken.
    Mit CHANGE wuerde er auch beim Loslassen ausloesen und
    der Zustand der LED waere danach wieder derselbe.
  */
  attachInterrupt(digitalPinToInterrupt(TASTER), LEDSchalten, FALLING);
}

void loop()
{
  // LED entsprechend dem gespeicherten Zustand schalten
  digitalWrite(LED, LEDStatus);
}

// Interrupt-Service-Routine: wird beim Tastendruck aufgerufen
void LEDSchalten()
{
  // LEDStatus umdrehen: aus true wird false, aus false wird true
  LEDStatus = !LEDStatus;
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.