Processor Interrupts with Arduino

Pages
Contributors: JordanDee, Ell C
Favorited Favorite 10

How Does It Work?

When the event or interrupt happens, the processor takes immediate notice, saves its execution state, runs a small chunk of code (often called the interrupt handler or interrupt service routine), and then returns back to whatever it was doing before.

The programmer defines the code that is to be executed when a particular interrupt occurs within the program itself. In Arduino, we use a function called attachInterrupt() to do this and the recommended syntax looks similar to the output below.

language:c
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

This function takes three parameters:

  • First Parameter (i.e. digitalPinToInterrupt(pin)) - Pin number of the interrupt, which tells the microprocessor which pin to monitor. The pin depends on the microcontroller being used.

  • Second Parameter (i.e. ISR) - The location of code we want to execute if this interrupt is triggered.

  • Third Parameter (i.e.mode) - Tells it what type of trigger to look for: a logic high, a logic low or a transition between the two.

For more information what pins are reserved for interrupts and some example code, check out Arduino's attachInterrupt() page.