News

New Products

Industry News

Articles

Product Reviews

Tech Articles

Tutorials

Interviews

Case Studies

Digi-Key Store

Cables and Wires

LED/Optoelectronics

Connectors and Interconnect

Discrete

Electromechanical

Integrated Circuits (ICs)

Embedded Computers

Enclosures, Hardware and Office

Isolators

RF
and Wireless

Power and Circuit Protection

Programmers

Passives

Sensors and Transducers

Semiconductors

Test
Products

Tools

Projects

555 Timer Circuits

Op-amp Circuits

Audio Circuits

Power Supply Circuits

All Electronic Circuits

Arduino Projects

Raspberry Pi Projects

MSP430 Projects

STM32 Projects

ESP8266 Projects

PIC Projects

AVR Projects

8051 Projects

ESP32 Projects

NodeMCU Projects

IoT Projects

PCB Projects

Arduino ESP8266 Projects

All Microcontroller Projects

Forum

Contact

Editorial

Advertise

About Us

More

Partner Events and Conferences

EE Calculators

Login

Register

Arduino
Interrupts Tutorial

Arduino

ByPramoth
Thangavel Feb 12, 2019
2

Consider a fast moving car, if it suddenly gets hit by an
another car in opposite direction, the first thing happens is the accelerometer
sensor present in the car senses a sudden de-acceleration and triggers an
external interrupt to the microcontroller present in the car. Then MCU produces
an electric signal to deploy the air bags immediately. Microcontroller present
in the car monitor many things simultaneously like sensing speed of the car,
checking other sensors, controlling air conditioner temperature etc. So what
makes a sudden opening of air bag in seconds? An interrupt
signal is used here which has the highest priority of all.

Another simple example of Interrupts is
touch screen mobile phones which has the highest priority to the “Touch” sense.
Almost every electronic device has some kind to interrupts to ‘interrupt’ the
regular process and do some higher priority things on particular event. The
regular process is resumed after serving the Interrupt.

So technically, Interrupts is a mechanism by which
an I/O or an instruction can suspend the normal execution of processor and gets
itself serviced like it has higher priority. For example a processor
doing a normal execution can be interrupted by some sensor to execute a
particular process that is present in ISR (Interrupt Service Routine). After
executing the ISR processor can again resume the normal execution.

Types of Interrupts

There are two types of interrupts:

Hardware Interrupt: It happens when an
external event is occurred like an external interrupt pin changes its state from
LOW to HIGH or HIGH to LOW.

Software Interrupt: It happens according
to the instruction from the software. For example Timer interrupts are
software interrupt.

Interrupts in Arduino

Now we will see how to use interrupts in
Arduino Board. It has two types of interrupts:

External Interrupt

Pin Change Interrupt

External Interrupt:

These interrupt are interpreted by hardware and are very
fast. These interrupts can be set to trigger on the event of RISING or FALLING
or LOW levels.

Arduino Board

External Interrupt pins:

UNO , NANO

2,3

Mega

2,3,18,19,20,21

Pin Change Interrupts:

Arduinos can have more interrupt pins enabled by using
pin change interrupts. In ATmega168/328 based Arduino boards
any pins or all the 20 signal pins can be used as interrupt pins. They can also
be triggered using RISING or FALLING edges.

Using Interrupts in Arduino

In order to use interrupts in Arduino the following
concepts are need to be understood.

Interrupt Service Routine (ISR)

Interrupt Service Routine or an Interrupt handler is an
event that has small set of instructions in it. When an external interrupt
occurs, the processor first executes these code that is present in ISR and
returns back to state where it left the normal execution.

ISR has following syntax in Arduino:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

digitalPinToInterrupt(pin): In Arduino
Uno, NANO the pins used for interrupt are 2,3 & in mega 2,3,18,19,20,21.
Specify the input pin that is used for external interrupt here.

ISR: It is a function that is called when
an external interrupt is done.

Mode: Type of transition to trigger on,
e.g. falling, rising, etc.

RISING:  To trigger an interrupt when the pin
transits from LOW to HIGH.

FALLING: To trigger an interrupt when the pin transits
from HIGH to LOW.

