Skip to main content

Interrupt in Detail with Arduino

 What is Interrupt ?

  As in simple language is any continues process is going on and  some this come in between to distract  is called interrupt. This same things also come in microcontroller world. So right here we are talking about interrupt with  microcontroller using Arduino IDE and Arduino UNO board.

  So in microcontroller world, Basically interrupt is an event which come between continues system flow. as example  

  Normally  microcontroller  execute  instructions one after another as you define in main  when interrupt occur , MCU holds execution of instruction in your main program, save its current state so it can reload it later and then procced to interrupt service routine appropriate for that interrupt occurred. when entire  interrupt routine executed, it then  exits the ISR (Interrupt Service Routine) ,Load back to the state of the main program it saved earlier and the continues the execution of main program

 How it works?

  Interrupt is a mechanism through which the processor is informed to stop the current execution of instructions, execute a special event or process and then return back to the original execution.

The main advantage of Interrupts in Arduino (or any microcontroller or processor on that note) is that the processor doesn’t have to continuously poll with the devices whether they need any attention. The device itself “Interrupts” the processor whenever a service is required until which, the processor can perform some other tasks.



If you take the button example once again, originally, the controller was continuously checking for the status of the button. But introducing the concept of Interrupts into this situation, the controller can do whatever it wants (I mean anything other than checking for the status of the button, as per your code) and whenever the button is pressed, it will automatically stop the current execution and puts its full attention to the button press event.

Upon interrupt, I said that the processor executes a special process and this special process is nothing but a set of instructions called Interrupt Service Routine. The structure of an Interrupt Service Routine or ISR is similar to that of any user defined void function.

Types of Interrupts

There are two types of interrupts −

Hardware Interrupts − They occur in response to an external event, such as an external interrupt pin going high or low.
Software Interrupts − They occur in response to an instruction sent in software. The only type of interrupt that the “Arduino language” supports is the attachInterrupt() function.

Interrupts in Arduino

Arduino has three different sources of Interrupts:

  • Timer Interrupts
  • External Interrupts
  • Pin-Change Interrupts

Arduino Statement Syntax

 attachInterrupt(digitalPinToInterrupt(pin),ISR,mode);//recommended for arduino board
attachInterrupt(pin, ISR, mode) ; //recommended Arduino Due, Zero only
//argument pin: the pin number
//argument ISR: the ISR to call when the interrupt occurs; 
   //this function must take no parameters and return nothing. 
   //This function is sometimes referred to as an interrupt service routine.
//argument mode: defines when the interrupt should be triggered.

The following three constants are predefined as valid values −

  • LOW to trigger the interrupt whenever the pin is low.

  • CHANGE to trigger the interrupt whenever the pin changes value.

  • FALLING whenever the pin goes from high to low.

 

const int ledPin = 13;
const int buttonPin = 2;

int ledToggle = LOW;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(buttonPin), button_ISR, CHANGE);
}

void loop() 
{
  delay(300);
}

void button_ISR()
{
  ledToggle = !ledToggle;
  digitalWrite(ledPin, ledToggle);
}


In the above code, the button is connected to Pin 2 (INT0) of Arduino and an interrupt is attached with respect to this pin on an event of CHANGE in the value of the button pin and triggering an ISR named button_ISR.

Comments

Popular posts from this blog

ARDUINO PORTENTA H7 Tutorial

Portena H7 simultaneously run high level code along with real time tasks. H7 main processor is the STMICROELECTRONICS dual core STM32H747  including an ARM CORTEX -M7 running at 480 MHz and ARM COTEXT M4 running at 240MHz. The two core communicate via a Remote Procedure call mechanism that allows calling functions on the other processor seamlessly. The  Portenta H7  simultaneously runs high level code along with real time tasks, since it includes two processors that can run tasks in parallel. For example, it is possible to execute Arduino compiled code along with MicroPython one and have both cores to communicate with one another. The Portenta functionality is two-fold, it can either be running like any other embedded microcontroller board or as the main processor of an embedded computer. There are many features in one module  STM32H747 dual-core processor 8 MB SDRAM 16 MB  NOR FLASH 10/100 ETHERNET PHY USB HIGH SPEED SECURE ELEMENT WIFI/ BLUETOOTH MODULE UFL CO...

Getting Started with mmWave Sensor MR60BHA1 and Arduino

Introduction : The MR60BHA1 mmWave Sensor is a compact and efficient radar sensor that utilizes millimeter-wave technology to detect objects, measure distance, and track motion. With its high precision and low power consumption , this sensor is ideal for applications in robotics, automation, and security systems. . Millimeter-wave (mmWave) sensors operate at extremely high frequencies, typically in the 30GHz to 300GHz range, allowing them to detect objects with great accuracy regardless of lighting conditions. Unlike optical sensors, mmWave sensors can penetrate fog, dust, and even certain materials, making them reliable in harsh environments. The MR60BHA1 is one such sensor that operates at 60GHz , providing precise motion detection and range measurement capabilities. By integrating the MR60BHA1 with an Arduino , users can develop a variety of projects, such as human presence detection , security systems , and robotic navigation . In this blog, we will explore how to interface...

Exploring Color Sensing with Arduino: A Journey into the World of RGB Detection

                           In today's tech-driven world, the Arduino platform continues to empower enthusiasts and professionals alike to explore the realms of electronics and programming. One fascinating avenue within this domain is color sensing using Arduino boards. With the integration of RGB sensors, Arduino opens up a world of possibilities for projects ranging from color sorting machines to ambient light displays. Let's delve into the exciting world of color sensing with Arduino and discover its applications, principles, and how you can embark on your own creative endeavors. Understanding Color Sensing       At the heart of color sensing lies the ability to distinguish between different wavelengths of light. RGB (Red, Green, Blue) sensors are commonly used for this purpose. These sensors typically consis...