Sunday, 25 April 2010

Arduino External Interrupts

Overview 
In this tutorial I will be showing you how to get your 
Arduino Diecimila to respond an external interrupt. When the interrupt occurs, the Arduino will toggle the state of an LED. The external interrupt we will be using will be INT0, which is Digital Pin 2 on the Arduino.

Here is what we'll use:

  • Arduino Diecimila with usb cable (or a Freeduino, which is 100% Arduino Diecimila compatible)
  • Solderless breadboard
  • Several jump wires
  • 220 Ohm resistor
  • Push switch (optional)
All components other than the Arduino should be easily purchased at your local hobby electronics store (such as Maplin in the UK).
This tutorial assumes the user is able to run the Arduino environment and run a program on the arduino board. For further information please see 
Getting Started with Arduino.

Arduino External Interrupts
Atmega168 micro-controller on the Arduino Diecimila boards have two external interrupts available: INT0 and INT1, located on digital pins 2 and 3 respectively.
The Atmega168 supports four trigger modes for external interrupts:
  • LOW -  a low level trigger
  • CHANGE - a change in level trigger
  • RISING - a rising edge of a level trigger
  • FALLING - a falling edge of a level trigger



For this tutorial we will be using a FALLING trigger mode. So, digital pin 2 will be connected to 5v via a pull-up resistor. When we want to generate the interrupt we will connect the pin to ground.

When configuring our external interrupt we will use the Arduino library function attachInterrupt(interrupt, function, mode). This function takes three parameters:
  • interrupt - This is the interrupt source, either 0 for INT0 or 1 for INT1;
  • function - The function to call when the interrupt occurs;
  • mode - The trigger mode as specified above;

External Interrupt Circuit
The following is the schematic of the circuit we will be using:

The following is a picture of the circuit as I have set it up on the bread board:

Notes On This Circuit
  • You don't need a push switch, you could replace the switch with a wire that you connected to ground in order to fire the interrupt.
  • We will have to implement software debouncing in order to prevent the interrupt from firing multiple times as the switch contacts settle.


Source Code
int interrupt_pin = 2;
int led_pin = 2;

volatile int toggle = 0;

/***************************************************
 *  Name:        pin2Interrupt
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Service routine for pin2 interrupt  
 *
 ***************************************************/
void pin2Interrupt(void)
{
  /* This will bring us back from sleep. */
  toggle = 1;
  
  /* Delay in here as a very crude debouncing mechanism. */ 
  delay(100);
}



/***************************************************
 *  Name:        setup
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Setup for the Arduino.           
 *
 ***************************************************/
void setup()
{
  Serial.begin(9600);
  
  /* Setup the interrupt pin */
  pinMode(interrupt_pin, INPUT);
  attachInterrupt(0, pin2Interrupt, FALLING);
  
  Serial.println("Initialisation complete.");
}



/***************************************************
 *  Name:        loop
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Main application loop.
 *
 ***************************************************/
int seconds=0;
void loop()
{
  if (toggle == 1)
  {
    Serial.println("Toggle");
    digitalWrite(led_pin, !digitalRead(led_pin));
    toggle = 0;
  }
}

The source file can be downloaded from here.

3 comments:

  1. Hi,

    I think I noticed a bug in this code as it doesn't toggle the LED. I think it should be:

    digitalWrite(led_pin, !digitalRead(led_pin));

    Cheers

    Mase

    ReplyDelete
  2. Hi Mase,
    Yes, you are right:
    http://www.arduino.cc/en/Reference/digitalWrite
    Thank you, I have updated the code.
    Donal

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete