Arduino Projects    |   Raspberry Pi  |     Electronic Circuits   |    AVR    |    PIC    |    8051    |    Electronic Projects

How to Use Interrupts with Arduino Board

Summary

Developed By: 
Ajish Alfred

Any AVR microcontroller based board which follows the standard Arduino schematic and is flashed with the Arduino boot-loader can be called an Arduino board. The Arduino is an easy prototyping platform in which the hardware is very simple to use and to be connected with any other system. The programing environment is also very easy to start with and has lot of built-in functions for every simple and complex task. The output or inputs can be taken from the boards or given to the board using convenient connectors. Both digital and analog inputs and outputs are available in all Arduino boards. The Arduino boards can also communicate with other devices using standard communication ports like USART, IIC, and USB etc. The Arduino board also provides pins which can be used as external hardware interrupts pins.

The interrupt is a method by which a microcontroller is made to divert from current path of code execution to do some other important things that needs immediate processing. The code which is written to process whenever an interrupt occurs is called an Interrupt Service Routine (ISR). Once an interrupt occurs the controller stops its current processing and start executing the ISR. This project demonstrates how to use the external interrupt pin of an Arduino board by changing the status of an LED each time the interrupt occurs.

How To Use Interrupts With Arduino Board

Description

This article discusses the external interrupt based on the Arduino pro-mini board which is programmed using the Arduino IDE version 1.0.3 downloaded for windows. The advantage of this board is that it comes in very small in size; different kind of connectors can be soldered according to the application requirements. It is very breadboard friendly and occupies very less space of a typical breadboard. The board also provides two external interrupt pins.

The image of the Arduino pro-mini board and the screen shot of the Arduino IDE are shown below;

Arduino

 

Arduino

Since the arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the arduino board and also helps in the serial communication with the PC through the USB port of the PC.

Arduino

It is assumed that the reader has gone through the project how to get started with the arduino and done all the things discussed in it. The Arduino pro-mini board has two external pins namely Pin2 and Pin3 which normally acts as digital I/O pins but can be configured as interrupt pins also. An interrupt can be triggered in pin2 or pin3 using the following three basic methods

{C}{   ·   Interrupt occurs when the pin is connected to a low valued voltage (low voltage triggered)

{   ·   Interrupt occurs when voltage at the pin changes from high value to a low value (falling edge triggered)

{C    ·   Interrupt occurs when voltage at the pin changes from low value to high value (rising edge triggered)

{C}     ·  Interrupt occurs whenever the voltage transition occurs at the pin i.e. interrupt for both low to high and high to low transitions (state change triggered)

The interrupt pins are usually connected with pull-up or pull-down resistors depending on the triggering method used by the interrupt pin. The pull-up resistor is used when the pin uses low voltage triggering or falling edge triggering. The pull-down resistors are used when the interrupt pin uses rising edge triggering. The project uses rising edge triggering and the triggering circuitry is very simple using a push button and a pull down resistor of value 10K. The Arduino IDE provides some functions to make use of the interrupt features of the Arduino board. The code written for this project make use of functions attachInterrupt() and detachInterrupt() which can be used to enable an interrupt on either of the two external interrupt pins or disable an already enabled interrupt respectively. The details of the functions are discussed in the following section;

attachInterrupt()

The function attachInterrupt() is used to enable an interrupt on either of the two external interrupt pins along with configuring the type of triggering and also assigning an ISR to be executed whenever an interrupt occurs. The function has three variables, the first one is the pin number at which the interrupt signals expected to appear and the second one is the function which can act as the ISR. The third parameter defines the method of triggering.

The prototype of the function is given as the following;

void attachInterrupt ( interrupt_pin, isr_function, trigger_method );

The return type of the function is declared as void and hence the function returns nothing. The first parameter is the interrupt pin which determines at which pin the interrupt should be enabled as mentioned below;

{C}{C}{C}{C}      -   interrupt_pin

0 for enabling the interrupt on pin2

1 for enabling the interrupt on pin3

The second parameter points to the function which should be made as the ISR. The parameter should be provided with the name of the function which is required to act as an ISR.

The third parameter defines the kind of triggering that should be enabled on the interrupt pin mentioned by the first parameter. The four different values and their details that can be applied to the third parameter are given below;

{C      -  trigger_method

LOW to trigger the interrupt whenever the pin is low,

CHANGE to trigger the interrupt whenever the pin changes value

RISING to trigger when the pin goes from low to high,

FALLING for when the pin goes from high to low.

detachInterrupt()

The function detachInterrupt() is used to disable the interrupt on either of the interrupt pin at which the interrupt is already enable by the function attachInterrupt(). The function has no return value but one parameter which is the pin number on which the interrupt should be disabled.

For example to disable the interrupt on the pin number 2 one can use the following statement;

detachInterrupt(0);

To disable the interrupt on the pin number 3 one can use the following statement;

detachInterrupt(0);

THE CODE

The code initializes the pin number 6 as output at which an LED is connected using the function pinMode() and later in the code it continuously write a value to that particular pin using the function digitalWrite() which are already discussed in a previous project on how to get started with the Arduino.

The function attachInterrupt() is used to enable the interrupt on the pin number 2 with assigning an ISR function. The pin is configured as rising edge triggered. Each times an interrupt occurs the ISR inverts the value that is continuously written to the pin number 6. There is also a condition written inside the function loop() which will continuously check a count to become greater than 10 which is incremented inside the ISR. After 10 interrupts the condition becomes true and it disables the interrupt using the function detachInterrupt().

The code also displays some text on the LCD with the help of the functions from the library <LiquidCrystal.h>. The important functions provided by the library <LiquidCrystal.h> are already used and explained in previous projects on how to interface an LCD, how to display sensor value on LCD, how to connect the LCD with the PC and how to make an LCD scrolling display.

When the coding is finished one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino. The interrupt can be triggered by pressing the push button which will make a low to high transition on the interrupt pin. The ISR runs each time the interrupt occurs and one can observe the LED changing its state from off state to on or from on state to off for the first ten key press.

Circuit Diagram

Video

Code

This Code is only visible to Registered users. Please Login/Register

 

Components

Resistor Image
Resistor

Resistor is a passive component used to control current in a circuit. Its resistance is given by the ratio of voltage applied across its terminals to the current passing through it. Thus a particular value of resistor, for fixed voltage, limits the current through it. They are omnipresent in electronic circuits.

...
LEDs | Light Emitting Diode
LED
 
Light emitting diodes (LEDs) are semiconductor light sources. The light emitted...
16 x 2 LCD | 16x2 Character LCD Module
LCD

 

LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of applications. A 16x2 LCD display is very basic module and is very commonly used in various devices and circuits. These modules are preferred over seven segments...

Arduino Pro Mini
Arduino Pro Mini

The Arduino Pro Mini is an ATmega168 based microcontroller board. The board comes with built-in arduino bootloader. It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 8 analog inputs, an on-board resonator, a reset button, and holes for mounting pin headers....

You are here