Playing with push buttons

Push buttons and Arduino

Push buttons and Arduino

The first sketch I tried with my Arduino Uno was the button sketch. The code to turn on and off a LED is quite simple to understand even for someone like me that has no programming experience. There are some tutorials on lady Ada’s website, they are very good for people that have no knowledge of electronics and programming.

So now it was time to write some sketches myself or to be more precise to change some existing sketches and let them do what I want.
Next thing I wanted to try was using a push button to act as a switch. So when you press the button one time the output stays high. When you press it again the output goes low. Pretty easy I thought, until I tried to write the sketch. I took me quite some time to figure out I had to put the state of the led and the pushbutton in to variables.
Eventually I came up with the following code:

/* 
  http://www.bajdi.com
  Push button as switch
  
  The circuit:
 * Using internal led on pin 13
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
*/

//Constants
const int button2Pin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables
int ledState = LOW;         // the current state of the output pin
int lastButtonState = LOW;   // the previous reading from the input pin

void setup() {
  pinMode(button2Pin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(button2Pin);
  
    if (lastButtonState == LOW && reading == HIGH) 
    {
    if (ledState == HIGH) {ledState = LOW;} 
    else {ledState = HIGH;}
    digitalWrite(ledPin, ledState);}
    lastButtonState = reading;
  
  }

The sketch worked, but not exactly like it should. When you release the push button the microcontroller might receive some noise thinking that you have pressed the button again. This made it quite hard to actually turn of the led. So I needed to program something that can filter out the noise, this is called “debouncing”. Luckily you don’t need to look very far, in the Arduino ide there is an example sketch debounce. So I tried that, but the sketch would not keep the led on after releasing the push button. So google to the rescue and I found this, seems like there is bug in the sketch. Luckily the correct pde file is provided, you can download it here.
The code to debounce the push button is quite complex. But there is an easier solution, someone has made a bounce library that makes debouncing digital inputs a bit easier. You can find it here. So I took my original sketch and added a second push button and used the bounce library to debounce both buttons.
Here is the code:

/* 
  http://www.bajdi.com
  2 push buttons as switches with debouncing (using the bounce library)
  
  The circuit:
 * using the internal led on pin 13
 * pushbutton attached from pin 2 to +5V
 * pushbutton attached from pin 3 to +5V
 * 2 10K resistors attached from pin 2 and 3 to ground
*/

#include <Bounce.h>

const int button2Pin = 2;    // the number of the first pushbutton pin
const int button3Pin = 3;    // the number of the first pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;           // the current state of the output pin
int lastButton2State = LOW;   // the previous reading from the first push button pin
int lastButton3State = LOW;   // the previous reading from the second push button pin

Bounce bouncer1 = Bounce(button2Pin,50);   //setting up the debouncing for the first push button
Bounce bouncer2 = Bounce(button3Pin,50);   //setting up the debouncing for the second push button

void setup() {
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  
  bouncer1.update ( );    // Update the debouncer for the first push button
  bouncer2.update ( );    // Update the debouncer for the second push button
  
 int value1 = bouncer1.read();     // Get the update value      
 int value2 = bouncer2.read();     // Get the update value
 
  int reading1 = value1 == HIGH;    
  int reading2 = value2 == HIGH;
  
    if (lastButton2State == LOW && reading1 == HIGH) 
    {
    if (ledState == HIGH) {ledState = LOW;} 
    else {ledState = HIGH;}
    digitalWrite(ledPin, ledState);}
    lastButton2State = reading1;

    if (lastButton3State == LOW && reading2 == HIGH) 
    {
    if (ledState == HIGH) {ledState = LOW;} 
    else {ledState = HIGH;}
    digitalWrite(ledPin, ledState);}
    lastButton3State = reading2;
  }

Another thing I tried was to make a start/stop sketch. This is useful to start and stop or turn on or off a process. I again made use of the bounce library. I first made the sketch without it but then the push buttons don’t work as they should.

/* 
  http://www.bajdi.com
  2 push buttons, one turns on the led the other turns of the led.
  You can use this as a start / stop or on / off sketch
  The push buttons are debounced using the bounce library
  
  The circuit:
 * using the internal led on pin 13
 * pushbutton on/start attached from pin 2 to +5V
 * pushbutton off/stop attached from pin 3 to +5V
 * 10K resistor attached from pin 2 to ground
*/

#include <Bounce.h>

const int on = 2;     //on push button on pin 2
const int off = 3;    //off push button on pin 3
const int led =  13;  //led on pin 13

Bounce bouncer1 = Bounce(on,50);   //setting up the debouncing for the on push button
Bounce bouncer2 = Bounce(off,50);   //setting up the debouncing for the off push button

void setup() {
  pinMode(on, INPUT);
  pinMode(off, INPUT);
  pinMode(led, OUTPUT);
}
void loop() {
 
 bouncer1.update ( );    // Update the debouncer for the on push button
 bouncer2.update ( );    // Update the debouncer for the off push button
 
 int value1 = bouncer1.read();     // Get the update value      
 int value2 = bouncer2.read();     // Get the update value
 
 int reading1 = value1 == HIGH;    
 int reading2 = value2 == HIGH;
  
    if(digitalRead(on) && reading1 == HIGH ){
    digitalWrite(led, HIGH);
    }

    if(digitalRead(off) && reading2 == HIGH){
    digitalWrite(led, LOW);
    }
 
}

Now something simple, lets say you have 2 push buttons or digital sensors. And both must be activated to set an output high. By using the boolean && this is very easy. To see all the operators, control structures,… have a look at the Arduino language reference page.

/* 
  http://www.bajdi.com
  2 push buttons, both must be pressed to turn on the led
  
  The circuit:
 * using the internal led on pin 13
 * pushbutton attached from pin 2 to +5V
 * pushbutton attached from pin 3 to +5V
 * 2 10K resistors attached from pin 2 and 3 to ground
*/

const int button1 = 2;     // the number of the button 1 pin
const int button2 = 3;     // the number of the button 2 pin
const int led =  13;      // the number of the LED pin

// variables will change:
int button1State = 0;         // variable for reading the button 1 status
int button2State = 0;         // variable for reading the button 2 status

void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);      
  // initialize the button 1 pin as an input:
  pinMode(button1, INPUT); 
  // initialize the button 2 pin as an input:
  pinMode(button2, INPUT);      
}

void loop(){
  // read the state of the button 1 and 2 value:
  button1State = digitalRead(button1);
  button2State = digitalRead(button2);

  // check if both inputs are high.
  if (button1State == HIGH && button2State == HIGH) {    
    // turn LED on:    
    digitalWrite(led, HIGH);  
  }
  else {
    // turn LED off:
    digitalWrite(led, LOW);
  }
}

That’s all for push button sketches. Led 13 on my Arduino Uno has blinked enough 🙂

BTW I bought the little push buttons here, only 1$ for 5 push buttons. They are small but the pins are perfectly spaced to put them on a breadboard. You do need to use some force to make them stick to the breadboard. You would think that they are made by some unknown Chinese brand but they are made by Omron, a well known Japanese brand of electric components.

2 responses to “Playing with push buttons”

  1. yinyang

    Sir, do you have codes for buttons first pressed will lock out the other buttons? please reply sir..thanks ..nice work!

Leave a Reply


*