CHANGE: To trigger an interrupt when the pin transits
from LOW to HIGH or HIGH to LOW (i.e., when the pin state changes ).

Some Conditions while using Interrupt

Interrupt Service Routine function (ISR) must be as
short as possible.

Delay () function doesn’t work inside ISR and should be
avoided.

In this Arduino Interrupt tutorial, a
number is incremented from 0 and two push buttons are used to trigger Interrupt,
each one is connected to D2 & D3. A LED is used to indicate the Interrupt.
If one push button is pressed the led goes ON and display shows
interrupt2 and goes off, and when another push button is
pressed the led goes OFF and the display shows interrupt1 and goes
off.

Components Required

Arduino Board (In this tutorial Arduino NANO is
used)

Push button - 2

LED - 1

Resistor (10K) - 2

LCD (16x2) - 1

Bread Board

Connecting Wires

Circuit Diagram

Circuit Connection between Arduino Nano and 16x2
LCD display:

LCD

Arduino Nano

VSS

GND

VDD

+5V

V0

To Potentiometer Centre PIN

For Controlling Contrast of the LCD

RS

D7

RW

GND

E

D8

D4

D9

D5

D10

D6

D11

D7

D12

A

+5V

K

GND

Two push buttons are connected to Arduino
Nano at pin D2 & D3. They are used for using two external
interrupts, one for turning LED ON and another for turning OFF a LED. Each push
button has a pull down resistor of 10k connected to ground. So
when push button is pressed it is logic HIGH (1) and when not pressed it is
logic LOW (0). A Pull down resistor is compulsory otherwise there will be
floating values at the input pin D2 & D3.

A LED is also used to indicate that a
Interrupt has been triggered or a button has been pressed.

Arduino Interrupt Programming

In this tutorial a number is incremented from 0 which
displays continuously in (16x2) LCD connected to the Arduino Nano, whenever the
left push button (interrupt pin D3) is pressed the LED goes ON and display shows
Interrupt2, and when the right push button (interrupt pin D2) is pressed the LED
goes OFF and display shows Interrupt1.

Complete Code with a working video is given at the
end of this tutorial.

1. First the header file for the LCD
display is included and then the LCD pins that are used in connecting with the
Arduino Nano are defined.

#include<LiquidCrystal.h>
LiquidCrystal lcd (7,8,9,10,11,12); // Define LCD display pins RS, E, D4, D5, D6, D7

2. Inside the void setup () function,
first display some intro message on LCD display. Learn more about interfacing
LCD with Arduino here.

lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("CIRCUIT DIGEST");
lcd.setCursor(0,1);
lcd.print("ArduinoInterrupt");
delay(3000);
lcd.clear();

3. Then in same void setup () function the input and
output pins must be specified. The pin D13 is connected to LED’s Anode, so this
pin must be defined as output.

pinMode(13,OUTPUT);

4. Now the main important part in the programing comes
that is attachInterrupt() function, it is also included inside
the void setup().

attachInterrupt(digitalPinToInterrupt(2),buttonPressed1,RISING);
attachInterrupt(digitalPinToInterrupt(3),buttonPressed2,RISING);

Here it is specified that pin 2 is for external interrupt,
and buttonPressed1 function is called when there is RISING (LOW to
HIGH) at D2 pin. And pin 3 is also for external interrupt and buttonPressed2
function is called when there is RISING at D3 pin.

5. Inside the void loop(), a number (i)
is incremented from zero and printed on LCD(16x2).

lcd.clear();
lcd.print("COUNTER:");
lcd.print(i);
++i;
delay(1000);

In same void loop(), digitalWrite() is used on
the pin D13 where LED’s anode is connected. Depending upon the value in variable
output LED will turn on or off

digitalWrite(13,output);

6. The most important part is creating a interrupt
handler function according to the name that is used in the
attachInterrupt() function.

As two interrupt pins are used 2 and 3 so two ISR are
required. Here in this programming following ISR are used

buttonPressed1():

void buttonPressed1()
{
output = LOW;
lcd.setCursor(0,1);
lcd.print("Interrupt 1");
}

