Interrupts 101

Contents

Overview

Interrupts are part of every day life. Not just real life, but the daily operations of computers - including embedded systems such as Arduino.

I prepared this guide because there seems to be quite a bit of interest in the topic of interrupts, but also a significant amount of mystery which often lead to misinformation.

There is a companion video that not only follows along with the examples in this web site, but also looks at some of the background theory and some examples of how interrupts are used within the Arduino HAL software.

Following are some attributes of interrupts:

  • Interrupts can occur at any time - including in the middle of operations in your C code.
  • Interrupts are fired upon completion of the currently running machine instruction.
  • Interrupts are serviced (processed/deal with) via an Interrupt Service Routine (ISR)
  • ISRs should be "short and sweet".
  • Interrupts are deferred when an ISR is active.
  • Deferred interrupts are fired when an active ISR completes.

In this guide (and the video) I create a couple of projects that illustrate:

  • how interrupts work using a button to trigger them
  • how interrupts are used to ensure reliable operations while "other stuff" is happening at the same time
  • how interrupts are used to reliable receive an incoming signal

What is an Interrupt?

As its name implies, an interrupt is something that happens that breaks the continuity of some activity.

Typically the interrupt is dealt with in some way. Once the interruption is dealt with the original activity is resumed - typically from the point where the interruption occured.

Whether this is in real life or in a computer, the basic idea of an interrupt is the same. All but the simplest of computers rely on interrupts for smooth operations. This includes several features in the Arduino ecosystem which rely on interrupts to work.

The examples

On this site, and in the companion video, I explore interrupts, how they are used in Arduino, some do's as well as some don'ts by looking at some practical follow along examples.

In general, there are two versions of the projects. Specifically:

  • A standard version of the program that does not use interrupts.
  • The same program, but converted to be interrupt driven.
The purpose of this duality is to firstly illustrate "the problem" that you may encounter with standard coding. In addition to that, to illustrate how to convert a program to use interrupts.

The projects are:

  • Interrupts triggered by a push button (this is "you certainly can do this, but arguably not best practice").
  • Monitoring how Serial works, how it uses interrupts and how that relates to your code.
  • A hypothetical example that demonstrates the basis of a real world practical use of interrupts.
In addition to those projects that we create as part of this guide (and in the video), I have a look at a real project that I created that relies on interrupts to maintain a rock solid 4 digit display.
In that project, you can easily switch between using interrupts or not using them. This allows you to see the benefit/impact of using/not using interrupts for critical operations.
The project, Event Countdown Clock - V2.0, is fully documented on Instructables.

Parts list

To complete this tutorial you will need:

  • Two Arduinos.You can do much of it with just one. But, to do everything, you will need 2.
    I used 2 Arduino Unos. Other models can be used, but some of the code and connections may need to be adapted.
  • A push button
  • 2 LEDs + current limiting resistors
  • Breadboard and hookup wire for your display.

Please support me if you can

Please help support these projects by: Buy me a coffee - gm310509 Patreon - gm310509