You are on page 1of 5

University of Engineering and Technology, Lahore Spring 2018

ARDUINO 3: INTERRUPTS

Name : Date :
Regd-No :
OBJECTIVES:
▪ Learn what an interrupt is
▪ Program Arduino using hardware interrupts.
SUGGESTED READING:
▪ Lab Lecture about interrupts
▪ http://playground.arduino.cc/Code/Interrupts
▪ http://arduino.cc/en/Reference/attachInterrupt
▪ http://www.engblaze.com/we-interrupt-this-program-to-bring-you-a-tutorial-on-arduino-
interrupts/
▪ http://www.dave-
auld.net/index.php?option=com_content&view=article&id=107:arduino-
interrupts&catid=53:arduino-input-output-basics&Itemid=107
▪ http://www.gammon.com.au/forum/?id=11488

Please read through all the suggested reading before you come to lab.

EQUIPMENT AND COMPONENTS:


▪ Arduino™ UNO or similar Board
▪ Standard A-B type USB cable
▪ Push buttons
▪ LEDs
▪ Jumper wires
---------------------------------------------------------------------------------------------------------------------
Introduction to Interrupts:
On a very basic level, an interrupt is a signal that interrupts the current processor
activity. It may be triggered by an external event (change in pin state) or an internal
event (a timer or a software signal). Once triggered, an interrupt pauses the current
activity and causes the program to execute a different function. This function is
called an interrupt handler or an interrupt service routine (ISR). Once the
function is completed, the program returns to what it was doing before the interrupt
was triggered.
The AVR chips used in most Arduinos are not capable of parallel processing, i.e.
they can’t do multiple things at once. Up till now, we have programmed Arduino to
work in a sequence. That means that it will execute a sequence of instructions in

MCT-222: EMBEDDED SYSTEMS


1
ARD 4
University of Engineering and Technology, Lahore Spring 2018

a specific order, from start to end, and then repeat the process. Such programming is
known as ‘synchronous’ or ‘sequential’, because the controller is following a
specific pattern. Meanwhile it is taking logical decisions based on external (buttons,
sensors) and internal (delays, timers) events that occur during the execution of the
code. As you might already have noticed, the processor gets tied down performing
a specific task, and cannot detect any other change that occurs during that time.
A better way to program Arduino is to use an ‘asynchronous’ programming method
using interrupts. Interrupts are special events that alter the code flow. The processor
pauses whatever task it is doing and executes a special function (known as ISR or
Interrupt Service Routine). After execution of the ISR, the processor resumes
whatever it was doing before the interrupt event was generated (Fig.8.1)

Fig.4.1: Non-Interrupt v.s Interrupt sequence

Types of Interrupts:
There are two main types of interrupts:
▪ Hardware interrupts, which occur in response to an external event, such as
an input pin going high or low
▪ Software interrupts, which occur in response to an instruction sent in
software
We will discuss the hardware interrupts for now, that can be programmed using
built-in Arduino IDE functions (wiring library).

MCT-222: EMBEDDED SYSTEMS


2
ARD 4
University of Engineering and Technology, Lahore Spring 2018
Using Hardware Interrupts:
Arduino provides “attachinterrupt()” function for attaching interrupts to specific
pins. Specifies a named Interrupt Service Routine (ISR) to call when an interrupt
occurs. Replaces any previous function that was attached to the interrupt. Most
Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on
digital pin 3). The table below shows the available interrupt pins on various boards.

Note:
Inside the attached function, delay() won't work and the value returned by
millis() will not increment. Serial data received while in the function may be lost.
You should declare as volatile any variables that you modify within the attached
function. See the section on ISRs below for more information.

ISRs are special kinds of functions that have some unique limitations most other
functions do not have. An ISR cannot have any parameters, and they shouldn't return
anything.

Syntax:
attachInterrupt(interrupt, ISR, mode);

Parameters:

interrupt: the number of the interrupt (int)

ISR: the ISR to call when the interrupt occurs; this function
must take no parameters and return nothing. This
function is sometimes referred to as an interrupt service
routine
mode: defines when the interrupt should be triggered. Four
constants are predefined as valid values:
• LOW to trigger the interrupt whenever the pin is
low,
• CHANGE to trigger the interrupt whenever the
pin changes value

MCT-222: EMBEDDED SYSTEMS


3
ARD 4
University of Engineering and Technology, Lahore Spring 2018
• RISING to trigger when the pin goes from low
to high,
• FALLING for when the pin goes from high to
low.

Returns:
none

Example 4_1:

/*
* Simple code to demonstrate the use of
* interrupts to control the program flow.
*/

const byte LED = 13; // declaring a constant


const byte BUTTON = 2;

// Interrupt Service Routine (ISR)


void pinChange ()
{
if (digitalRead (BUTTON) == HIGH)
digitalWrite (LED, HIGH);
else
digitalWrite (LED, LOW);
} // end of pinChange

void setup () Fig.4.2: Push button with interrupt


{
pinMode (LED, OUTPUT); // so we can update the LED
digitalWrite (BUTTON, HIGH); // internal pull-up resistor
attachInterrupt (0, pinChange, CHANGE); // attach interrupt handler
} // end of setup

void loop ()
{
// loop doing nothing
}

The main reasons you might use interrupts are:


▪ To detect pin changes (eg. rotary encoders, button presses)

MCT-222: EMBEDDED SYSTEMS


4
ARD 4
University of Engineering and Technology, Lahore Spring 2018
▪ Watchdog timer (eg. if nothing happens after 8 seconds, interrupt me)
▪ Timer interrupts - used for comparing/overflowing timers
▪ SPI data transfers
▪ I2C data transfers
▪ USART data transfers
▪ ADC conversions (analog to digital)
▪ EEPROM ready for use
▪ Flash memory ready

TASK 4_1:
▪ Modify Task 1_1 and Task 1_4 to incorporate interrupts on buttons used.

MCT-222: EMBEDDED SYSTEMS


5
ARD 4

You might also like