Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to Use Interrupts With Arduino Board- (Part 25/49)

By Ajish Alfred July 9, 2013

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.


 

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;

Typical Arduino Pro-Mini Board

Fig. 2: Typical Arduino Pro-Mini Board

Arduino IDE Software Window

Fig. 3: Arduino IDE Software Window

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.

External USB To TTL Converter Board For Programming Arduino And Serial Communication

Fig. 4: External USB to TTL converter board for programming Arduino and serial communication

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

       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)  

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

      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

–   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;

  –  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.

Project Source Code

###




/*================================= EG LABS =======================================

Using an external interrupt to change the state of an LED

 The circuit:

 * LED attached from pin 6 to ground through a 1K resistor

 LCD:

 * LCD RS pin to digital pin 12

 * LCD Enable pin to digital pin 11

 * LCD D4 pin to digital pin 5

 * LCD D5 pin to digital pin 4

 * LCD D6 pin to digital pin 3

 * LCD D7 pin to digital pin 2

 * LCD R/W pin to ground

 * 10K resistor:

 * ends to +5V and ground

 * wiper to LCD pin 3

 * LED anode attached to digital output 6

 * LED cathode attached to ground through a 1K resistor

//================================= EG LABS =======================================*/

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);   // initialize the LCD library with the numbers of the interface pins

int led = 6;                                         // variable indicating the pin number where the LED is connected

int led_state = 0;                                   // variable indicating the state of the LED              

int count = 0;

void setup()

{             

  pinMode(led, OUTPUT);                              // initialize the digital pin as an output.  

  lcd.begin(16, 2);                                  // set up the LCD

  lcd.print("ENGINEERS GARAGE");            

  lcd.setCursor(0, 1);   

  lcd.print("  EXT INTERRUPT ");  

  delay(2000);

  lcd.clear();

  lcd.print("  EXT INTERRUPT ");    

  lcd.setCursor(0, 1);   

  lcd.print("     ENABLED    ");

  attachInterrupt(0, glow, RISING);                 // enable the external interrupt 0, with function 'glow' as ISR and interrupt occurs on rising edge

}

void loop()

{

  digitalWrite(led, led_state);                      // turn the LED on or off depending on the value of the 'led_state'

  if(count > 10)

  {

    detachInterrupt(0);

    lcd.setCursor(0, 1);   

    lcd.print("    DISABLED    ");

  }else;

}

//========== ISR ============//

void glow()

{

  count ++;

  led_state = !led_state;                             // invert the value of the led_state

}

//========== ISR ============//

###

 


Circuit Diagrams

Circuit-Diagram-Using-Interrupts-Arduino-Board

Project Components

  • Arduino Pro Mini
  • LCD
  • LED
  • Resistor

Project Video


Filed Under: Arduino Projects
Tagged With: Arduino, interrupts
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Phase Shift Full Bridge suffers spurious FET turn_ON
  • optimum spacing between feed and sub reflector
  • Equipment to see if household should buy battery/solar/inverter?
  • 'HERIC' pairs of IGBTs essential for Mains inverters
  • reverse polarity circuit protection between to power sources

RSS Electro-Tech-Online.com Discussions

  • Adhesive Defibrillator Pad Cable
  • Epson crystal oscillators
  • Simple LED Analog Clock Idea
  • Fun with AI and swordfish basic
  • Microinverters and storeage batteries?

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How to design a heart-rate pulse sensor BPM meter
  • Three-phase EMI filter with neutral provides >1 MOhm insulation resistance
  • Infineon adds 60 V and 150 V power MOSFETs with 30 krad(Si) TID and AEC-Q101 qualification
  • Microchip releases DSC families with 78 ps PWM resolution and 40 Msps ADCs
  • Silanna Semiconductor releases laser driver IC with 86% efficiency

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe