How to use Arduino interrupts explained with examples




In this tutorial, you will learn how to use Arduino interrupts. First, we will see what are Arduino interrupts? After that we will see the general concepts of interrupts. We will see how to use interrupt service routine with Arduino. At the end of article, we will take two examples to demonstrate the effect and use of Arduino projects in real world embedded systems projects.




What are interrupts?

Interrupts are the section of hardware and software on microcontroller which is capable of monitoring the external event on the input pin when any external event is monitor then the program is stop execution at this point and jump into their ISR function which is a interrupt handler then after executing the ISR function go back that point where the external event is monitor than it complete there execution.

Number of Arduino interrupts in different Arduino boards

Different types of Arduino board have different numbers of interrupts pins e.g. Arduino UNO have two interrupt ports and Arduino Mega2560 have six interrupt ports named as INT1,INT0. On the software side create sleep mode for Arduino and use a timer base interrupts which would internally be essentially triggering awakening function and not relay on any external hardware.Arduino interrupts in different arduino boards




How interrupts work?

So interrupts call an external function which is more commonly called an interrupts service routine or an ISR function. Interrupts service routine do have very specific constrains and do not behave exactly like some of the other functions that have been written for the Arduino.

What is a interrupt service routine?

Interrupt service routine (ISR) is also called an interrupts handler. There are many different type of interrupt handler which may handles many different types of interrupts like same as a simple example the clock in a system have its own interrupt handler same as the keyboard it also have its own interrupt handler for every device which is existing have its have its interrupt handler.

Generally ISR will use that something called a volatile variable which can still be used between other pieces of code also ISR should be short and fast as possible. Interrupts execute immediately if stop everything than program will currently doing in order to jump into the interrupts function and execute the code. Further more the interrupts will returns to the same point within the software where had perversely left off.

Basic Example of Arduino interrupts 

Arduino interrupts

Above is the example of execution so line by line the code is execute until interrupt is called on Line3 then the function jumps down to the ISR and started executing the line5 and line 6 than after executing the lines within the ISR it jumps back to line4 and finishing the execution as routine. If it is in loop than go back to line1.

How interrupts are been triggered?

In Arduino interrupts, you can set how the interrupts are been triggered. There are five types of triggering Arduino interrupts:

  • Change: When signal change even if signal rise or signal fall or if the signal is in low state at 0 or if the signal is in high state trigger 5v.
  1. Rising: On a rising edge the signal going from low to high means signal triggering from 0v to 5v.
  1. Falling: On a falling edge the signal going from high to low means signal is triggering from 5v to 0v.
  1. Low: Low is a continuous trigger whenever the signal is low in other words the signal is on 0v.
  1. High: High is a continuous trigger whenever the signal is high in other words the signal is on 5v.

The syntax which are going to be attach interrupt and specify the pin e.g. pin number 2 than ISR is the function which is going to be call and mode tells that whenever the interrupts is been triggered.

How to use Arduino interrupts?

Now lets see how to use Arduino interrupts functions already available in Arduino IDE  to initialize Arduino interrupts and with which pin of Arduino you want to initialize it. Attach interrupt function is used for this purpose. This function takes two arguments as a input. one is a pin number to which pin you want to use for external interrupt triggering. Second argument is function name to which you want to call upon every interrupt. Last argument is the types of interrupt you are using. we have explained five types above.

attachInterrupt ( digitalPinToInterrupt ( pin ), ISR, mode ) ;

e.g.

attachInterrupt ( digitalPinToInterrupt ( 2 ), InterruptFunction, Low ) ;

Example code of how to use Arduino interrupts

Arduino interrupts example

Below the example code of LED blinking in which the interrupt function is used to understand more clearly.

const byte ledPin = 13 ;

Led is attach on the board of input pin 13.

const byte interruptPin = 2 ;

A push button is attached on the interrupt pin 2.

volatile byte state = LOW ;

The state set to be at low

When the button pushed up then the interrupt is triggered and change the state if button is not pushed up then there is no change in the program and stay in the loop function.

void setup ( ) {

   pinMode ( ledPin , OUTPUT ) ;

   pinMode ( interruptPin , INPUT_PULLUP) ;

   attachInterrupt ( digitalPinToInterrupt ( interruptPin ), glow, CHANGE ) ;

}

With the help of this function the interrupt is attached in the code. Three parameters are presents in attachInterrupt function the 1st is interrupt pin in the care of this example the interrupt pin is 2, and the 2nd is ISR function which is named as Glow, and the 3rd is mode in this example the mode is set to change mode means whenever the interrupt trigger even if interrupt rise or fall and low or high at any condition interrupt triggered.

void loop ( )

{

   digitalWrite ( ledPin, state ) ;

 }

led set on the digital pin 13.When the interrupt call then level triggered and change is position than Led will glow up. Glow is the function of interrupt service routing.

void glow ( ) {

   state = !state ;

This line changes the state of LED whenever the button is pushed or the interrupts called.

}

Complete code is given below:

const byte ledPin = 13 ;

const byte interruptPin = 2 ;

volatile byte state = LOW ;

void setup ( ) {

   pinMode ( ledPin , OUTPUT ) ;

   pinMode ( interruptPin , INPUT_PULLUP) ;

   attachInterrupt ( digitalPinToInterrupt ( interruptPin ), glow, CHANGE ) ;

} 

void loop ( ) {

   digitalWrite ( ledPin, state ) ;.

}

void glow ( ) 

{

   state = !state ;

}

Example code 2 of Arduino interrupts with falling edge 

 int ledpin = 13 ;

LED is attached to digital pin 13 in Arduino board

int y = 0 ;

Variable to be updated by the interrupt

Void setup ( ) {

attachInterrupt ( 0, increment, Falling);

Enable interrupt 0 on pin 2 which is connected to a button

Jump to the increment function on falling edge

Serial.begin (9600 );

It turn on the serial communication

}

void loop ( ) {

digitalWrite(ledPin, LOW) ;

delay(3000) ;

It required interrupts to works but when the delay is inside the ISR function so it not able to works.

Serial.println(y, DEC) ;

Print y to serial monitor

}

ISR function for interrupt 0

Void increment ( ) {

Y++ ;

digitalWrite ( ledPin, HIGH) ;

}

When the code is start execution and the variable y is incremented then the LED is turned on and after that the program return back to the main loop wherever it was break when the interrupt comes.

Add Comment