Control LED Using PushButton With NodeMCU

101K4617

Intro: Control LED Using PushButton With NodeMCU

Hello Makers,

In this instructable we will see the easiest way to connect the Push Button to NodeMCU. And control LED using PushButton.

STEP 1: Materials Required

List of parts required for this instructable :

Hardware Required

  • NodeMCU
  • Push Button
  • 10K ohm resistor
  • Bread board
  • Micro USB cable
  • Connecting Wires

Software Required

  • Arduino IDE (with ESP8266 Library installed)

STEP 2: Description

The PushButton is a electronic component that connects two points in a circuit when you press it. We use these little buttons on everything!

These Miniature Single Pole Single Throw (SPST) switches are breadboard friendly. Perfect as a tactile reset switch. These buttons are rated up to 50mA.

Typical Push Button Switches Applications : Calculators, push-button telephones, various other mechanical and electronic devices, home and commercial.

STEP 3: How PushButton Work's

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to 3.3v (through the pull-up resistor) and we read a HIGH.

When the pushbutton is closed (pressed), there is 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)

So, Let's get started with connecting PushButton to NodeMCU and control LED.

STEP 4: Wiring Connections

Push Button connections :

The first pin goes from one leg of the pushbutton through a pull-up resistor(here 10K Ohms) to the 5v supply.

The second pin goes from the corresponding leg of the pushbutton to Ground (GND) pin.

The third pin connects to a Digital I/O pin (here pin D0) which reads the button's state.

LED connections :

LED Anode is connected to Digital I/O pin (here pin D1) and Cathode to ground (GND) pin.

STEP 5: Coding Process

int led = 5; // LED pin
int button = 16; // push button is connected int temp = 0; // temporary variable for reading the button pin status

void setup() {
pinMode(led, OUTPUT); // declare LED as output pinMode(button, INPUT); // declare push button as input }

void loop() {
temp = digitalRead(button); if (temp == HIGH) { digitalWrite(led, HIGH); Serial.println("LED Turned ON"); delay(1000); } else { digitalWrite(led, LOW); Serial.println("LED Turned OFF"); delay(1000); } }

Download the "Button_NodeMCU.ino" file and open it up in the Arduino IDE.

Then Create a new sketch and paste the code below in the Arduino IDE and hit Upload. You can tinker with it if you like based on the application, or just use it as it is.

STEP 6: Output

That's all Makers.

You have successfully completed another instructable, It takes pretty less time to create this instructable, and its fun too.

10 Comments

Could you please update this to proper terminology and proper wiring? Your diagram shows a 10K pulldown instead of a 10K pullup. A pullup resistor goes on the other side of th switch and connects 3.3V or 5V directly to the pin. This would also work as a current limiting resistor to both the input and the ground. In the way it is currently setup you only have the limit between Vin and GND. There is probably an internal resistor so that the D0 pin doesn't get shorted so it's not necessary. It would explain your wiring properly though.

Is it obligatory to connect one pin to 5v? Why we can not connect to 3.3v pin?

Looks like it's a typo, there isn't a 5v pin on the ESP8266
Actually, NodeMCU has a 5v pin marked as "Vin".
So you could connect the button directly to the LED and do away with the microcontroller, rigth?
Can you do a similar thing with a momentary 2 pin switch?
Hi there...! Just a small query. Is a pull-down register necessary?
Thank you for great explanation. I am wondering if you know how to combine physical pushbutton with an html (virtual) button so this LED could be controlled from browser and/or button? Thanks again.

Awesome article, Thanks! It really helped me a lot. Is it possible to have two different push buttons to control two different functions?

Thanks! Seems like such a simple thing, but this really helped me!