In a basic Arduino sketch, if you want to take action based on the status of a digital input pin, you’d check the state of the pin repeatedly until a particular condition is met. However, as your sketches become increasingly complex, you’ll find that it’s not always the best way to do this. EngBlaze posted a pairof guides to using interrupts so that you can execute code asynchronously in Arduino sketch. Here’s the analogy they use to illustrate the difference:

Imagine you’re sitting on your couch, enjoying a frosty brew and watching a movie after a long day. Life is good. There’s only one problem: you’re waiting for an incredibly important package to arrive, and you need it as soon as possible. If you were a normal AVR program or Arduino sketch, you’d have to repeatedly stop your movie, get up, and go check the mailbox every 5 minutes to make sure you knew when the package was there.

Instead, imagine if the package was sent Fedex or UPS with delivery confirmation. Now, the delivery man will go to your front door and ring the doorbell as soon as he arrives. That’s your interrupt trigger. Once you get the trigger, you can pause your movie and go deal with the package. That’s your interrupt service routine. As soon as you’re done, you can pick up the film where you left off, with no extra time wasted. That’s the power of interrupts.

Matt Richardson is a San Francisco-based creative technologist and Contributing Editor at MAKE. He’s the co-author of Getting Started with Raspberry Pi and the author of Getting Started with BeagleBone.

A good example of interrupt use is the firmware in the repraps and makerbots. It uses interrupts to jump back and forth between running the steppers as well as receiving data and handling requests for information like temperature and location.

Valerio Savioli

Hi,
this is a very good and clear explanation about timer interrupt.
It helped me to understand how interrupt work.
Thanks for the code ready for use.
Valerio

Fred

I make software tools for the telephony industry. The way I’ve explained this for the last 20 years has been “you are waiting for an important phone call”. You don’t keep lifting up the receiver every 5 minutes to see if someone is there. And if your calls are short, you could miss the call or have to increase the frequency of your checking. Interrupts are the phone ringing. It is pretty basic. I can’t imagine life without interrupt driven programming or “event driven” as we say in Visual Studio