Arduino- Reading a Momentary push button and changing state

There are various methods of reading a push button with variations in both hardware as well as software level. We will consider the simplest possible method and discuss the same in this post. Arduino Uno board is taken as reference.

Program

/*
This program uses a momentary push button connected at digital pin number 3 on arduino and internal LED connected on digital pin 13.

When the push button is pressed the LED will toggle its state. Software de-bouncing technique is used for stable input detection (see the explanation).

Code by Syam
*/

const int PushButton = 3;    // the pin that the push button is attached to
const int LedPin = 13;    // the pin that the LED is attached to (already internally wired in Arduino)

boolean buttonState = 0;    // current state of the button
boolean lastButtonState = 0;  // previous state of the button

void setup()
{
pinMode(PushButton, INPUT);                   // initialize the PushButton as a input:
digitalWrite(PushButton, HIGH);                 // enable internal pull up
pinMode(LedPin, OUTPUT);                       // initialize the LED as an output:
}
void loop()
{
buttonState= digitalRead(PushButton);
if (buttonState == 0)                                      // check if key is pressed
{
lastButtonState = !lastButtonState;             // Change the last state by logic inversion
digitalWrite(LedPin, lastButtonState);        // Reflect the change in the output LED
while (buttonState == 0)                             // Debounce logic for key release
{buttonState= digitalRead(PushButton);}
}
}

Program Explanation

This program aims at turning on/off an led using a momentary push button switch. We will use the arduino pin 13 to output, since there is an internally connected LED at pin number 13 of the arduino. Hence for this program you may need only a push button or the micro switch for the interfacing. However if you require to connect the led manually you can use the signal coming out of the pin 13. If connecting the same is done make sure you connect a resistance, say 220Ω, in series with the led, to act as a current limiter. If this resistance is not used the led may burn out.

The program essentially contains two parts, as with all arduino sketches; the setup() and the loop(). Inside the setup() we declare the input and output pins. Here the input pin is PushButton which in turn is a declared constant representing pin number 3, and the LedPin is the output pin which represents pin number 13 on the arduino board. The input pin or the PushButton is internally pulled up using the digitalWrite(). The advantage in using this technique is that, since the pin is internally pulled up, there is no need to provide an external pull up resistor for having clarity in logic. If neither the internal pull up nor the external pull up resistor is provided the logic level during no key/button press will be undetermined, as the state there is high impedance, and in such cases the controller may misbehave. Hence we provide a pull up (here its internal pull up that is being used), so that we may obtain clarity in logic levels; in this program; when no button is pressed the logic state will be high and when the button is pressed the logic state changes to low. In the loop part we write the statements that needs continuous looping.

There are two global variables declared which perform the core operation required; there are buttonState and lastButtonState. The buttonState variable is used to store the input value, i.e. logic coming at the buttonPin, and the lastButtonState is used to toggle the output by comparing the last logic state stored in itself. Hence if the logic state detected at the input side, (which is read using digitalRead()), the lastButtonState is inverted and saved into self. Now using the digitalWrite(), we output the logic state to the LedPin, hence turning on/off the led. After this, another important step is to include the debounce logic. The debounce logic is important so as to avoid multiple instances or button press being read during a single press, due to spikes and high execution speed of the core (multiple nested loops may be required to arrest the spikes in this program as it is very short, and also it depends on the quality of the momentary push button being used).

Connection Diagram

It should be noted that in the diagram the LED represents the internal LED and hence the current limiting resistor is not shown. However if you are connecting an external LED to pin 13, make sure that you connect a resistor in series with the LED, as discussed earlier.

push_button_read

Published by

Syam Nair

Technocrat, Traveler, & Researcher

One thought on “Arduino- Reading a Momentary push button and changing state”

Leave a comment