How do you manage interrupts with Arduino?
What is an interrupt and why should you know about it?
Interrupts are a feature of the microcontrollers and they represent an asynchronous task that microcontroller has to pay a constant attention to.
When an interrupt occurs, the normal execution of your program, momentarily stops, execute an interrupt function defined by the user and then continue.
While the main program runs sequentially, an interrupt function is always ready to be executed by the microcontroller.
Today I’m gonna show you how to use them with Arduino Board.
For the Arduino Mega2560 board, you have the pins 2,3,18,19,20,21 which can be enabled as interrupts.
The method attachinterrupt is structured as follows:
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
The first thing to do is define pins on the setup function as follows:
digitalPinToInterrupt(2)
digitalPinToInterrupt(3)
In this example, we’re setting the pins 2 and 3 as interrupts.
Then you have to specify the ISR that is the function that will be called by the interrupt.
Finally, you have to specify the mode that will activate the interrupts:
LOW to trigger the interrupt whenever the pin is low.
CHANGE to trigger the interrupt whenever the pin changes value .
RISING to trigger when the pin goes from low to high.
FALLING for when the pin goes from high to low.
HIGH to trigger the interrupt whenever the pin is high.
The interrupt function doesn’t return anything so it will be something like:
0 1 2 3 4 | void DoSomethingDuringInterrupt (){ //your code..... } |
Now I want to present an example that I prepared for you.
I set the pin 2 and 3 as interrupts.
I have two leds that alternatively blink and two switches.
The interrupts pay attention to the falling signal caused by these switches.
On output, we have a relay (that works with 5 V) that closes a contact that will make the multimeter buzzer ring.

Fritzing electrical scheme
So this is the scheme for your experiment.
Once you’re connected, we have to put the compiled code into the microcontroller.
The code to load on the microcontroller is :
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | int Relay=4; int LedGreen=6; int LedYellow=7; int count = 0; void setup() { Serial.begin(9600); pinMode(Relay,OUTPUT); pinMode(LedGreen, OUTPUT); pinMode(LedYellow, OUTPUT); digitalWrite(LedYellow, LOW); digitalWrite(LedGreen, LOW); digitalWrite(Relay, LOW); attachInterrupt (digitalPinToInterrupt (2), InterrupByFirstSwitch, FALLING); attachInterrupt (digitalPinToInterrupt (3), InterrupBySecondSwitch, FALLING); } void InterrupByFirstSwitch() { Serial.println("An interrupt occurred with First Switch and buzzer is On!"); digitalWrite(Relay, HIGH); } void InterrupBySecondSwitch() { Serial.println("An interrupt occurred with Second Switch and buzzer is Off!"); digitalWrite(Relay, LOW); } void loop() { digitalWrite(LedGreen, HIGH); digitalWrite(LedYellow, LOW); delay(200); digitalWrite(LedGreen, LOW); digitalWrite(LedYellow, HIGH); delay(200); } |
Once an interrupt has occurred the following code will be executed:
0 1 2 3 4 | void InterrupByFirstSwitch() { Serial.println("An interrupt occurred with First Switch and buzzer is On!"); digitalWrite(Relay, HIGH); } |
0 1 2 3 4 | void InterrupBySecondSwitch() { Serial.println("An interrupt occurred with Second Switch and buzzer is Off!"); digitalWrite(Relay, LOW); } |
This is a video that shows you how the project works:
So to recap:
An interrupt is a “sudden interruption” from an external source that requires an urgent attention.
To activate them with Arduino board you have to set them up on the setup function of your code.
An essential feature of the interrupts is that an ISR can occur between two instructions.
To use interrupts you have to implement the IST function and configure it as follows:
0 1 2 3 4 5 6 7 8 9 10 11 | Void Setup(){ attachInterrupt (digitalPinToInterrupt (PinYouWantToUse), ISR, MODE); } void InterruptFunctionThatDoesWhatYouDo { //your code.... } |