Arduino Interrupts Button Tutorial Example – Pin 2 Hardware

///Arduino Interrupts Button Tutorial Example – Pin 2 Hardware

Arduino Interrupts Button Tutorial Example – Pin 2 Hardware

Arduino Interrupts are powerful. In this tutorial, I will be showcasing an example of how to tie Pin 2 of the Arduino to a hardware interrupt with the use of a button. This simple guide illustrates the functions of an interrupt, how it triggers different software states as well as the configurations you need to make.

We will also take a deeper dive into the intricacies of interrupts, where they may be needed and how the configuration works in the software.

Arduino Interrupts – Hardware Used

What are Interrupts and How they Work?

An interrupt is essentially a trigger which may occur in software or hardware. Upon an occurrence of an interrupt, the software will cease all operations and transfer the execution to the function specified by the interrupt declaration. In other words, just as the name suggests, the main flow of the program is interrupted and redirected toward a “mission critical” execution.

Most microcontrollers, Programmable Logic Controllers (PLCs), Processors and related integrated circuits have hardware interrupts. The Arduino platform is no exception. Depending on the board you own, you may have access to more or less of these pins, but they are there. Here’s a diagram from the official site which outlines the available pins for every Arduino board.

Arduino Interrupts Pins Tutorial Button

Software interrupts may also be defined in order to trigger the associated function, but they will be conditional on internal triggers rather than hardware tied to the board.

Why and How Should You use Interrupts?

If you need to execute immediate pieces of code which may not wait for the program cycle, you should consider interrupts. They are an excellent way to divert your program into a small piece of code and returning to the main function afterward.

In order to call an interrupt, you must tie the right signal to one of the pins allocated for this function on your board. For the Arduino Uno R3, these pins are digital inputs 2 and 3.

Next, you must create a function which will be called by the interrupt. That being said, this function has special constraints which do not apply to regular functions:

  • The function may not return anything (it must be declared as “void”)
    • A workaround (as specified by the Arduino datasheet) is to use “volatile variables”
  • The function may not take on any parameters
  • The function may not make use of any delay() instructions as those are reserved during the execution of the interrupt.

Next, you need to create an interrupt initializer which specifies the pin used for the interrupt, the function it will be calling as well as the mode.

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

  • Specified Pin – the hardware input which is tied into the interrupt.
  • Function – the name of the function called by the interrupt
  • Mode – One of the modes allowed by the interrupt (explained lower)
    • 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.

Interrupt Modes

You may select from one of the four modes on the Arduino as listed above. Each one of these modes will call the interrupt function based on a different condition. The condition will depend on your application.

Arduino Hardware Interrupts Low and Change Mode

Arduino Hardware Interrupts Rising Falling Mode

Interrupts Basic Example – Button on Pin 2

Here’s the full program used to create the interrupt function for the button attached to Pin 2 of the Arduino Uno R3:


void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, INPUT);
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), InterruptFunction, LOW);
}

void loop() {
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(9, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(8, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(10, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
}

void InterruptFunction () {
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
}


The interrupt specified within the “setup()” function is applied to pin 2, calls the InterruptFunction() and is set to the LOW mode.

In case you are wondering as to why we’re using “LOW” here; I wanted the LED to stay OFF as long as I hold down the button which continuously writes a logic low to the three LED colors.

Issues and Drawbacks of Interrupts

Interrupts may seem like they should be used all over the place. They are a very convenient “patch” as soon as your program evolves into an uncontrollable monstrosity. However, there are many drawbacks and watch-outs when it comes to using this feature. Here’s an outline of some of them:

  • Interrupts will completely suspend the main block of code. Until they complete their execution, no other piece of code will be allowed to run. It is therefore recommended to keep interrupt based functions short and to the point.
  • Interrupts may induce variations within your code. An example of this would be a peripheral device being turned on right before an interrupt is called. Although the program returns where it left off, a longer pulse has been created.
  • Interrupts don’t allow other interrupts to be called. Therefore, if you have too many “mission critical” instances at any given time, you may run into problems.

Video Tutorial

Here’s the full video tutorial of Arduino Interrupts!

Conclusion on Interrupts

Regardless of the hardware platform, you’re working with, interrupts are an important tool for the right application. They allow one to conveniently execute a small piece of code in the fastest manner possible. The interrupt will be based on a defined software or hardware trigger which will immediately transfer the execution of the program to a pre-defined function. This function will execute and release control back where the interrupt was initiated.

Interrupts may seem like the ideal solution for every single input. However, that’s not the case as they do have some drawbacks. The functions may not pass or return any parameters (in Arduino & some other platforms), the execution time must be minimized and you only get a few pins (perhaps less on a different platform) per controller. Furthermore, you can easily get your interrupts to fight each other as one can’t execute while the other one is being processed.

The bottom line is that it is a tool which requires a specific application!

Take care,
– EEEnthusiast

By | 2018-06-22T19:19:49+00:00 June 23rd, 2018|Arduino, Tutorials|0 Comments

About the Author:

Welcome to EEEnthusiast.com My name is Vlad, I'm an Electrical Engineer and I love to build and teach. I graduated in 2013 and have tried my best to share my electrical knowledge and experience with viewers and readers from all over the world. You can find me on my personal site or LinkedIn!

Leave A Comment

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Show Buttons
Hide Buttons