Overview

In this tutorial, we will look at Arduino interrupts. Interrupts are useful in many cases where a process simply wants to continue its main function, and other devices (timers or external events) come to its attention if necessary.

Specifications:

  •  ATMEGA2560  Microcontroller
  •  Operating Voltage: 5V
  •  Input Voltage (recommended): 7-12V
  •  Input Voltage (limits): 6-20V
  •  Digital I/O Pins: 54 (of which 14 provide PWM output)
  •  Analog Input Pins :16
  •  DC Current per I/O Pin: 40 mA
  •  DC Current for 3.3V Pin: 50 mA
  •  Flash Memory: 256 KB (of which 8 KB used by bootloader)
  •  SRAM: 8 KB
  •  EEPROM: 4 KB
  •  Clock Speed: 16 MHz

Digital Pins connection:

S.NoGpio_pin
1.Digital pin 2 ( INPUT )
2.Digital pin 3 ( INPUT )
3.Digital pin 13 ( OUTPUT)

Types of Interrupts

There are two types of interrupts:

Hardware Interrupt – This happens when an external event occurs, such as an external interrupt pin changing its state from LOW to HIGH or HIGH to LOW.

Software Interrupt: happens according to the instructions of the software. For example, timer interrupts are software interrupts.

Example Implementation

We will consider button interrupts. More specifically, interrupts that are triggered when a rising or falling edge is detected on one of the pins. Now, not all pins of your board can be used for interrupting the code. Each board has some specific pins reserved for external interrupts. The list can be found here: attachInterrupt() – Arduino Reference

Digital Pins With Interrupts

As you can see from the above link, pins 2 and 3 can be used for external interrupts on Arduino Uno.

Now, interrupts are supposed to be executed very fast. Therefore, all we will do inside the interrupt is set a flag. And in the loop, we will print a statement whenever the flag is set, and then set the flag back to 0.

The main function of importance is attachInterrupt().

The syntax is − attachInterrupt(digitalPinToInterrupt(pin), myISR, mode)

Here, the pin is the pin number, ISR is the name of the ISR function, and the mode can be one of the following −

  • RISING: Whenever a low to high transition is seen on the pin
  • FALLING: Whenever a high to low transition is seen on the pin
  • LOW: Whenever the pin is at a low voltage
  • CHANGE: Whenever the pin sees a change in the voltage (high to low or low to high)

Source Code

#include <stdio.h>

const int LED_PIN = 13;
const int INTERRUPT_PIN_2 = 2;
const int INTERRUPT_PIN_3 = 3;

int __FLAG;

void setup() {
   __FLAG=0;
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(INTERRUPT_PIN_2, INPUT_PULLUP);
pinMode(INTERRUPT_PIN_3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN_2), myISR, FALLING); // trigger when button pressed, but not when released.
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN_3), myISR, FALLING); // trigger when button pressed, but not when released.
Serial.println("Hellow Devilal");       // prints a label

}

void loop() {
 digitalWrite(LED_PIN, 0); //pin 13 equal the state value
if(__FLAG){
      Serial.print("Button Pressed!");
      Serial.print("\t\t");
      Serial.println(__FLAG);
      digitalWrite(LED_PIN, 1); //pin 13 equal the state value
    __FLAG=0;
   }
   
  Serial.print("__FLAG\t\t");       // prints a label
  Serial.println(__FLAG);       // prints a label
  delay(1000);

}

void myISR() {
  __FLAG=1;
  
    Serial.print("interrupt Generated");       // prints a label
    Serial.print("\t\t");       // prints a label
    Serial.println(__FLAG);       // prints a label


}

Output images

By Devilal

Leave a Reply

Your email address will not be published. Required fields are marked *