3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino Tutorial Bundle .:Arduino Experimentation Kit:. (ARDX)

Step 9.:Button Pressing:. (Pushbuttons) - CIRC07

.:Button Pressing:. (Pushbuttons) - CIRC07

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)

(you can also download the breadboard layout sheet from the bottom of this step)


The Parts:
  • CIRC-07 Breadboard Sheet
  • 2 Pin Header (x4)
  • Pushbutton (x2)
  • 10k ohm Resistor (brown-black-red) (x2)
  • 560 ohm Resistor (green-blue-brown) (x2)
  • 5mm Red LED (x1)


The Circuit and Plugging Everything In:
A Small Video of Everything Being Plugged in


The Code: - File > Sketchbook > Examples > Digital > Button
/* * Button * by DojoDave <http://www.0j0.org> * * Turns on and off a light emitting diode(LED) connected to digital   * pin 13, when pressing a pushbutton attached to pin 7.  * * http://www.arduino.cc/en/Tutorial/Button */ int ledPin = 13;                // choose the pin for the LEDint inputPin = 2;               // choose the input pin (for a pushbutton)int val = 0;                    // variable for reading the pin statusvoid setup() {  pinMode(ledPin, OUTPUT);      // declare LED as output  pinMode(inputPin, INPUT);     // declare pushbutton as input}void loop(){  val = digitalRead(inputPin);  // read input value  if (val == HIGH) {            // check if the input is HIGH    digitalWrite(ledPin, LOW);  // turn LED OFF  } else {    digitalWrite(ledPin, HIGH); // turn LED ON  }}



Not Working?
  • 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 LEDint inputPin1 = 3;      // button 1int inputPin2 = 2;      // button 2void 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.


CIRC07-sheet.pdf(612x792) 30 KB
CIRC07-sheet.pdf(630x810) 45 KB
« Previous StepDownload PDFView All StepsNext Step »
1 comment
Apr 13, 2010. 1:43 AMSchwann says:
 Hey, just thought I'd let you know that the example from arduino.cc uses a pull-down configuration while your circuit uses a pull-up configuration, so the LED on/off is reversed (:

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
406
Followers
14
Author:oomlout