top of page

How to use interrupts - Arduino

  • Writer: automatelabonline
    automatelabonline
  • Mar 23
  • 3 min read

Interrupts allow the microcontroller to interrupt the normal execution of its code to respond to external or internal events quickly and accurately, without having to constantly check the status of an event. This event-driven approach to programming is essential for creating more efficient and responsive systems.

Arduino interrupts are often used in applications that require real-time or immediate responses, such as rpm measurement systems, sensor readings with millimeter precision, communication projects where synchronization is crucial, or even simple button readings. They allow Arduino to perform tasks in the background while maintaining the processing of other functions, which makes the system more agile and capable of handling multiple demands simultaneously.

Functions for Interrupts

Each Arduino board has different pins for interrupts. The Arduino Uno has two pins that can be programmed with interrupts, pins 2 and 3. On the Arduino MEGA, the pins are 2, 3, 18, 19, 20 and 21. For the Arduino Micro and Leonardo boards, pins 0, 1, 2, 3 and 7 are used.

To create an interruption, use the function:

attachInterrupt(CHANNEL, FUNCTION, MODE);

In this function, CHANNEL represents the interrupt channel. On the Arduino Uno, pin 2 represents channel 0, and pin 3 represents channel 1. To make things easier, you can use the function:

digitalPinToInterrupt(PIN);

Where the pin is passed as an argument and the channel corresponding to that pin is returned. It is recommended to use the function like this:

attachInterrupt(digitalPinToInterrupt(PIN), FUNCTION, MODE);

FUNCTION is the name of a function created in the code, which will be executed whenever the interruption occurs. MODE represents the type of interruption, there are the following possibilities:

  • LOW: interruption will occur when the pin is at a low logic level;

  • CHANGE: interrupt will occur when pin changes logic level;

  • FALLING: interruption will occur when the pin changes from high to low logic level;

  • RISING: interrupt will occur when pin changes from low to high logic level;

The Arduino Due, Zero and MKR1000 boards also allow you to configure them as:

  • HIGH: interruption will occur when the pin is at a high logic level;


To disable interruptions, simply use the function:

detachInterrupt(digitalPinToInterrupt(PIN));

Project - Creating Interrupts in Arduino

To better understand how interrupts work, let's make a simple project, where a button will trigger an interrupt. For this, the following circuit will be used:

Arduino Interrupt Project

Below is the project code:

#define interruptPin 2

void interrupt() {
  Serial.println("Button Pressed");
}

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), interrupt, RISING);
}

void loop() {
  delay(100);
}

Understanding the Code

First, the interrupt() function is created, which will be called every time the button is pressed.

void interrupt() {
  Serial.println("Button Pressed");
}

Since this function will interrupt the code, it must execute quickly. Therefore, it is not recommended to use the println() function inside the interruption function. In this case, we will use it only for demonstration purposes. However, in real projects, this practice is not appropriate.

In the setup() function, the interrupt is configured on pin 2 using the RISING method and calling the interrupt() function. When analyzing the circuit, when the button is pressed, it causes pin 2 to have contact with 5V. That's why the RISING mode is used.

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), interrupt, RISING);
}

With this code, every time the button is pressed, the Arduino will write the message "Button pressed" to the serial monitor.


Conclusions

Interrupt functions in Arduino are powerful features that allow developers to create more responsive and efficient systems, especially in projects that require constant monitoring of events and immediate response to stimuli.

However, the use of interrupts requires caution, especially in more complex projects where time control and resource management can be critical. It is essential to understand how interrupts work, their limitations, and how to integrate them correctly into the program flow to avoid problems such as synchronization failures and unwanted deadlocks.

Commentaires


bottom of page