Arduino - Botão - LED

Vamos aprender a:

Vamos aprender a alternar o LED a cada vez que o botão é pressionado no tutorial Arduino - Botão Alterna LED.

Sobre LED e Botão

Se você não conhece o LED e o botão (pinagem, como funcionam, como programar...), aprenda sobre eles nos seguintes tutoriais:

Diagrama de Ligação

Diagrama de Ligação Arduino Botão LED

This image is created using Fritzing. Click to enlarge image

Código Arduino

/* * Este código de Arduino foi desenvolvido por pt.newbiely.com * Este código de Arduino é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/tutorials/arduino/arduino-button-led */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int LED_PIN = 3; // 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(LED_PIN, OUTPUT); // initialize the pushbutton pin as an pull-up input: // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(BUTTON_PIN); // control LED according to the state of button if(buttonState == LOW) // If button is pressing digitalWrite(LED_PIN, HIGH); // turn on LED else // otherwise, button is not pressing digitalWrite(LED_PIN, LOW); // turn off LED }

Passos Rápidos

  • Conecte o Arduino ao PC via cabo USB
  • Abra a Arduino IDE, selecione a placa e a porta corretas
  • Copie o código acima e abra com a Arduino IDE
  • Clique no botão Upload na Arduino IDE para carregar o código no Arduino
Arduino IDE Carregar Código
  • Pressione e mantenha o botão pressionado por alguns segundos
  • Veja a mudança do estado do LED

Explicação do Código

Leia a explicação linha por linha nos comentários do código-fonte!

Vídeo

※ NOSSAS MENSAGENS

  • Fique à vontade para compartilhar o link deste tutorial. Porém, por favor, não use nosso conteúdo em outros sites. Investimos muito esforço e tempo para criar este conteúdo, por favor respeite nosso trabalho!