Arduino — Interrupts

An interrupt is a signal that tells the processor to immediately stop its current task and handle another high priority processing. This high priority processing is called an Interrupt Handler/Interrupt Service Routine. Almost every electronic device comes across a scenario where an interrupt needs to ‘interrupt’ the regular process and attend to an important task at hand. So technically, interrupts are a mechanism by which an I/O or an instruction can suspend the normal execution of the processor and get serviced since it has a high priority. Once that task has been wrapped up, it goes back to doing whatever the initial task.

How do interrupts work?

Interrupts call an external function which is commonly called the interrupt service routine (ISR) so that when an external interrupt occurs, the processor first executes the code present in the ISR and then reverts to the state where it left the normal execution.

ISR has the following syntax in Arduino: attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

digitalPinToInterrupt(pin): In Arduino Uno, the pins used for interrupt are 2 & 3. You need to specify the input pin that is used for external interrupt out here.

ISR: It’s a function that’s called when an external interrupt is done.

Mode: The type of transition to trigger the interrupt on, e.g falling, rising etc.

  • Rising — To trigger an interrupt when the pin transits from LOW to HIGH.
  • Falling — To trigger an interrupt when the pin transits from HIGH to LOW.
  • Change — To trigger an interrupt when the pin transits from either LOW to HIGH or HIGH to LOW.
  • LOW — To trigger the interrupt when the pin is LOW.

Types of Interrupts

There are two types of interrupts:

  • Hardware Interrupts — They occur in response to an external event, such as an external interrupt pin going high or low.
  • Software Interrupts — They occur in response to an instruction sent from the software. Timer interrupts are software interrupts.

Now let’s see how to use the Arduino interrupts using the functions that are available in the Arduino IDE. We’ll be using the attach interrupt function to initialize the interrupt and specify the Arudino pin which will be used to do so. The function takes two arguments as an input — one is the pin which is used to trigger the external interrupt and the second argument is the function name which you want to call upon every interrupt. The last argument is for the mode of interrupt that you’re using.

We’ll be using an example of the LED blinking to understand how the interrupt function works. Make the connections according to the following diagram:

Components required:

  1. Arduino Uno
  2. Breadboard
  3. Jumper wires
  4. 2 LEDs
  5. Push Button
  6. 1K Resistor

Image for post

Once you’re done with the connections, you can use the following sketch and upload it on the Arduino.

Image for post


The LED is attached to Pin 13 and a push-button is attached on the interrupt pin 2. So when the button is pushed up, the interrupt is triggered and there’s a change in the state. But if it's not pushed up, there’s no change and the loop continues. The ISR function is named ‘blink’, and the mode is set to ‘change’ so that whenever there’s a rise or fall, the interrupt is triggered. The ISR is used to change the state of the LED, so whenever the button is pushed or the interrupt is called the LED glows.

In this blog, we learned about what interrupts are, their different modes, types, and the functions that are required to trigger them. We also saw an example to understand how an interrupt is triggered and how exactly it works.