O botão é um componente que conecta dois pontos do circuito quando está pressionado. Neste exemplo quando o botão está pressionado o Led se acende.
Componentes necessários:
- Arduino
- Botão
- Resistor 10kΩ
- Protoboard
- Jumpers
- 1 Cabo USB
Circuito
Conecte 3 jumpers à placa Arduino. Os dois primeiros, vermelho e preto, conecte às duas linhas verticais laterais da protoboard para prover acesso ao suprimento de 5 volts e terra. O terceiro jumper vai do pino digital 2 até uma perna do botão. Esta mesma perna do botão conecta um resistor (neste caso de 10kΩ) ao terra. A outra perna do botão conecta ao suprimento de 5 volts.
Quando o botão está aberto (não pressionado) não há conexão entre as duas saídas do botão, então o pino está conectado ao terra (através do resistor pull-up) e podemos ler um LOW. Quando o botão está fechado (pressionado) faz a conexão entre as saídas do botão e fazendo a conexão com 5 volts, e com isso podemos ler HIGH.
Você também poderá fazer a instalação no sentido oposto com um resistor pull-down mantendo o input HIGH e mudando para LOW com o botão pressionado. Se fizer assim, o comportamento do projeto será invertido, com o Led normalmente aceso e apagando quando o botão for pressionado.
Se você desconectar o pino digital e/s de todo o resto, o Led poderá piscar com erro. Isso acontece porque o input está flutuante, isso é, ele vai mais ou menos aleatoriamente informar HIGH ou LOW. Por isso é necessário o resistor pull-up ou pull-down neste circuito.
Esquema
Código
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 30 Aug 2011
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 = 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);
}
}
O conteúdo desta página é uma tradução para o português a partir do site original do Arduino.