This function executes when push button on the pin D2 is
pressed (RISING EDGE). This function changes the state of the output to LOW
causing LED to turn OFF and prints the “interrupt1” on the LCD display.

buttonPressed2():

void buttonPressed2()
{
output = HIGH;
lcd.setCursor(0,1);
lcd.print("Interrupt2");
}

This function executes when push button on the pin D3 is
pressed. This function changes the state of the output to HIGH causing LED to
turn ON and prints the “interrupt2” on the LCD display.

Arduino Interrupt Demonstration

1. When PUSH BUTTON on the leftside is pressed the LED
goes ON and the LCD displays Interrupt2.

2. When the PUSH BUTTON on the right side is pressed the
LED goes OFF and the LCD displays Interrupt1

This is how an Interrupt can be useful to trigger any
important task in between of normal execution.

Code

//Interrupts using Arduino
//Circuit Digest

#include<LiquidCrystal.h>
// Including lcd display library

LiquidCrystal lcd (7,8,9,10,11,12);
// Define LCD display pins RS,E,D4,D5,D6,D7

volatile int output = LOW;

int i = 0;

void setup()

{
lcd.begin(16,2);
//  setting LCD as
16x2 type
lcd.setCursor(0,0);
lcd.print("CIRCUIT
DIGEST");

lcd.setCursor(0,1);
lcd.print("ArduinoInterrupt");

delay(3000);

lcd.clear();

pinMode(13,OUTPUT);

attachInterrupt(digitalPinToInterrupt(2),buttonPressed1,RISING);  //
function for creating external interrupts at pin2 on Rising (LOW to HIGH)

attachInterrupt(digitalPinToInterrupt(3),buttonPressed2,RISING);
//  function for creating external interrupts at pin3 on Rising (LOW to
HIGH)

}

void loop()

{

lcd.clear();

lcd.print("COUNTER:");

lcd.print(i);

++i;

delay(1000);

digitalWrite(13,output);     //Turns LED ON or OFF
depending upon output value
}

void buttonPressed1()           //ISR function
excutes when push button at pinD2 is pressed
{

output =
LOW;                //Change Output
value to LOW

lcd.setCursor(0,1);

lcd.print("Interrupt 1");
}

void buttonPressed2()           //ISR function
excutes when push button at pinD3 is pressed

{

output = HIGH;
//Change Output value to HIGH

lcd.setCursor(0,1);

lcd.print("Interrupt2");
}

Video

TAGS

arduino

Interrupts

tutorial

Arduino Nano

LED

button

16x2 LCD

Get Our
Weekly Newsletter!

Subscribe below to receive
most popular news, articles and DIY projects from Circuit Digest

Email Address *

Name

