On Arduino Interrupts

Posted by:

Arduino Interrupts and some of its Usage

Background and rational

In the process of developing a device, we come across a situation that we needed a responsive way to abort the device; However Arduino will be stuck in the middle of loop and only response after that, esp when the only communication to Arduino is via a serial port. We have adapted a very wasteful way of putting the whole protocol into another for loop and check if there is a abort command from serial stream, every second. This does the job. After some research and playaround, it is clear that interrupt is the way to go. The interrupt is like a asynchronous wake up call from the hotel from desktop. In stead of waking up every so often check the time, have a peaceful sleep and get up upon receiving your wakeup call.

The Definition

-from wikipedia:

An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code the processor is executing.

Most Arduino boards have two external interrupts: numbers 0 [INT0 on digital pin 2] and 1 [INT1 on digital pin 3]; Mega and some other boards have 4 additional as shown in following table.

Board int.0 int.1 int.2 int.3 int.4 int.5
Uno, Ethernet 2 3        
Mega2560 2 3 21 20 19 18
Leonardo 3 2 0 1 7  

Note: It is obvious that the pin you use for interrupt will not be available for other purpose.

The Schematic and Sketch

Following is a very simple example demonstrate the usefulness of interrupt -which solved my problem.

  1. Start with a Fritzing schematic

    interrupt_bb.png

    interrupt_schem1.png

  2. Here is the sketch code

    All it does is to wait for a “s” from serial input and the led pin 13 blinks for 5 times. You can do this via hyperterminal, python pyserial, or simply Arduino serial Monitor. As you can see, the blinking can not be stopped in the middle of the loop (5 blink) when you try to send ‘x’. When you press the button – the intterupt way, the blinking is aborted right way. The watchdog is set at 15ms [ wdt_enable(WDTO_15MS);] in these case. If you set as 8s, you will notice that the board will reset 8sec after you press the button.

The 3rd way to reset the Arduino by code (see link here).

Above sketch lists 2 ways to reset the Arduino, there is one more base on link above, that is,to directly set the reset pin to LOW either via another pin on your Arduino board. Did not try it myself, there are more info at this link.

no_reset.png

All roads leads to Rome.

Obviously, you can define whatever code you want in your function linked to your interrupt. I needed a reset to stopped all the peripherals. Following is using sleep and wakeup to interrupt Arduino (see more here). I hope I can figure out a way to do the wakeup and sleep alternately -then this will be a more elegant way.

Some other ways of using interrupts.

The interrupt can also be used to excute any functions outside loop as shown in this link, and I am sure that I am just barely scratching the surface.

0

Add a Comment