Interrupts in Arduino  What are they? + Effects and Functions ‚Ė∑ 2021

Arduino is one of the technologies that we can use to program and create great innovations, adding that it is totally practical to be developed by any of us without the need for some certifications in programming or electronics.

There are so many details about Arduino, that currently you can implement interruptions in your process. Throughout this article we will explain clearly and in detail what these interruptions really are and their importance.

Also, we will talk about its functions and how you can create one quickly and easily. If after this you want to create your own interrupt, you can use as a basis some of the projects that we mention here.

What are interrupts in Arduino and what are they for when developing a device?

Arduino interrupts

When we talk about interruptions we mean a powerful mechanism that can be used for safe stops, or stop any equipment or system immediately. Because Arduino it is an electronic system, you must also make use of this mechanism.

In a more illustrative way, We can imagine that we have a technology that allows us to control a centrifugal pump from a PLCHowever, we need it to stop and it does not respond to the actions of the PLC, that is where we must quickly go to the stop or interrupt button, for him to order an immediate arrest.

The great value of this stop, not only lies in a safe work for an operator, it can also offer us advantages, such as:

  • Intervention to check the correct consumption of energy and resources, which can be queried through the input status.
  • Having a system at the point of collision, that needs to be intervened immediately, so that corrective actions can be carried out.
  • Check the pulses that are sent from the processors, Due to systematic consultation, we may not notice shortened stock pulses.

On the other hand, these interruptions if they are not programmed and activated can be a serious problem, causing system and processor instabilities, interrupting events that have been executing perfectly. These types of interruptions can be named ISR by its acronym in English, ‚ÄúInterruption Service Rutine.‚ÄĚ

From a more technical point of view, these interrupts work through a normal processor and program flow that is interrupted, whose tasks and actions sent are not taken into account, executing the sole function of the ISR.

Effects of an interrupt in Arduino How does it influence the operation of a project?

Arduino interrupts

Interruptions have a positive influence on all projects, although, this clearly due to the proper use of it. A system can contain more than one interrupt line, its use depends on the needs and priorities. If we have a system and processor that execute a set of tasks in a normal and continuous way, but we want it to fulfill other activities whose value and importance is greater than the previous one, the ISR must be run.

How it works can be explained in a basic way through the following lists:

  • Cancel all commands of the machines that are attached to the switch.
  • All messages and values that are emitted by the processor, are protected and saved in a register in such a way that can continue to run when the action is requested again.
  • The processor or CPU skips all the addresses that are stored for routine tracking, and execute a single target parry. This is one of the safest stops for both the system and the operator.
  • Once we finish the interruption, the CPU will run all tasks that were previously ignored, so it is resumed from the previous point of use.

Types of Interruptions

exist more than one type of systems and processorsTherefore, there are also different types of interruptions.

Such we mention them below:

  • HW or external interrupts: They are asynchronous interruptions to the actions issued by the processor. In other words, they are stops that can be executed at any time regardless of the CPU is working at that time.
  • SW interrupts: These are the famous software interruptions, the process of which is induced through the programming of a code and action. Arduino does not currently support these types of interrupts.
  • Scheduled events or time stops: As the name implies, they are stops in time factor, these are programmed according to a specific working time.
  • Exceptions: These are the type of stops that occur every time an operator wishes to perform improper or blocked actions, where the system responds with an immediate stop.

ISR function What is it and what is its purpose in Arduino interrupts?

We could roughly say that the functions of an interrupt are called ISR, and are based with the objective of not sending or receiving another function, preventing the return of any data. In the previous section we mentioned that there are different types of ISR, however neither can be run simultaneously because it can cause a lack of control. The right way for it is to execute one in continuation with another.

If you enter properly into the world of Arduino technologies you will notice that it is constantly repeated that an interruption the shorter the better. When an ISR has the shortest possible run time, allows the system a return of its activities quickly and effectively, and the generation of errors is less.

Otherwise, we can see it in an illustrative way; if we have an object collection process with the implementation of an engine, a long stop can cause errors, due to that the motor loses power and stops putting pressure on the arm, causing it to release the objects.


If we want to modify a variable that belongs to the external part of the interrupt, we must always take into account the importance of making a Volatile declaration, whose indicator is the one used to refer to a variable that can be audited or queried.

A variable indicator is the one that compiles all the records, it also disables some of the optimization features that can be considered as efficiency leaks. When we need to create variables, we only have to mark as volatile, the characteristics that are really necessary for creating the main loop within interrupts.

Learn step by step how to create and test an interrupt on an Arduino fast and easy

If you want to create your own interrupt in a arduino board, this is the right place for you. In this section We are going to explain what you must do to learn how to create and test an interrupt on Arduino, in the easiest way out there.

Go for it:

Create interrupt

To create an interruption, the first thing you should take into account is its language and syntax, mostly it is handled with the following:

attachInterrupt (digitalPinToInterrupt (pin), ISR, modo); //(recomendado)

attachInterrupt (Interrupt, ISR, modo); //(no recomendado)

attachInterrupt (pin, ISR, modo); //Arduino SAMD Boards, One WiFi Rev2, Due y 101

Knowing the above, we proceed to create our own ISR, using the listed parameters:

  • Interupt: This indicates the number of interruptions we want. The syntax that must be entered to refer to this is ‚Äúint‚ÄĚ.
  • Pin: As its own name indicates, it is only the Arduino pin number.
  • ISR: This time when we talk about ISR, we only want to refer to the call that makes the interrupt. It is a function completely free of parameters and does not return anything, in many cases it is just a routine of the service.
  • Mode: With this function you define when the interrupts should be activated, and they are based on four constants of allowed values, What:
  • Low: It is used if we want to activate the interrupt as long as the pin is low.
  • Change: It is used to activate the interrupt when the value of the pin changes.
  • Rising: When the pin changes, from low to high, this pin is used.
  • Falling: Unlike the previous one, this is when the value of the pin goes from high to low.

Test interrupt

Not only do we have to create an interrupt, we also have to test in a way that confirms that the algorithm works correctly. Because it is a test, we will have to value the maximum of everything, as well as the minimum, so, we must execute all possible stops, both long and short, if we notice something unusual in the system, the ISR will have to be changedOn the contrary, if all goes well, we must congratulate ourselves on the good work.

List of the best Arduino projects with Interrupts that you can do yourself to practice

In this last section we will talk about those projects that you can use to practice and exercise all your acquired knowledge for creating interruptions.

The following are simple projects suitable for programming:

Car parking sensor

Car parking sensor

In order to take care of the integrity of the car when we park it is convenient to create a sensor with an Arduino board. With this device, an alarm will sound every time the car approaches an object at the time of the maneuver, but it is important that it has an interruption mechanism to stop the sound after having executed the task.

Knowing the syntax, creating the ISR of this project is very easy to program because perform simple tasks, and on the other hand the interruption must be long, so that it aborts all the executed tasks. You will have to use an Arduino UNO model in this project, three ultrasonic sensors, a breadboard, a cable kit and two mini battery screens. When you have all these elements you will have to assemble them according to the image we show you.

Then, you will have to open IDE and write these instructions for programming:

#include <TM1651.h>

#define CLK 3 // Cableado de la pantalla derecha

#define DIO 2

#define CLK2 5 // Cableado de la pantalla izquierda

#define DIO2 4

#define trigPin 6 // Cableado de los módulos de ultrasonido R = Derecha L = Izquierda el otro es el medio

#define echoPin 7

#define trigPinR 8

#define echoPinR 9

#define trigPinL 10

#define echoPinL 11

#define Maxdistance 20 // Distancia m√°xima 20cm

larga duración ;

int distancia ;

TM1651 DisplayR ( CLK, DIO ) ;

TM1651 DisplayL ( CLK2, DIO2 ) ;

configuración vacía () {

pinMode ( trigPin, SALIDA ) ; // Pinmodes del HC-SR04 e inicio de las pantallas

pinMode ( echoPin, ENTRADA ) ;

pinMode ( trigPinR, SALIDA ) ;

pinMode ( echoPinR, ENTRADA ) ;

pinMode ( trigPinL, SALIDA ) ;

pinMode ( echoPinL, ENTRADA ) ;

init () ;

set ( M√ĀS BRILLANTE ) ;

init () ;

set ( M√ĀS BRILLANTE ) ;


bucle vacío () {

int distanciaM = CDistancia ( trigPin, echoPin ) ;       // Calculando todas las distancias llamando a la función de abajo

int distanciaR = CDistancia ( trigPinR, echoPinR ) ;

int distanciaL = CDistancia ( trigPinL, echoPinL ) ;

int R = min ( distancia M , distancia R ) ; // Tenemos dos pantallas R y L, primero tomamos la distancia mínima entre M / R y M / L

int L = min ( distanciaM, distanciaL ) ;

if ( R> 0 && R <Maxdistance ) {

Nivel corto R = mapa ( R, 0, Distancia m√°xima, 0,7 ) ;

displayLevel ( LevelR ) ;


if ( R> Maxdistance )

displayLevel ( 7 ) ;

if ( L> 0 && L <Maxdistance ) {

Nivel corto L = mapa ( L, 0, Distancia m√°xima, 0,7 ) ;

displayLevel ( LevelL ) ;


si ( L> Maxdistance )

displayLevel ( 7 ) ;

retraso ( 50 ) ;


int CDistance ( int a, int b ) { // Calculando la función de distancia

digitalWrite ( a, BAJO ) ;

delayMicroseconds ( 2 ) ;

escritura digital ( a, ALTA ) ;

delayMicroseconds ( 10 ) ;

digitalWrite ( a, BAJO ) ;

duración = pulseIn ( b, HIGH ) ;

distancia = duración * ( 0 .034 / 2 ) ;

distancia de regreso ; // Devuelve la distancia calculada


Smart traffic light

Smart traffic light

The typical Arduino semaphore, is the second simplest Arduino projectHowever, the level of difficulty lies when we must create an interruption that cancels an action and allow another process to run. This means that the circulation color can be changed. For this, the type of ISR to be used must be short and programmed in time factor.

Taking into account the above, to create your own smart traffic light you will need a Arduino Nano R3, generic led lights, photovoltaic resistors, cables and a 100 Ohm resistor. When you finish look at the diagram that we leave you to join the components.

Then open the programming platform and enter these codes:

// Elige el pin de entrada para el sensor LDR al lado del led

int ledSensor  =  A1  ;

// Elige el pin de entrada para el sensor LDR en la parte superior de la l√°mpara que siente la luz del sol

int sunSensor  =  A0  ;

// Elige el pin de entrada para el LED

int led  =  13  ;

int pirSensor  =  2  ;

int ledSensorValue  =  0 ;

// Esta es la variable para almacenar el valor proveniente del censor, en la parte superior de la l√°mpara que siente la luz solar

int sunSensorValue  =  0  ;

int pirSensorValue  =  0  ;

configuración vacía () {

Serial . comenzar ( 9600 );

pinMode ( led , SALIDA );

pinMode(pirSensor, INPUT);


void loop() {

sunSensorValue = analogRead(sunSensor);

if (sunSensorValue > 900 )


pirSensorValue = digitalRead (pirSensor);

if (pirSensorValue == 1)


digitalWrite(13, HIGH);





ledSensorValue = analogRead(ledSensor);




}else if (pirSensorValue == 0){

digitalWrite(13, LOW);

print(String(0)); //led is off




ledSensorValue = analogRead(ledSensor);

Serial .print(String(ledSensorValue));


Serial . println(String(pirSensorValue));



// Si  (sunSensorValue < 900)


escritura digital ( 13, LOW);

Serial . imprimir ( Cadena ( 0));

Serial . impresión(",");

Serial . imprimir ( Cadena (sunSensorValue));

Serial . impresión(",");

ledSensorValue =  analogRead(ledSensor);

Serial . imprimir ( Cadena (ledSensorValue));

Serial . impresión(",");

pirSensorValue =  digitalRead (pirSensor);

Serial . println ( String ( pirSensorValue));


retraso ( 10000 ); // Demora 10 segundos y luego vuelve a tomar la lectura. Es aquí en donde se produce la interrupción


Sketch car lights

Sketch car lights

This is one of the Arduino projects most chosen by beginners, because it not only allows us to decorate the car and give it a sense of luxury, but you will also be able to practice the IDE. Its programming is simple, so you cannot put aside this electronic work. Interruptions play an important role, because the direction or flow of the process of each of the lights must change according to our taste, so the ISR allows the change canceling the task that is being executed at the moment.

You will need, to carry out the project, an Arduino Nano R3, A 120-piece led strip, 3mm sodial, male to male cables and three 400-point seamless breadboard plates. You will be able to join these materials together taking into account the image above.

Finally, there is the programming part of the Arduino Nano Rev3 board, so you will have to load the sequences that we indicate below:

byte ledPin[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

int ledDelay = 300 ;

int direccion = 1;

int LEDActual = 5;

unsigned long TiempoCambio;

void setup() {

for(int x=0; x<10 ; x++){

pinMode(ledPin[x], OUTPUT);


TiempoCambio = millis();


void loop() {

if ((millis() - TiempoCambio) > ledDelay){


TiempoCambio = millis();



void cambioLED() {

for(int x=0; x<10 ; x++){




LEDActual += direccion;

if (LEDActual == 9) {

direccion = -1;


if (LEDActual == 0) {

direccion = 1;



If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will also be of great help to more members of the community. Thanks! ūüėČ

Mario Jose

Author: Mario José

Graduated in journalism, specialized in investigation, I seek the truth of all things. Now 100% focused on technology, computing and the Internet.

You may be interested:

Leave a Comment