Wednesday, May 16, 2012

Simple Labs' Quick Start Kit for Arduino - Push Button / Switch Interfacing - How To?

The Push Button
Push Buttons like LEDs form a common part of most electronics devices. Push buttons can be used to get user feedback. (image a common scenario of scrolling through a menu and selecting an option - there are buttons all around the process!)

Working of a Push Button

So Far we have not used the SUPPLY voltage from the Arduino however, for connecting the button, we would need this. So, before connecting the Button, we need to connect our '+'ve terminal to the 5Volts SUPPLY line on the Arduino. Connect as shown in the image below.


There are 2 ways to connect a Push Button to the arduino - the Pull-Down Configuration & the Pull-Up Configuration. We will take a look at both these.

The Pull-Down Cnfiguration
In this configuration the Push Button is set-up such that it keeps giving a constant LOW signal(0) when not being pressed and gives a HIGH signal(1) when being pressed. To ensure that it keeps giving a LOW signal at all times, the Input line is also connect to the '-'ve terminal through a resistor. So in when the button is not being pressed, the input line stays connected to the GROUND. When the button is being pressed (and as the path of low resistance is always preferred) the input line gets connected to the SUPPLY line. Look at the following image to see how to connect a button in this configuration. We will keep this connection only as a reference as we will be using the Pull-Up configuration for our circuits.

The Pull-Down Configuration
The Pull-UP Cnfiguration
In this configuration the Push Button is set-up such that it keeps giving a constant HIGH signal(1) when not being pressed and gives a LOW signal(0) when being pressed. To ensure that it keeps giving a HIGH signal at all times, the Input line is also connect to the '+'ve terminal through a resistor. So in when the button is not being pressed, the input line stays connected to the SUPPLY . When the button is being pressed (and as the path of low resistance is always preferred) the input line gets connected to the GROUND line. Look at the following image to see how to connect a button in this configuration. This is the most preferred method to connect a button.
The Pull-Up Configuration
Now try the following code, Pressing the Button increases the intensity of one colour at a time, once the maximum intensity of the colour is reached that colour is set to 0 and the intensity of the next colour starts increasing [ RGB_Button.ino]

/*
  RGB_Button
  Pressing the Button increases the intensity of one colour at a time, once the maximum intensity of the colour is reached
  that colour is set to 0 and the intensity of the next colour starts increasing
 */

int intensity = 0, pin = 9;

void setup() {      
pinMode(2,INPUT); // This is the pin to which we have connected the button
}

void loop() {
  if(digitalRead(2)==0) // check if the button is being pressed
  {
    Serial.println("here");
    if(intensity < 255)
    {
    intensity++;
    }
    else
    {
      intensity=0;
      analogWrite(pin,intensity);
      if(pin<11)
        pin++;
      else
        pin=9;
    }
    analogWrite(pin,intensity);
  }

}

Here's the Output


No comments:

Post a Comment