Introduction: 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:
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);
}
}
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:
- Code is loaded onto the Arduino board from a computer
- The Arduino has inputs and outputs
- Inputs are like buttons
- Outputs are like lights
- The Arduino looks for a change in input (indicated by a change in voltage)
- Normally the voltage from the button is zero (or ground) (or Low)
- When the button is pressed, it connects to to the 5V pin (High)
- The Arduino sees the change from zero -> 5V (Low -> Hight)
- The code tells the Arduino to turn on the light if it sees that the button is pushed (high, or 1, or 5V)
- So the Arduino lights up the light, by setting the output to 5V
- 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);
}
}