Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Project 2: Button Controlled LED with ESP32

Introduction

This project introduces digital input handling with ESP32. A push button will be used to control an LED, turning it on when the button is pressed and off when released.

Required Components

  • ESP32 Board
  • LED (5mm)
  • 220Ω, 10KΩ Resistor
  • Push Button
  • Breadboard

Pinout

  • Switch Pinouts
    • Button Pressed: When you press the button, the metal contacts inside touch, creating a connection. This allows electricity to flow, completing the circuit.
    • Button Released: When you release the button, the contacts separate, breaking the connection. This stops the flow of electricity, turning off whatever device it’s controlling.
  • LED Pinout
    • LED Anode (+)
    • LED Cathode (-)

Circuit Diagram

  • ESP32 GPIO 5220Ω Resistor → LED (Anode +)
  • ESP32 GNDLED (Cathode -)
  • ESP32 GPIO 18One Side of Push Button
  • ESP32 GNDOne Side of Push Button
  • ESP32 3.3VOne Side of Push Button

Arduino Code

#define LED_PIN 5
#define BUTTON_PIN 18

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Internal pull-up resistor enabled
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);
  
  if (buttonState == LOW) {  // Button pressed
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Explanation

  • Push button is connected to GPIO 18 with an internal pull-up resistor.
  • When the button is pressed, GPIO 18 reads LOW, and the LED turns on.
  • When the button is released, GPIO 18 reads HIGH, and the LED turns off.

Troubleshooting

  • LED Not Responding? Check if GPIO 18 is correctly connected to the button.
  • LED Always ON? Ensure pull-up resistor is enabled or manually add a 10kΩ resistor.
  • Button Not Working? Try changing GPIO 18 to another pin in the code.

    Leave a Reply

    Your email address will not be published.

    Need Help?