Tutorial.Button History

Hide minor edits - Show changes to output

May 02, 2012, at 03:35 AM by Scott Fitzgerald -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Button/Button.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/02.Digital/Button/Button.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:05 AM by Scott Fitzgerald -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/2.Digital/Button/Button.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Button/Button.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:02 AM by Scott Fitzgerald -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Button/Button.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/2.Digital/Button/Button.ino lang=arduino tabwidth=4:)
September 16, 2010, at 10:04 PM by Tom Igoe -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Button/Button.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Button/Button.pde lang=arduino tabwidth=4:)
September 08, 2010, at 08:26 PM by Tom Igoe -
August 24, 2010, at 10:53 PM by Christian Cerrito -
Changed lines 49-52 from:
* [[(Reference/)if]]
* [[(Reference/)else]]

to:
* [[(Reference/)if]]()
* [[(Reference/)else]]()

August 24, 2010, at 10:51 PM by Christian Cerrito -
Deleted lines 45-46:
* [[(Reference/)setup]]()
* [[(Reference/)loop]]()
August 24, 2010, at 10:45 PM by Christian Cerrito -
Changed lines 6-12 from:
to:
'''Hardware'''
* Arduino Board
* momentary button or switch
* 10K ohm resistor
* breadboard
* hook-up wire
August 24, 2010, at 10:43 PM by Christian Cerrito -
Changed line 3 from:
!! Button
to:
!!! Button
August 24, 2010, at 08:21 PM by Christian Cerrito -
Changed lines 45-46 from:
* [[(Reference/)Else]]
to:
* [[(Reference/)if]]
* [[(Reference/)else]]

August 24, 2010, at 08:20 PM by Christian Cerrito -
Changed lines 45-46 from:
to:
* [[(Reference/)Else]]
August 24, 2010, at 08:19 PM by Christian Cerrito -
Added lines 6-11:

!!!Circuit

Attach:button.png
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]
Changed lines 23-28 from:
!!!Circuit

Attach:button.png
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

to:
Changed lines 36-50 from:
(:divend:)
to:
(:divend:)


''See Also:'''
* [[(Reference/)setup]]()
* [[(Reference/)loop]]()
* [[(Reference/)pinMode]]()
* [[(Reference/)digitalWrite]]()
* [[(Reference/)digitalRead]]()

* [[BareMinimum]] - bare minimum code needed to start a sketch
* [[DigitalReadSerial]] - read a switch, print the state out to the Serial Monitor
* [[Blink]] - turn an LED on and off
* [[Debounce]] - read a pushbutton filtering noise
* [[Button State Change]] - counting the number of button pushes
August 24, 2010, at 08:07 AM by Christian Cerrito -
Changed line 33 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Digital/Button/Button.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/2.Digital/Button/Button.pde lang=arduino tabwidth=4:)
August 18, 2010, at 12:08 AM by Christian Cerrito -
February 24, 2010, at 04:22 AM by Tom Igoe -
Changed lines 33-55 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Button
%color=#7e7e7e%
%color=#7e7e7e% Turns on and off a light emitting diode(LED) connected to digital
%color=#7e7e7e% pin 13, when pressing a pushbutton attached to pin 2.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * LED attached from pin 13 to ground
%color=#7e7e7e% * pushbutton attached to pin 2 from +5V
%color=#7e7e7e% * 10K resistor attached to pin 2 from ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: on most Arduinos there is already an LED on the board
%color=#7e7e7e% attached to pin 13.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% created 2005
%color=#7e7e7e% by DojoDave <http://www.0j0.org>
%color=#7e7e7e% modified 17 Jun 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/Button
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Digital/Button/Button.pde lang=arduino tabwidth=4:)
Deleted lines 34-64:
%color=#7e7e7e%// constants won't change. They're used here to
%color=#7e7e7e%// set pin numbers:
const %color=#cc6600%int%% buttonPin = 2; %color=#7e7e7e%// the number of the pushbutton pin
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// the number of the LED pin

%color=#7e7e7e%// variables will change:
%color=#cc6600%int%% buttonState = 0; %color=#7e7e7e%// variable for reading the pushbutton status

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// initialize the LED pin as an output:
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
%color=#7e7e7e%// initialize the pushbutton pin as an input:
%color=#cc6600%pinMode%%(buttonPin, %color=#006699%INPUT%%);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%(){
%color=#7e7e7e%// read the state of the pushbutton value:
buttonState = %color=#cc6600%digitalRead%%(buttonPin);

%color=#7e7e7e%// check if the pushbutton is pressed.
%color=#7e7e7e%// if it is, the buttonState is HIGH:
%color=#cc6600%if%% (buttonState == %color=#006699%HIGH%%) {
%color=#7e7e7e%// turn LED on:
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%HIGH%%);
}
%color=#cc6600%else%% {
%color=#7e7e7e%// turn LED off:
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%LOW%%);
}
}
November 18, 2009, at 04:38 PM by David A. Mellis -
Changed line 37 from:
%color=#7e7e7e% pin 13, when pressing a pushbutton attached to pin 7.
to:
%color=#7e7e7e% pin 13, when pressing a pushbutton attached to pin 2.
August 27, 2009, at 08:45 PM by Tom Igoe -
Changed lines 20-22 from:
[-image developed using [[(http://www.)fritzing(.org)]]-]

to:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

August 27, 2009, at 08:43 PM by Tom Igoe -
Changed lines 20-22 from:
to:
[-image developed using [[(http://www.)fritzing(.org)]]-]

Changed lines 28-29 from:
[-image developed using [[(http://www.)fritzing(.org)]]-]
to:
August 27, 2009, at 08:42 PM by Tom Igoe -
Changed lines 26-27 from:
to:
[-image developed using [[(http://www.)fritzing(.org)]]-]
July 05, 2009, at 07:12 PM by Tom Igoe -
Changed line 86 from:
(:divend:)
to:
(:divend:)
July 05, 2009, at 07:11 PM by Tom Igoe -
Changed lines 27-28 from:
!!!Code
to:
!!!Code
July 05, 2009, at 07:11 PM by Tom Igoe -
Added lines 27-53:
!!!Code

(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% Button
%color=#7e7e7e%
%color=#7e7e7e% Turns on and off a light emitting diode(LED) connected to digital
%color=#7e7e7e% pin 13, when pressing a pushbutton attached to pin 7.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * LED attached from pin 13 to ground
%color=#7e7e7e% * pushbutton attached to pin 2 from +5V
%color=#7e7e7e% * 10K resistor attached to pin 2 from ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: on most Arduinos there is already an LED on the board
%color=#7e7e7e% attached to pin 13.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% created 2005
%color=#7e7e7e% by DojoDave <http://www.0j0.org>
%color=#7e7e7e% modified 17 Jun 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/Button
%color=#7e7e7e% */
Changed lines 55-57 from:
[@
/*
Button
to:
%color=#7e7e7e%// constants won't change. They're used here to
%color=#7e7e7e%// set pin numbers:
const %color=#cc6600%int%% buttonPin = 2; %color=#7e7e7e%// the number of the pushbutton pin
const %color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// the number of the LED pin
Changed lines 60-61 from:
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
to:
%color=#7e7e7e%// variables will change:
%color=#cc6600%int%% buttonState = 0; %color=#7e7e7e%// variable for reading the pushbutton status
Added lines 63-68:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// initialize the LED pin as an output:
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
%color=#7e7e7e%// initialize the pushbutton pin as an input:
%color=#cc6600%pinMode%%(buttonPin, %color=#006699%INPUT%%);
}
Changed lines 70-73 from:
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
to:
%color=#cc6600%void%% %color=#cc6600%'''loop'''%%(){
%color=#7e7e7e%// read the state of the pushbutton value:
buttonState = %color=#cc6600%digitalRead%%(buttonPin);
Changed lines 74-75 from:
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
to:
%color=#7e7e7e%// check if the pushbutton is pressed.
%color=#7e7e7e%// if it is, the buttonState is HIGH:
%color=#cc6600%if%% (buttonState == %color=#006699%HIGH%%) {
%color=#7e7e7e%// turn LED on:
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%HIGH%%);
}
%color=#cc6600%else%% {
%color=#7e7e7e%// turn LED off:
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%LOW%%);
}
}
Changed lines 86-125 from:

created 2005
by DojoDave <http://www.0j0.org>
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);
}
}
@]
to:
(:divend:)
July 02, 2009, at 11:48 PM by Tom Igoe -
Changed line 33 from:
pin 13, when pressing a pushbutton attached to pin 7.
to:
pin 13, when pressing a pushbutton attached to pin 2.
Changed lines 38-39 from:
* pushbutton attached to pin 7 from +5V
* 10K resistor attached to pin 7 from ground
to:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
Changed line 55 from:
const int buttonPin = 7; // the number of the pushbutton pin
to:
const int buttonPin = 2; // the number of the pushbutton pin
June 25, 2009, at 02:04 PM by Tom Igoe -
Changed lines 4-8 from:

The pushbutton is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button.

We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 10 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 2) which reads the button's state.
to:
Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the button.

Connect three wires to the Arduino board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10 KOhms) to ground. The other leg of the button connects to the 5 volt supply.

Changed lines 10-15 from:
two legs of the pushbutton, so the pin is connected to 5 volts (through the pull-up resistor) and we read a HIGH. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to ground, so that we read a LOW. (The pin is still connected to 5 volts, but the resistor in-between them means that the pin is "closer" to ground.)

You can also wire this circuit the opposite way, with a pull-down resistor keeping the input LOW, and going HIGH when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will more-or-less randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.
to:
two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.


You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.
June 25, 2009, at 01:56 PM by Tom Igoe -
June 25, 2009, at 01:56 PM by Tom Igoe -
Changed lines 18-20 from:
Attach:Button.jpg

!!!Code
to:
Attach:button.png

'''Schematic:'''

[-click the image to enlarge-]

%width=400px%[[Attach:button_schem.png | Attach:button_schem.png]]
June 24, 2009, at 10:53 PM by Tom Igoe -
Changed lines 14-15 from:
If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will more-or-less randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resister in the circuit.
to:
If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will more-or-less randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.
June 17, 2009, at 11:19 PM by Tom Igoe -
Changed lines 23-26 from:
int ledPin = 13; // choose the pin for the LED
int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
to:
/*
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
* 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 <http://www.0j0.org>
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 = 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
Changed lines 56-57 from:
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
to:
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Changed lines 63-67 from:
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
to:
// 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);
November 01, 2008, at 11:31 PM by David A. Mellis -
Changed lines 7-8 from:
We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 2.2 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 7) which reads the button's state.
to:
We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 10 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 2) which reads the button's state.
Changed lines 18-19 from:
Attach:PushButton.jpg
to:
Attach:Button.jpg
January 17, 2008, at 04:02 PM by David A. Mellis -
Added lines 14-15:
If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will more-or-less randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resister in the circuit.
June 12, 2007, at 03:56 PM by David A. Mellis - mentioning pull-down resistors (in addition to pull-up)
Added lines 12-13:
You can also wire this circuit the opposite way, with a pull-down resistor keeping the input LOW, and going HIGH when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.
April 11, 2007, at 06:10 PM by David A. Mellis -
Deleted lines 18-26:
/* invert
* <http://www.arduino.cc/en/Tutorial/Invert>
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
* concept of Active-Low, which consists in connecting buttons using a
* 1K to 10K pull-up resistor.
*
*/
February 03, 2007, at 03:03 PM by David A. Mellis -
Added lines 1-45:
''Examples > Digital I/O''

!! Button

The pushbutton is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button.

We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 2.2 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 7) which reads the button's state.

When the pushbutton is open (unpressed) there is no connection between the
two legs of the pushbutton, so the pin is connected to 5 volts (through the pull-up resistor) and we read a HIGH. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to ground, so that we read a LOW. (The pin is still connected to 5 volts, but the resistor in-between them means that the pin is "closer" to ground.)

!!!Circuit

Attach:PushButton.jpg

!!!Code

[@
/* invert
* <http://www.arduino.cc/en/Tutorial/Invert>
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
* concept of Active-Low, which consists in connecting buttons using a
* 1K to 10K pull-up resistor.
*
*/
int ledPin = 13; // choose the pin for the LED
int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}

void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
@]

Share