Control LED Using PushButton With NodeMCU

49,205

40

9

Introduction: Control LED Using PushButton With NodeMCU

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @makers_bee & Motivate me on YouTube @MakersBee

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.

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

Be the First to Share

    Recommendations

    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020

    9 Discussions

    0
    StanislavB13
    StanislavB13

    2 years ago

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

    0
    greg29209
    greg29209

    Reply 14 days ago

    Looks like it's a typo, there isn't a 5v pin on the ESP8266

    1
    StanislavB13
    StanislavB13

    Reply 14 days ago

    Actually, NodeMCU has a 5v pin marked as "Vin".

    0
    punund
    punund

    8 weeks ago

    So you could connect the button directly to the LED and do away with the microcontroller, rigth?

    0
    Flyazza
    Flyazza

    Question 3 months ago on Step 1

    Can you do a similar thing with a momentary 2 pin switch?

    0
    spidershere
    spidershere

    Question 6 months ago

    Hi there...! Just a small query. Is a pull-down register necessary?

    0
    DmitriP5
    DmitriP5

    1 year ago

    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.

    0
    PaulL343
    PaulL343

    2 years ago

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

    0
    bdowell
    bdowell

    2 years ago on Introduction

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