Home Arduino Platform Arduino - Input / Output Basics Arduino Example #9 - Using Interrupts
Print E-mail
User Rating: / 206
Arduino - Arduino - Input / Output Basics
Written by Dave Auld   
Friday, 19 February 2010 00:00

Arduino Basic Input Output Circuits

Using Interrupts With the Arduino

What is an interrupt?

Interrupts are a method of signaling to the microprocessor that something has happened. However, you may ask yourself, is that not what happens anyway when you use a digital input etc.? Well quite simply - No.
When you use the likes of a digital input, you will typically read its value by issuing an instruction, then act on the read value by using some form of logic, i.e. the code is polling for a value. Depending on the complexity of your routines, and the duration of the state change on the input it is quite possible to not see the change on the input occur at all.
By using an interrupt, the code is literally interrupted, and forced to branch off and do some other execution. i.e. the state change on the input is not 'missed'. So interrupts are like a hardware trigger.


What Benefit Are They?

Interrupts can help solve timing issues which may occur within your code/hardware setup. You may have had timing issues already and just not understood what happened. How many times have you said to yourself, "Why didn't that fire?" or "It worked last time I ran it. What is different."


Getting Down To Business

I am using the Arduino Duemilanove Board for this example, and will be using the Release 18 of the development IDE. This can be downloaded directly from Arduino.cc.
If you are using a different type of Arduino Board, you will need to check out the specs to see which pins are which, as the different types of board can have diffirent allocations and numbers of interrupts/digital pins/analog pins etc.

On the Duemilanove, there are 2 hardware interrupts available. These are located on Digital Pins 2 and 3. In this demo, we will use Pin 2, and also use Digital Pin 4 as an output to control an LED. The schematic for this is shown below;



Standard Digital Input and Output - No Interrupts

If you set up the Arduino as per the schematic and upload the code below to the microprocessor. Here you read the value of an input, do a conditional comparison, run some lengthy routine and repeat.
This will given unpredicatble outputs on the LED due to the lengthy process being at an undetermined point in relation to when the input button is triggered. Sometimes the LED will change state immediately, other times nothing happens, and then sometimes you need to hold the button for a while for the state changed to be recognised.

int pbIn = 2;          // Digital input on pin 2
int ledOut = 4;        // The output LED pin
int state = LOW;       // The input state
void setup()
  // Set up the digital Pin 2 to an Input and Pin 4 to an Output
  pinMode(pbIn, INPUT); 
  pinMode(ledOut, OUTPUT);
void loop()                     
  state = digitalRead(pbIn);      //Read the button
  digitalWrite(ledOut, state);    //write the LED state
  //Simulate a long running process or complex task
  for (int i = 0; i < 100; i++)
     // do nothing but waste some time

Making Use of Interrupts

Now we will use the same schematic diagram and modify the code to make use of hardware interrupts. Now when you upload the code, the LED changes state whenever the button is pressed. Even though the code is still running the same long delay in the main loop.

int pbIn = 0;                  // Interrupt 0 is on DIGITAL PIN 2!
int ledOut = 4;                // The output LED pin
volatile int state = LOW;      // The input state toggle
void setup()
  // Set up the digital pin 2 to an Interrupt and Pin 4 to an Output
  pinMode(ledOut, OUTPUT);
  //Attach the interrupt to the input pin and monitor for ANY Change 
  attachInterrupt(pbIn, stateChange, CHANGE);
void loop()                     
  //Simulate a long running process or complex task
  for (int i = 0; i < 100; i++)
    // do nothing but waste some time
void stateChange()
  state = !state;
  digitalWrite(ledOut, state);  

The volatile keyword is added to the state variable, this causes the compiler to use RAM instead of a storage register. This is done because the storage register can be temporarily inaccurate if they are being modified by areas other than the main thread. In the Arduino, this relates to code being triggered by interrupts.

The attachInterrupt(param1, param2, param3) requires 3 parameters, these are;
param1 = Which interrupt to listen for. This is the Interrupt Number not the Digital In number
param2 = Which code function to call, this must be a method that takes no parameters and returns no value.
param3 = Which condition to watch for.

