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 pair of 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.

The first guide covers using hardware interrupts, such as switches and the second discusses timer interrupts, so that you don’t have to repeatedly check if a certain amount of time has passed when executing code on a particular interval. The guides are complete with code examples to give you a jumpstart on your own interrupt-driven Arduino project. [via Reddit]

2 Responses to How-To: Arduino Interrupts

  1. 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.

  2. Valerio Savioli on said:

    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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

%d bloggers like this: