Introduction: ESP8266 - Led Blink With Button
Today, we are going to put together a program that, at the press of a button, will light an LED that already comes embedded in our WiFi ESP8266 NodeMcu ESP12E. Once again, we will work with ESP8266, and if you have never programmed a board, I recommend that you watch this video: INTRODUCTION TO ESP8266, where I teach all the configurations of this model in the Arduino IDE.
Step 1: WiFi ESP8266 NodeMcu ESP-12E
This NodeMCU is an ESP that I like a lot, because it has the USB input that allows for automatic feeding. I show all the pins of this board in this diagram, but in this project, we will connect the button specifically to the D1 pin.
Step 2: Assembly
Step 3: Code
Setup
In the setup () function, we will define the behavior of the pins we will use, in this case, the LED and the BUTTON.
void setup()
{ // Instrução para colocar o gpio que iremos utilizar como entrada, // podemos fazer a leitura nesse pino pinMode(D1, INPUT); // D1 é uma constante que indica o pino que ligamos nosso botão // Instrução para colocar o gpio que iremos utilizar como saída, // podemos alterar seu valor livremente para HIGH ou LOW pinMode(LED_BUILTIN, OUTPUT); // LED_BUILTIN é uma constante que indica o LED do ESP8266 }
Loop
In the loop () function, we will make the logic to read the BUTTON (if pressed or not) and according to the value of the button, it will turn the LED on or off.
void loop()
{ // faz a leitura do pino D1 (no nosso caso, o botão está ligado nesse pino) ´ byte valor = digitalRead(D1); // checa se o botão está pressionado if(valor == HIGH) { digitalWrite(LED_BUILTIN, LOW); // Acende o LED pino 16 } else { digitalWrite(LED_BUILTIN, HIGH); // Apaga o LED } }