fbpx

Interrupt using Arduino Uno

Interrupt is one of the powerful concept in microcontroller application

  • If interrupt occurs in microcontroller, controller completes the execution of the current instruction and starts the execution of an Interrupt Service Routine (ISR).
  • ISR is a piece of code that tells the controller what to do & when the interrupt occurs.
  • After the execution of ISR, controller returns back to the instruction.

Types Of Interrupt

  • Two type of interrupt
    • Hardware interrupt.
    • Software interrupt.

Hardware interrupt.

  • If the interrupts are generated by the interfaced devices like switch is called hardware interrupt.

Software interrupt.

  • Interrupts are generated by a piece of code is called software interrupt.

Arduino Interrupt

  • In arduino it have predefined functions to access the interrupt.
  • noInterrupts() – Disables interrupts
  • Some functions will not work while interrupts are disabled, and incoming communication may be ignored.
  • attachInterrupt()
  • Interrupt Service Routine (ISR) to call when an interrupt occurs.
  • detachInterrupt()
  • Turns off the given interrupt.
  • Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3).

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.

Code

 int led = 6;
 int led_state = 0;
 int count = 0;
 void setup()
 {
 pinMode(led, OUTPUT);
 // enable the external interrupt 0, with function 'glow' as ISR and interrupt occurs on rising edge
 attachInterrupt(0, glow, RISING);
 }
 void loop()
 {
 digitalWrite(led, led_state);
 if(count > 10)
 {
 detachInterrupt(0);
 }
 else;
 }
 //========== ISR ============//
 void glow()
 {
 count ++;
 led_state = !led_state;
 }

Leave a Reply

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