CIRC
07
.:Button Pressing:.
.:Pushbuttons:.
(ARDX) .:Arduino Expermentation Kit:. (ARDX)

What We're Doing
Up to this point we have focused entirely on outputs, time to get our Arduino to listen, watch and feel. We'll start with a simple pushbutton. Wiring up the pushbutton is simple. There is one component, the pull up resistor, that might seem out of place. This is included because an Arduino doesn't sense the same way we do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or LOW. The button is set up to pull the Arduino's pin LOW when it is pressed, however, when the button is unpressed the voltage of the pin will float (causing occasional errors). To get the Arduino to reliably read the pin as HIGH when the button is unpressed, we add the pull up resistor.
(note: the first example program uses only one of the two buttons)


The Circuit
The Parts
CIRC-07
Breadboard Sheet
x1
2 Pin Header
x4
Pushbutton
x2
Wire
10k Ohm Resistor
Brown-Black-Orange
x2
560 Ohm Resistor
Green-Blue-Brown
x1
Red LED
x1


Schematic

 

Resources
.:download:.
breadboard layout sheet
http://ardx.org/BBLS07

.:view:.
assembly video
http://ardx.org/VIDE07

 

Code (no need to type everything in just)
File > Examples > 2.Digital > Button
(example from the great arduino.cc site, check it out for other great ideas)

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 7. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave 
 modified 17 Jun 2009
 by Tom Igoe
 
  http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

 

Not Working? (3 things to try)
 

 
Light Not Turning On
The pushbutton is square and because of this it is easy to put it in the wrong way. Give it a 90 degree twist and see if it starts working.
 
Light Not Fading
A bit of a silly mistake we constantly made, when you switch from simple on off to fading remember to move the LED wire from pin 13 to pin 9.
 
Underwhelmed?
No worries these circuits are all super stripped down to make playing with the components easy, but once you throw them together the sky is the limit.
 
 

Making it Better?
 
On button off button:
The initial example may be a little underwhelming (ie. I don't really need an Arduino to do this), lets make it a little more complicated. One button will turn the LED on the other will turn the LED off. Change the code to.
int ledPin = 13; // choose the pin for the LED
int inputPin1 = 3; // button 1
int inputPin2 = 2; // button 2
 
void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(inputPin1, INPUT); // make button 1 an input
  pinMode(inputPin2, INPUT); // make button 2 an input
}
 
void loop(){
  if (digitalRead(inputPin1) == LOW) {
    digitalWrite(ledPin, LOW); // turn LED OFF
  } else if (digitalRead(inputPin2) == LOW) {
    digitalWrite(ledPin, HIGH); // turn LED ON
  }
}
 
Upload the program to your board, and start toggling the LED on and off.
 
Fading up and down:
Lets use the buttons to control an analog signal. To do this you will need to change the wire connecting the LED from pin 13 to pin 9, also change this in code.
     int ledPin = 13; ----> int ledPin = 9;
Next change the loop() code to read.
 
int value = 0;
void loop(){
  if (digitalRead(inputPin1) == LOW) { value--; }
  else if (digitalRead(inputPin2) == LOW) { value++; }
  value = constrain(value, 0, 255);
  analogWrite(ledPin, value);
  delay(10);
}

 
Changing Fade Speed:
If you would like the LED to fade faster or slower, there is only one line of code that needs changing;
   delay(10); ----> delay(new #);
To fade faster make the number smaller, slower requires a larger number.