Country United States of
America Aaland Islands
Afghanistan Albania Algeria
American Samoa Andorra Angola
Anguilla Antarctica Antigua And Barbuda Argentina
Armenia Aruba
Australia Austria Azerbaijan Bahamas Bahrain
Bangladesh Barbados Belarus
Belgium Belize Benin Bermuda Bhutan
Bolivia Bonaire,
Saint Eustatius and Saba Bosnia and Herzegovina Botswana
Bouvet Island Brazil British Indian Ocean Territory
Brunei Darussalam Bulgaria
Burkina Faso Burundi Cambodia
Cameroon Canada Cape Verde
Cayman Islands Central
African Republic Chad Chile
China Christmas Island Cocos
(Keeling) Islands Colombia Comoros Congo Cook Islands Costa
Rica Cote D'Ivoire Croatia Cuba Curacao Cyprus
Czech Republic Democratic
Republic of the Congo Denmark
Djibouti Dominica Dominican Republic Ecuador Egypt El Salvador Equatorial Guinea Eritrea Estonia
Ethiopia Falkland Islands Faroe
Islands Fiji Finland France
French Guiana French
Polynesia French Southern
Territories Gabon Gambia
Georgia Germany Ghana Gibraltar Greece
Greenland Grenada Guadeloupe Guam
Guatemala Guernsey Guinea
Guinea-Bissau Guyana Haiti Heard and Mc Donald Islands
Honduras Hong
Kong Hungary Iceland India Indonesia Iran
Iraq Ireland
Isle of Man Israel Italy Jamaica Japan Jersey (Channel Islands) Jordan Kazakhstan
Kenya Kiribati Kuwait
Kyrgyzstan Lao
People's Democratic Republic Latvia
Lebanon Lesotho Liberia
Libya Liechtenstein Lithuania Luxembourg Macau
Macedonia Madagascar Malawi
Malaysia Maldives Mali Malta Marshall
Islands Martinique Mauritania
Mauritius Mayotte Mexico
Micronesia, Federated States
of Moldova, Republic of
Monaco Mongolia Montenegro Montserrat Morocco Mozambique Myanmar Namibia
Nauru Nepal
Netherlands Netherlands
Antilles New Caledonia New Zealand Nicaragua Niger
Nigeria Niue
Norfolk Island North
Korea Northern Mariana
Islands Norway Oman Pakistan Palau Palestine
Panama Papua
New Guinea Paraguay Peru
Philippines Pitcairn Poland
Portugal Puerto
Rico Qatar Republic
of Kosovo Reunion Romania
Russia Rwanda
Saint Kitts and Nevis Saint Lucia Saint
Martin Saint Vincent
and the Grenadines Samoa
(Independent) San Marino Sao Tome and Principe Saudi
Arabia Senegal Serbia
Seychelles Sierra Leone Singapore Sint
Maarten Slovakia Slovenia
Solomon Islands Somalia
South Africa South
Georgia and the South Sandwich Islands South Korea South
Sudan Spain Sri Lanka St.
Helena St. Pierre and
Miquelon Sudan Suriname
Svalbard and Jan Mayen
Islands Swaziland Sweden
Switzerland Syria Taiwan Tajikistan Tanzania Thailand
Timor-Leste Togo Tokelau Tonga Trinidad and
Tobago Tunisia Turkey
Turkmenistan Turks
& Caicos Islands Turks
and Caicos Islands Tuvalu Uganda Ukraine
United Arab Emirates United Kingdom Uruguay USA Minor Outlying Islands Uzbekistan Vanuatu Vatican City State (Holy See)
Venezuela Vietnam Virgin Islands (British) Virgin Islands (U.S.) Wallis
and Futuna Islands Western
Sahara Yemen Zambia Zimbabwe

Related Content

Gesture
Controlled Contactless Elevator using APDS9960 Sensor

Measuring
Turbidity of Water to Determine Water Quality using Arduino and Turbidity
Sensor

Arduino
Programmable Open-Source Flight Controller and Mini Drone using SMT32 and
MPU6050 for Drone Enthusiasts

DIY
Raspberry Pi LoRa HAT - LoRa Communication between Raspberry Pi and
Arduino

SPI
Communication with Nuvoton N76E003 MCU to Communicate with Arduino

Open-Source
Arduino Shield with a GPU, FPGA, HDMI, and Python Support for Hassle-free Game
Designing Experience

18650
Lithium Battery Capacity Tester using Arduino

Long
Distance Wired Serial Communication with Arduino using RS485 and CAT
Cables

Previous post
Interfacing
Servo Motor with AVR Microcontroller Atmega16

Next post

How
to Program AVR Microcontroller Atmega16 Using USBASP programmer and Atmel Studio
7.0

Comments

Jay

May 23, 2020

Log
in or register
to post comments

Good article, but you need to explain how to use software interrupts as
well.

Cheers.

Jay

May 23, 2020

Log
in or register
to post comments

"In ATmega168/328 based Arduino boards any pins or all the 20 signal pins
can be used as interrupt pins".

Could you please explain how?

Log
in or register
to post Comment

AEC-Q101
Automotive MOSFETs

AEC-Q101 automotive MOSFETs are capable of high-speed switching with low
on-resistance

BD9F800
DC/DC Step-Down Switching Regulator

The BD9F800 is a constant on-time controlled DC/DC converter with
high-speed response

BD71850MWV
System PMIC for i.MX 8M Nano

The BD71850MWV integrates all power rails required by i.MX 8M Nano
processors and system peripherals

Littelfuse
SP4010 TVS Diode Array

Littelfuse’s SP4010 TVS diode array offers ESD protection for 10 V
high-speed signal lines.

