Instructables

Activate LED with button -- Arduino Example Video

video Activate LED with button -- Arduino Example Video
The shows me assembling the circuit on page 43 of Getting Started with Arduino, by Massimo Banzi.

This example is a very good starting point when using the Arduino system.  It is the basis of my project Blinky Fish

Here is how it works: 
  1. Code is loaded onto the Arduino board from a computer
  2. The Arduino has inputs and outputs
  3. Inputs are like buttons
  4. Outputs are like lights
  5. The Arduino looks for a change in input (indicated by a change in voltage)
  6. Normally the voltage from the button is zero (or ground) (or Low)
  7. When the button is pressed, it connects to to the 5V pin (High)
  8. The Arduino sees the change from zero -> 5V (Low -> Hight)
  9. The code tells the Arduino to turn on the light if it sees that the button is pushed (high, or 1, or 5V)
  10. So the Arduino lights up the light, by setting the output to 5V
  11. It turns off the light by seting the output to the LED back to zero.

Here is the code: (it is an example that comes with the Arduino program, Example -> 2. Digital -> Button

/*
  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 7 from +5V (changed from pin 2 to match page 43 of text, MPC)
* 10K resistor attached to pin 7 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

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 7;     // 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);
  }
}
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!