Tutorial.Button History

Show minor edits - Show changes to markup

May 02, 2012, at 03:35 AM by Scott Fitzgerald -
Changed line 40 from:
to:
November 16, 2011, at 04:05 AM by Scott Fitzgerald -
Changed line 40 from:
to:
September 30, 2011, at 03:02 AM by Scott Fitzgerald -
Changed line 40 from:
to:
September 16, 2010, at 10:04 PM by Tom Igoe -
Changed line 40 from:
to:
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:
to:

August 24, 2010, at 10:51 PM by Christian Cerrito -
Deleted lines 45-46:
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:
to:
August 24, 2010, at 08:20 PM by Christian Cerrito -
Changed lines 45-46 from:
to:
August 24, 2010, at 08:19 PM by Christian Cerrito -
Added lines 6-11:

Circuit

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Changed lines 23-28 from:

Circuit

image developed using Fritzing. For more circuit examples, see the Fritzing project page

to:
Changed lines 36-50 from:
to:
August 24, 2010, at 08:07 AM by Christian Cerrito -
Changed line 33 from:
to:
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:
 /*
   Button
  
  Turns on and off a light emitting diode(LED) connected to digital  
  pin 13, when pressing a pushbutton attached to pin 2. 
  
  
  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 <http://www.0j0.org>
  modified 17 Jun 2009
  by Tom Igoe
  
   http://www.arduino.cc/en/Tutorial/Button
  */
to:
Deleted lines 34-64:
 // 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); 
   }
 }
November 18, 2009, at 04:38 PM by David A. Mellis -
Changed line 37 from:
  pin 13, when pressing a pushbutton attached to pin 7. 
to:
  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 fritzing

to:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

August 27, 2009, at 08:43 PM by Tom Igoe -
Changed lines 20-22 from:
to:

image developed using fritzing

Changed lines 28-29 from:

image developed using fritzing

to:
August 27, 2009, at 08:42 PM by Tom Igoe -
Changed lines 26-27 from:
to:

image developed using fritzing

July 05, 2009, at 07:12 PM by Tom Igoe -
Changed line 86 from:
to:
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:
Changed lines 55-57 from:

[@ /*

  Button
to:
 // 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
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:
 // variables will change:
 int buttonState = 0;         // variable for reading the pushbutton status
Added lines 63-68:
 void setup() {
   // initialize the LED pin as an output:
   pinMode(ledPin, OUTPUT);      
   // initialize the pushbutton pin as an input:
   pinMode(buttonPin, 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:
 void loop(){
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
Changed lines 74-75 from:
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
to:
   // 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); 
   }
 }
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:
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:

Code

to:

Schematic:

click the image to enlarge

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:
to:
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

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