Toshiba
DF2B5M4ASL and DF2B6M4ASL ESD Protection Diodes

Toshiba’s DF2BxM4ASL ESD protection diodes offer minimal insertion loss for
high-speed designs.

Eaton
0603HV Series Fuses

Eaton 0603HV high-voltage, fast-acting fuses offer overcurrent protection
for sensitive electronics.

STMicroelectronics ESDL031-1BF4 ESD Protection

STMicroelectronics’ 3.3 V ultra-low clamping ESDL031-1BF4 protects data,
I/O, and power lines.

Würth
Single Pair Ethernet

Würth Single Pair Ethernet ensures thinner cable diameters, reduces cost,
and saves PCB
space.

News

Articles

Projects

Maxim
Integrated’s Automotive Buck-Boost Controller Enables Automotive USB Power
Delivery Ports with Industry’s Smallest Solution Size and Lowest Cost

STMicroelectronics
Pioneers Smaller and Faster Chargers and Power Supplies by Integrating Si Driver
and GaN Transistor in Single Package

Space
Saving LED Drivers in Compact DFN Package with Side-Wettable Flanks for
Automated Inspection and Improved Reliability

New
Family of WiFi-6 + BT/BLE Solutions from NXP Semiconductors for Increased
Capacity and Performance in Gaming, Audio, and IoT Connectivity Solutions

IQ
Vertiq 6806 – Compact Integrated Motor and Controller for High Performance
Commercial Grade Drone and Robotics Applications

Comparison
between CMOS and TTL Logic - Which is Better and Why?

Moin
SPM, Co-Founder and COO of Agnikul Shares How They are Building India’s First
Private Smallsat Rocket

All
About Transformer Protection and Transformer Protection Circuits

A
Glimpse into the Types of Drones Available Today Based on their Technology and
Application

Ajit
Thomas Co-Founder of Cavli Wireless on how to Break the Geographic Barriers in
IoT Connectivity Applications

Build
Your Own Capacitive Touch Sensor to Control Home Appliance using ESP32

IoT
Based Contactless Body Temperature Monitoring using Raspberry Pi with Camera and
Email Alert

Gesture
Controlled Contactless Elevator using APDS9960 Sensor

Measuring
Turbidity of Water to Determine Water Quality using Arduino and Turbidity
Sensor

DIY
Raspberry Pi LoRa HAT - LoRa Communication between Raspberry Pi and Arduino

Connect with us on social media and stay updated with latest news, articles
and projects!

Categories

Embedded Electronics

Power
Electronics

Analog
Electronics

Internet of
Things

Audio
Electronics

Electric
Vehicles

Events

Popular

Robotics

555
Circuits

Arduino
Projects

Raspberry
Pi Projects

Electronics News

Electronics Forum

Calculators

Newsletter

Sign Up for Latest News

E-mail *

Copyright © 2020 Circuit
Digest. All rights reserved.

Privacy
Policy |Cookie
Policy |Terms of
Use |Contact Us |Advertise

News

New
Products

Industry
News

Articles

Product Reviews

Tech
Articles

Tutorials

Interviews

Case
Studies

Digi-Key Store

Cables
and Wires

LED/Optoelectronics

Connectors
and Interconnect

Discrete

Electromechanical

Integrated
Circuits (ICs)

Embedded
Computers

Enclosures,
Hardware and Office

Isolators

RF
and Wireless

Power
and Circuit Protection

Programmers

Passives

Sensors
and Transducers

Semiconductors

Test
Products

Tools

Projects

555
Timer Circuits

Op-amp
Circuits

Audio
Circuits

Power
Supply Circuits

All
Electronic Circuits

Arduino Projects

Raspberry
Pi Projects

MSP430
Projects

STM32
Projects

ESP8266 Projects

PIC
Projects

AVR
Projects

8051
Projects

ESP32
Projects

NodeMCU
Projects

IoT
Projects

PCB
Projects

Arduino
ESP8266 Projects

All
Microcontroller Projects

Forum

Contact

Editorial

Advertise

About
Us

More

Partner
Events and Conferences

EE
Calculators

<none>

Login

Register