The Arduino can listen for 4 types of condition changes. These are;
LOW = The input is at a LOW state
RISING = The input state changes from LOW to HIGH
FALLING = the input state changes from HIGH to LOW
CHANGE = The input state changed from HIGH to LOW or LOW to HIGH, i.e. has changed its state


ReAssigning Interrupts

Interrupts can be changed at any point by using the attachInterrupt() method. As soon as this is done, any previously assigned interrupt on the associated pin is removed.


Starting / Stopping Interrupts

The Arduino also has the ability to temporarily ignore all the interrupts. You may want to do this if you have some sensitive code that must be executed without interruption. In this case you would issue a noInterrupts() call. Once your sensitive code block has completed, interrupts can be restarted by calling interrupts().


Removing Interrupts

Interrupts can also be removed by using the detachInterrupt(interrupt_number) method.


So, that is it, a quick basic introduction to hardware Interrupts on the Arduino platform. Now, you just need to see how they fit in with your projects, and how you can make use of them.


Last Updated on Saturday, 20 February 2010 16:39


+26 # 2010-05-28 16:39
Thanks for putting this on the internets. This was the best, easiest to understand tutorial out there on this subject.

I upgraded the code a bit so that it works every time, I think I was having issues with debounce and getting a false state locked in. If you want, feel free to email me and I will send it to you.

Reply | Reply with quote | Quote
-1 # 2013-06-10 19:41
Why is interrupt 0 binded to Digital Pin 2 ?

Can I do interrupt using other Pin ? I'm pretty sure you didn't guessed that, where did you find that Interrupt 0 = DP2 ?
Reply | Reply with quote | Quote
+2 # Dave Auld 2013-06-10 19:59
Hi Francois,

If you look at the Reference pages on Arduino.cc to the AttachInterrupt section, it lists the interrupt pin vs digital pin for each type or arduino.


Reply | Reply with quote | Quote
0 # 2015-01-10 23:25
I am having trouble with the code when adding 3 interrupt. Can you guide me as to the correct way to write the codes for more than 1 interrupt
Reply | Reply with quote | Quote
0 # Dave Auld 2015-01-11 18:05
Hi Sheldon, this depends on what sort of device you are using. The different versions have different numbers of interrupts to use.

You will need to look at the particular device you are using and checked the hardware pin number for the interrupt number and modify the code to fit.
Reply | Reply with quote | Quote
0 # 2015-03-28 23:53
Hello, is it possible to show us a simple code without hardware interrupt, I want to program an interruption based on an internal event.
attachInterrupt () is only for hardware ??
Reply | Reply with quote | Quote
0 # Dave Auld 2015-05-26 07:33
Use a variable that is updated by one block of code, the other part of the code that you want to interrupt then regularly checks this variable state.
Reply | Reply with quote | Quote
0 # JunkyardDonald 2015-05-20 17:30
Well, I've been horsing around with the Arduino Duemilanove for a couple years now and finally decided to see if intrrupts would be of any use for my increasingly larger code segments and I have to agree with Mike, (posting on 5/28/2010) that this was one fine concise, crash course dealing with interrupts. Many thanks to Dave, that's for sure.
Reply | Reply with quote | Quote
0 # paul 2015-05-25 20:22
I have been literally working for days trying to get the line attachIterrupt( 0,Sam,RISING) to compile. I trigger it with void() Sam;{a=2)}. any idea what I could be doing wrong? Tnx
Reply | Reply with quote | Quote
0 # Dave Auld 2015-05-26 07:37
Hi Paul,

there shouldn't be a semi colon in the function definition you are trying to call, maybe this is just a typo on the posting and you do not actually have it in the code.

void sam() { //do something }


void sam(); { // do something }
Reply | Reply with quote | Quote
0 # paul 2015-05-25 20:28
I have been trying for hours to get an interrupt to work. I am doing attach interrupt(o,sam ,RISING). I'm answering it with void() sam;{a=0}. … it errors out compiling the attach interrupt statement.
Reply | Reply with quote | Quote

Add comment

The site owners has the right to edit/delete or use the comment in any way for which they choose.
The comments may be moderated before going public.

Security code

Copyright © 2015 Dave-Auld.net. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
Find us on Facebook
We have 30 guests online