How-To: Arduino Interrupts

Arduino Technology
How-To: Arduino Interrupts


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]

8 thoughts on “How-To: Arduino Interrupts

  1. Marc says:

    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 says:

    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

  3. Fred says:

    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

  4. Project Process: November -December | Christiana's Class site says:

    […] Someone recommended Blinkwithoutdelay which I tried to research, but then another class mate told me to scrap that idea and use interrupts instead. […]

Comments are closed.

Discuss this article with the rest of the community on our Discord server!
Tagged

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.

View more articles by Matt Richardson

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK