Hide minor edits - Show changes to markup
Examples > Digital I/O
This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on). It also debounces the input, without which pressing the button once would appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
Ejemplos > E/S (I/O) Digital
Este ejemplo muestra el uso de un pulsador como un interruptor: cada vez que pulsas el botón, el LED (o lo que sea) es encendido (si está apagado) o apagado (si está encendido). Esto requiere anti rebotes en la entrada, sin el una sola pulsación puede aparecer para el código como muchas pulsaciones. Haz uso de la función millis() para no perder de vista el momento de pulsar el botón.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic:
click the image to enlarge
imagen desarrollada usando Fritzing. para más circuitos de ejemplo, visita lapágina del proyecto Fritzing
Esquema:
haz click en la imagen para ampliarla
The code below is based on Limor Fried's version of debounce, but the logic is inverted from her example. In her example, the switch returns LOW when closed, and HIGH when open. Here, the switch returns HIGH when pressed and LOW when not pressed.
El código está basado en la versión de anti rebotes de Limor Fried, pero la lógica está invertida en el ejemplo. En su ejemplo, el switch retorna LOW cuando está cerrado, y HIGH cuando está abierto. Aquí, el switch retorna HIGH cuando está pulsado y LOW cuando no está pulsado.
/* Debounce Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit (i.e. to ignore noise). The circuit: * LED attached from pin 13 to ground * pushbutton attached from pin 2 to +5V * 10K resistor attached from pin 2 to ground * Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example. created 21 November 2006 by David A. Mellis modified 3 Jul 2009 by Limor Fried http://www.arduino.cc/en/Tutorial/Debounce */
(:includeurl http://arduino.googlecode.com/svn/trunk/build/shared/examples/Digital/Debounce/Debounce.pde border=0:)
// 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 ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); }
void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin);
// check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); }
if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer // than the debounce delay, so take it as the actual current state: buttonState = reading; }
// set the LED using the state of the button: digitalWrite(ledPin, buttonState);
// save the reading. Next time through the loop, // it'll be the lastButtonState: lastButtonState = reading; }
image developed using Fritzing. For more circuit examples, see the Fritzing project page
/*
Debounce
(:div class=code :)
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
The circuit: * LED attached from pin 13 to ground * pushbutton attached from pin 2 to +5V * 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006 by David A. Mellis modified 3 Jul 2009 by Limor Fried
http://www.arduino.cc/en/Tutorial/Debounce */
/* Debounce Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit (i.e. to ignore noise). The circuit: * LED attached from pin 13 to ground * pushbutton attached from pin 2 to +5V * 10K resistor attached from pin 2 to ground * Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example. created 21 November 2006 by David A. Mellis modified 3 Jul 2009 by Limor Fried http://www.arduino.cc/en/Tutorial/Debounce */
// 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
// 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 ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin
// Variables will change: int ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers
// the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT);
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin);
// check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise:
// check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
// If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer // than the debounce delay, so take it as the actual current state:
// set the LED using the state of the button: digitalWrite(ledPin, buttonState);
// set the LED using the state of the button: digitalWrite(ledPin, buttonState);
// save the reading. Next time through the loop, // it'll be the lastButtonState:
// save the reading. Next time through the loop, // it'll be the lastButtonState:
(:divend:)
[@
Debounce
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
The circuit: * LED attached from pin 13 to ground * pushbutton attached from pin 2 to +5V * 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006 by David A. Mellis modified 3 Jul 2009 by Limor Fried
http://www.arduino.cc/en/Tutorial/Debounce */
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
// 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
The circuit: * LED attached from pin 13 to ground * pushbutton attached from pin 2 to +5V * 10K resistor attached from pin 2 to ground
// Variables will change: int ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin
* Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example.
// the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
created 21 November 2006 by David A. Mellis modified 3 Jul 2009 by Limor Fried
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise:
http://www.arduino.cc/en/Tutorial/Debounce */
// 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 ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the switch into a local variable: int reading = digitalRead(buttonPin);
// check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// set the LED using the state of the button: digitalWrite(ledPin, buttonState);
// save the buttonState. Next time through the loop, // it'll be the lastButtonState: lastButtonState = reading;
} @]
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// set the LED using the state of the button: digitalWrite(ledPin, buttonState);
// save the reading. Next time through the loop, // it'll be the lastButtonState: lastButtonState = reading; }
The code below is based on Limor Fried's version of debounce, but the logic is inverted from her example. In her example, the switch returns LOW when closed, and HIGH when open. Here, the switch returns HIGH when pressed and LOW when not pressed.
modified 17 Jun 2009 by Tom Igoe
modified 3 Jul 2009 by Limor Fried
// the follow variables are longs because the time, measured in milliseconds,
// the following variables are long's because the time, measured in miliseconds,
long debounceDelay = 200; // the debounce time, increase if the output flickers
long debounceDelay = 50; // the debounce time; increase if the output flickers
buttonState = digitalRead(buttonPin);
// read the state of the switch into a local variable: int reading = digitalRead(buttonPin);
if ((buttonState == HIGH) &&
(lastButtonState == LOW) &&
(millis() - lastDebounceTime) > debounceDelay) {
// toggle the output
if (ledState == HIGH) {
ledState = LOW;
} else {
ledState = HIGH;
}
// ... and store the time of the last button press
// in a variable:
lastDebounceTime = millis();
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
// set the LED using the ledState variable: digitalWrite(ledPin, ledState);
// set the LED using the state of the button: digitalWrite(ledPin, buttonState);
lastButtonState = buttonState;
lastButtonState = reading;
A push-button on pin 7 and an LED on pin 13.

* pushbutton attached from pin 7 to +5V * 10K resistor attached from pin 7 to ground
* pushbutton attached from pin 2 to +5V * 10K resistor attached from pin 2 to ground
const int buttonPin = 7; // the number of the pushbutton pin
const int buttonPin = 2; // the number of the pushbutton pin
int inPin = 7; // the number of the input pin int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin int reading; // the current reading from the input pin int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
The circuit: * LED attached from pin 13 to ground * pushbutton attached from pin 7 to +5V * 10K resistor attached from pin 7 to ground
* Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006 by David A. Mellis modified 17 Jun 2009 by Tom Igoe
http://www.arduino.cc/en/Tutorial/Debounce */
// 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 ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin
// the follow variables are longs because the time, measured in milliseconds,
long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers
void setup() {
pinMode(inPin, INPUT); pinMode(outPin, OUTPUT);
long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 200; // the debounce time, increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT);
void loop() {
reading = digitalRead(inPin);
// if we just pressed the button (i.e. the input went from LOW to HIGH),
// and we've waited long enough since the last press to ignore any noise...
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
// ... invert the output
if (state == HIGH)
state = LOW;
else
state = HIGH;
// ... and remember when the last button press was
time = millis();
void loop() {
buttonState = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
if ((buttonState == HIGH) &&
(lastButtonState == LOW) &&
(millis() - lastDebounceTime) > debounceDelay) {
// toggle the output
if (ledState == HIGH) {
ledState = LOW;
} else {
ledState = HIGH;
}
// ... and store the time of the last button press
// in a variable:
lastDebounceTime = millis();
digitalWrite(outPin, state);
previous = reading;
// set the LED using the ledState variable: digitalWrite(ledPin, ledState);
// save the buttonState. Next time through the loop, // it'll be the lastButtonState: lastButtonState = buttonState;
This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on). It also debounces the input, without which pressing the button once would appear to the code as multiple presses.
This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on). It also debounces the input, without which pressing the button once would appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
A push-button on pin 2 and an LED on pin 13.
A push-button on pin 7 and an LED on pin 13.
int inPin = 2; // the number of the input pin
int inPin = 7; // the number of the input pin
A push-button on pin 7 and an LED on pin 13.
A push-button on pin 2 and an LED on pin 13.
/*
* Debounce * by David A. Mellis * * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button * press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's * a minimum delay between toggles to debounce the circuit (i.e. to ignore * noise). * * http://www.arduino.cc/en/Tutorial/Debounce */
int inPin = 7; // the number of the input pin
int inPin = 2; // the number of the input pin
// if the input just went from LOW and HIGH and we've waited long enough // to ignore any noise on the circuit, toggle the output pin and remember // the time
// if we just pressed the button (i.e. the input went from LOW to HIGH), // and we've waited long enough since the last press to ignore any noise...
// ... invert the output
digitalWrite(outPin, state);
time = millis();
// ... and remember when the last button press was
time = millis();
digitalWrite(outPin, state);
A push-button on pin 2 and an LED on pin 13.
A push-button on pin 7 and an LED on pin 13.
int inPin = 2; // the number of the input pin
int inPin = 7; // the number of the input pin
/* switch
/*
* Debounce * by David A. Mellis
* David A. Mellis * 21 November 2006
* http://www.arduino.cc/en/Tutorial/Debounce
if (DEBUG)
Serial.begin(19200);
time = millis();
digitalWrite(outPin, state);
time = millis();
digitalWrite(outPin, state);
Examples > Digital I/O
This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on). It also debounces the input, without which pressing the button once would appear to the code as multiple presses.
A push-button on pin 2 and an LED on pin 13.

/* switch
*
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*
* David A. Mellis
* 21 November 2006
*/
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
if (DEBUG)
Serial.begin(19200);
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}