Push Button with ESP8266 (NodeMCU / Wemos D1 Mini)

adminESP82661 week ago7 Views

Introduction

A push button is a simple switch that connects or disconnects a circuit when pressed. With ESP8266, push buttons are used for:

  • Manual inputs (turn ON/OFF devices)
  • Reset / Mode switching
  • User interaction in IoT projects

In this tutorial, we’ll learn how to:

  1. Connect a push button to ESP8266
  2. Write code to detect button presses
  3. Control an LED with the button

Components Required

  • ESP8266 board (NodeMCU / Wemos D1 Mini)
  • Push button
  • 1 × 10kΩ resistor (for pull-down)
  • 1 × LED + 220Ω resistor (optional, for testing output)
  • Breadboard and jumper wires

Circuit Diagram

We’ll connect the button to GPIO D5 (GPIO14).

Wiring:

  • One side of button → D5 (GPIO14)
  • Other side of button → GND
  • Use INPUT_PULLUP (no need for external resistor)

👉 This means the button is normally HIGH, and goes LOW when pressed.

Example#1 Read Push Button State using ESP8266

int buttonPin = D5;   // GPIO14
int buttonState = 0;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP); // Internal pull-up resistor
}

void loop() {
  buttonState = digitalRead(buttonPin);  // Read button

  if (buttonState == LOW) {  // Pressed
    Serial.println("Button Pressed!");
  } else {
    Serial.println("Button Released!");
  }
  delay(200);
}

Explanation:

  • INPUT_PULLUP → Keeps pin HIGH by default.
  • When pressed → Pin connects to GND → Reads LOW.
  • Serial Monitor prints button state.

Circuit Diagram

We’ll use D5 (GPIO14) for button input and D6 (GPIO12) for LED output.

Connections:

  • Push Button Pin 1 → D5 (GPIO14)
  • Push Button Pin 2 → GND
  • 10kΩ Resistor → Between D5 and GND (pull-down)
  • LED Anode (+) → D2 (GPIO5) via 220Ω resistor
  • LED Cathode (–) → GND

(Alternatively, we can use ESP8266’s internal pull-up resistor to skip the external one.)

Example#2 Push Button Controlling LED using ESP8266

int buttonPin = D5;  // Button pin
int ledPin = D2;     // LED pin (GPIO4)
int buttonState = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {  // Button pressed
    digitalWrite(ledPin, HIGH);  // LED ON
  } else {
    digitalWrite(ledPin, LOW);   // LED OFF
  }
}

👉 LED turns ON when button is pressed.

Example#3 Toggle LED with Push Button (Like a Switch) using ESP8266

int buttonPin = D5;
int ledPin = D2;
int buttonState;
int lastButtonState = HIGH;
bool ledState = LOW;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW && lastButtonState == HIGH) {
    ledState = !ledState;  // Toggle LED state
    digitalWrite(ledPin, ledState);
    delay(200); // Debounce delay
  }

  lastButtonState = buttonState;
}

Real-Life Applications

  • Turning lights/fans ON & OFF in smart homes
  • Manual override button for IoT projects
  • Reset button for microcontrollers
  • Security system arming/disarming

Troubleshooting

  • If button always reads LOW → Check wiring (may need external resistor).
  • If LED flickers → Add a small debounce delay (50–200 ms).
  • Always prefer INPUT_PULLUP → Simpler wiring, less error.

Conclusion

You learned how to:

  1. Read button state with ESP8266.
  2. Control an LED with button press.
  3. Toggle devices using a button.

This is a foundation project for many IoT applications like doorbells, alarms, and smart switches. 🚀

Can I connect multiple buttons to ESP8266?

Yes, just assign each button to a separate GPIO pin.

Why does my button read HIGH when not pressed?

That’s expected if you’re using a pull-up resistor.

How do I reduce button bouncing?

Use software debounce (small delay) or a hardware capacitor.

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Loading Next Post...
Follow
Search Trending
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...