Using a Momentary Push Button Switch for Interrupt in Arduino

On a previous blog post we have discussed a simpler push button interfacing method for toggling an LED. In this post lets get down to much more important concept of using interrupts for processing. Although Interrupts and switches are not normally used as priority is not so high in every button press situation, it’s importance arises in critical inputs, such as an emergency shutdown button.

An example Scenario will be to safely turn off the system in case of emergency, in which if the power is suddenly cut without proper shutdown will cause a catastrophic event (a nuclear reactor for example), we call such inputs as high priority hard real time inputs. Any how I’m not going to turn off a nuclear reactor here, let’s stick to our good old LED on/off here. After all we discuss just the basics here. What you implement is up to you. I have taken Arduino Uno board as reference.

Before we can jump into the program, if you can recall, in the earlier post, we have utilized a simple software debouncing technique to avoid the jitter. However this method utilizes more code memory and leaves the code much more inefficient which however could be excused as we had lot of unused program memory and it did not involve any time critical process.

So before we get on to the coding section let us analyze the hardware section and what we can do there for debouncing. Of the many methods here I will be describing the best method (although bulky circuit and rarely used), of latch based debouncing. The latch based circuitry employs a SPDT momentary push button and a SR latch. For more on hardware debouncing you can refer a very nice article by Jack G. Ganssle (Click here for the article)

The Latch – Switch arrangement (Circuit)

Hardware-Switch-Debouncing

The circuit uses two cross coupled NAND gates which form an S-R latch, A SPDT (Single Pole Double Throw) switch, two pull up resistors. The resistor generates a logic ‘one’ for the gates, Switch pulls one of the inputs to ground.

If  the switch is in position as shown in figure  the output of the upper gate is ‘1’ irrespective of the input of the other gate and the one created by the bottom pull up resistor which drives the lower NAND gate to zero which in return races back to the other gate. If the switch moves back and forth between the contacts and is suspended for a while in neither region between the terminals,the latch maintains its state because ‘0’ from the bottom NAND gate is fed back. The switch may move between the contacts but the latch’s output ensures it never bangs back and thus switch is bounce free.

Coding side essentials

Now that the hardware section is complete lets get back to the coding side. As evident from the hardware side we feed the output of the latch “Q” (as shown in the figure) as the input to the arduino and Lets assume position “B” is the pressed state position. and hence when the switch is pressed we get a logic high at Q and when released we observe a logic low. So each time an interrupt is sensed we are going to toggle the LED on and off.

Setting that in mind let us now analyze the interrupt concept in arduino and all that is associated with it.

What is an Interrupt? Interrupt pins in Arduino Uno and types of trigger methods

Interrupt refers to a high priority signal in a microcontroller. Interrupts can be internal as well as external. Interrupts such as power on reset or brown out reset are internally generated interrupts (we will not be discussing it here), and external interrupts allows us to perform high priority tasks as and when we receive interrupt signals from the outside world.

In Arduino two external interrupts are available for use INT0 and INT1, in descending order of signal priority. The INT0 is mapped to digital pin 2 on arduino and INT1 is mapped to the digital pin 3. Both these interrupts can be triggered during any of the five different different signal scenario occurring at the external interrupt pins;
1) HIGH – When the pin is logic high
2)LOW – When the pin is logic low
3)CHANGE – When the logic of the pin changes; from low to high or high to low
4)RISING – When the logic changes from Low to High
5)FALLING – When the logic changes High to Low

Any interrupt needs an interrupt service routine. Simply speaking a function that needs to be executed when an interrupt is sensed. It should be noted that the ISR (Interrupt Service Routine), should be as short and simple as possible, any and all arduino functions that depends on timers cannot be called or used inside the ISR and if used the program may misbehave, or hang up the entire system.

To enable and interrupt and attach the interrupt in arduino we use the attachInterrupt() function.

Syntax:

attachInterrupt(interrupt, ISR, mode)

interrupt – defines the interrupt i.e. 0 for INT0 and 1 for INT 1
ISR – define the interrupt service routine or function to be called during the event
mode – defines the signal scenario as discussed earlier (RISING, FALLING, etc)

Program

Let us now write the program for the same

boolean toggle_flag = 1;
void setup()
{
pinMode(13,OUTPUT);
attachInterrupt(0, toggle, RISING);
}

void loop()
{digitalWrite(13,toggle_flag);}

void toggle()
{toggle_flag = !toggle_flag;}

So here we have attached the interrupt to INT0 i.e. digital pin 2 on arduino, so that is where our input i.e. “Q”, output of the latch segment needs to to hooked. Every time the button is pressed the ISR is run, only once as we have specified the ‘RISING’ mode and also because we have suppressed the button press jitter using the latch debounce technique, toggling the LED during every button press.

Happy coding 🙂 …..

Published by

Syam Nair

Technocrat, Traveler, & Researcher

Leave a comment