Welcome to the latest entry of the Programming with Arduino and Protocoder for makers course! In this entry, we will learn how to use interrupts, but what are they? An interrupt is a signal that halts whatever the processor is currently doing in order to execute a different function. The interruption can started externally, for example by pressing a button, or internally using a timer or a software signal. Once it has been activated, the interrupt will pause what the processor is doing and then execute the other function. This is known as an interrupt service routine or ISR. Once the ISR has been finalised, the program will return to what it was doing beforehand. To find out more about interrupts, you can visit the reference page here.

List of materials

  • ZUM BT-328 controller board or one compatible with Arduino
  • Button module

Electrical connections

The number of pins that can be used depends on the board we are using. For the ZUM BT-328 board, Arduino Uno and others from it, it is 2; pins 2 and 3, which correspond to interrupt 0 and interrupt 1 respectively. ZUM-push2

Using interrupts

In order to use the interrupts, we will first need to declare the desired interrupt. In this case we will use 0, which is on pin number two. So we need to declare:

In setup, we now need to specify the function to call when the interrupt is produced. We will do this using the attachInterrupt, which has the following syntax:

These are arguments of the attachInterrupt function:

  1. Interrupt: the interruption number, which means on which pin it will “listen” for the interruption.
  2. ISR: the function it will call when the interrupt is produced.
  3. Mode: set how the ISR will be launched, which means it must pass so that the interrupt is executed. The different modes are:
    1. LOW: executed whenever the pin value is 0.
    2. CHANGE: executed whenever there is a change.
    3. RISING: executed whenever the pin value is 0 to 1 (o from 0 to 5 volts).
    4. FALLING: executed whenever the pin value is 1 to 0 (o from 5 to 0 volts).

Take care with the modes and be sure to write them in capital letters, otherwise you will get an error. Here, we will use:

It will detect when we release the button. We also need to define the function that will be called on detection of the interruption. In this case, we have called the funcionInterrupción function and we define it with the same name that we put in attachInterrupt. The function is therefore:

Where we print on the serial monitor the number of the numbers it has entered into the interrupt. Something strange about interrupts is that they can´t have any arguments in their definition and they can´t return a value. So to make sure that an ISR has access to the variables, we need to use a volatile type variable, which will be int type in this case. If you want to know more about volatile, you can check the reference page. Another restriction with interrupts is that when we are in an ISR, the rest of the interrupts will not be detected, so delay will not work and the millis value won´t be updated. The ISRs should therefore be as short as possible.

 The code

You can see the full code here. Remember that you can also download the code together with the images used at the end of this post.

First we declare the variables that we are going to use; interrupción, which will tell the microprocessor the interruption that we will use, followed by numInterrupt, which will display the number of times that the it has accessed it. In the setup function, we start the serial port and declare the interruption that we will use with attachInterrupt();. In the loop function, a task that requires a certain time to be executed is simulated, which in this case, to avoid overloading the visualisation of program output on the serial port, “.”, will be printed on the serial port. When the interrupt is accessed, which in this example is on releasing the button, the program stops being executed and “Interrupt number” is printed on the serial monitor, followed by the number of times it has accessed the interruption. Now have a go at this exercise – change the mode for accessing the interrupt and see how it affects the circuit.