Tuesday 3 May 2016

Arduino Tutorial # 6 - Push Button , Debounce (Digital inputs)

Introduction.



Today we will discuss about digital inputs. For this we are going to use a push button to light a LED. This is a very simple setup we do not need a Arduino to do this, but we will use it to learn about digital inputs and some other important things.


Push Button



The pushbutton is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button.







Parts you will need


1X Breadboard
1X LED
1X 1K Resister
1X 330Ω Resister
1X Push button
Jumper wires

Connections


  • Connect the led positive to 330Ω resister and other side of the resistor to Arduino pin 13. LED negative should connect to ground.
  • Connect one pin of the pushbutton to VCC (5v) and other to ground though 1K resister.
  • Connect Arduino pin 8 to the same pin of the pushbutton that we connect 1K resister.



Pull-down resister


When the pushbutton is not activated it's value is pull down to ground.This is because the input is "floating", that means when it is not connected to either VCC (5V) or to ground (0V) it will more or less randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.

Sketch 1 (switch 1)

Here we will make the LED turn on when we press the push button and OFF when we release it. 

First define two variables


int ledPin = 13; // choose the pin for the LED
int inPin = 8;   // choose the input pin (for a pushbutton)



Then declare LED as a output and here we are going to declare pushbuttun as a digital input. This is the first time that we are going to set a input in setup function.


 void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}


Then we have to check whether the pusebuttuon is pressed or not. For this we are using if/else statement. we want the LED to be ON when we press the pushbutton.

void loop()
{
 if (digitalRead(inPin) == HIGH)        // check if the input is HIGH (button pressed)
    digitalWrite(ledPin, HIGH);  // turn LED ON
    else {
    digitalWrite(ledPin, LOW);  // turn LED OFF
  }
}
Here the if statement will check the pushbutton is pressed or not and if it is pressed then LED will turn ON and when push button released LED will turn OFF.



Sketch 2 (switch 2)


This time when button press the LED ON and it will stay ON when we release the button. And when we press again the LED will turn OFF and stay OFF even we release the button.

We have make two new Boolean variables. Boolean can hold one of two values - True or false, High or Low, 1 or 0, On or Off. 


int ledPin = 13; // choose the pin for the LED
int inPin = 8;   // choose the input pin (for a pushbutton)
boolean lastButton = LOW; //tracking previous button
boolean ledOn=false; //keep a track on current state of LED


hear lastButton variable will track the previous action of the pushbutton (initialized as LOW) and the current LED ON state true/false (initialized to false).

Setup function will remain the same as before.


 void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}


In loop function first thing we have check if the button is currently high and previously low that indicates that the button was pressed.

  if (digitalRead(inPin) == HIGH && lastButton == LOW) 

Since the button is pressed now we need to turn ON the LED for that will take the ledOn value and invert that value. Remember we set the ledON as false in beginning so this time it will turn ON. "!" will invert or flip the value.

   ledOn = !ledOn;

And we need to update lastButton variable to HIGH.

  lastButton = HIGH;

In else statement we have set the lastButton to pusubutton input value

   lastButton = digitalRead(inPin);

Finally we have write digitalWirte to ledPin and this time we are not going to set always HIGH we will to the value of ledOn.

  digitalWrite(ledPin, ledOn);

Our loop is completed



void loop()

{
  if (digitalRead(inPin) == HIGH && lastButton == LOW) 
//check the button is currently high and previously low
  {
    ledOn = !ledOn; //invert ledOn 
    lastButton = HIGH; // set it HIGH
  }
  else
  {
    lastButton = digitalRead(ledPin);
  }
  
  digitalWrite(ledPin, ledOn);

}


Debounce

Sometimes you may noticed that the LED will not turn ON and OFF as we programmed that is because the switch is bouncing. Mechanical switchers have tendency to generate multiple signals as contacts close or open. We have to change the code little bit in order to deal with it. We are calling it debounced.

Let's change our code to debounced the switch in software (we can do this in hardware also but for this tutorial we will talk how to do in software)


Sketch 3 (switch 3) -Debounce


We need to add one more Boolean variable called currenButtton to keep track what the current button value is and we will set it to LOW


int ledPin = 13; // choose the pin for the LED
int inPin = 8;   // choose the input pin (for a pushbutton)
boolean lastButton = LOW; //tracking previous button
boolean currentButton =LOW; ll track current button value
boolean ledOn=false; //keep a track on current state of LED


And we are going to create a new function it will return a boolean and we will call it as function debounce and we also feed in a Boolean value which in the function and refereed to as last( this will be the last sate of the switch). just like digitalRead(ledPin) is a function with input ledPin in it , "debounce" is now functioning with input "last"

boolean debounce(boolean last)


In our new function we are going to determine what the current value of the switch is.


 boolean current = digitalRead(ledPin);


here we are setting boolean current as the input that is reading from pushbutton. Then we are going to compare that to the last one. since we already pass the last value we can now compare that is it equal and that will tell us that someone has press the button.

if (last != current) ll if last not equal to current


and if the switch change it state first thing we are going to do is to delay by 5 milliseconds. This will give enough time to finish debounce but short enough that your finger not yet released from the switch.


delay(5);


then we will read it again, by reading again and we make sure that the debounce is finished and it will give a steady value.


current = digitalRead(inPin);



then we are returning that value from the function and that will give us our debounce value.

And then we have change our loop() as below


void loop()
{
// passing the lastButton value to currentButton
  currentButton = debounce(lastButton); 

  if (lastButton == LOW && currentButton == HIGH) // button pressed
  {
    ledOn = !ledOn;
  }
  lastButton = currentButton;
  
  digitalWrite(ledPin, ledOn);
}


Here is the full code (switch 3)

int ledPin = 13; // choose the pin for the LED
int inPin = 8;   // choose the input pin (for a pushbutton)
boolean lastButton = LOW; //tracking previous button
boolean currentButton =LOW; ll track current button value
boolean ledOn=false; //keep a track on current state of LED

 void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}


boolean debounce(boolean last)
{
boolean current = digitalRead(inPin);
if (last != current)
{
delay(5);
current = digitalRead(inPin);
}
return current;
}


void loop()
{
// passing the lastButton value to currentButton
  currentButton = debounce(lastButton); 

  if (lastButton == LOW && currentButton == HIGH) // button pressed
  {
    ledOn = !ledOn;
  }
  lastButton = currentButton;
  
  digitalWrite(ledPin, ledOn);
}


You can download sketch from here





No comments:

Post a Comment