DEV Community

Cover image for 🕹️ Control an LED with a Button Using ESP32 and Arduino IDE Introduction
Mohamed Ahmed
Mohamed Ahmed

Posted on

🕹️ Control an LED with a Button Using ESP32 and Arduino IDE Introduction

Introduction

Welcome back, makers! 👋
In the previous post, we connected and controlled an external LED using the ESP32.

Now, let’s take it a step further — by using a push button to turn the LED on and off.
This is a fundamental IoT concept: using input devices (like buttons or sensors) to control output devices (like LEDs or motors).

🧰 What You’ll Need

  1. ESP32 development board (e.g., ESP32 DevKit C)
  2. 1 × LED (any color)
  3. 1 × 220 Ω resistor (for the LED)
  4. 1 × Push button
  5. Breadboard
  6. Jumper wires
  7. USB cable connected to your computer

⚙️ Step 1: Breadboard/circuit Connections

Here’s how to wire everything:

Component ESP32 Pin Description
LED (long leg – anode) GPIO 4 Digital output pin
LED (short leg – cathode) GND Ground
Resistor (220 Ω) In series with LED Limits current to protect the LED
Button GPIO 21 Digital input pin
Other side of Button GND Pulls the button LOW when pressed

💡 Tip: We’ll use the ESP32’s internal pull-up resistor to keep things simple, so you don’t need an extra resistor for the button.

💻 Step 2: Write the Code

Open the Arduino IDE and paste this code:

// Control an LED with a button using ESP32
int ledPin = 4;      // LED connected to GPIO 4
int buttonPin = 21;  // Button connected to GPIO 15
int buttonState = 0; // Variable to store button state

void setup() {
  pinMode(ledPin, OUTPUT);        // Set LED pin as output
  pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
  Serial.begin(115200);
  Serial.println("Button Control Test Started");
}

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

  if (buttonState == LOW) {  // Button pressed (active LOW)
    digitalWrite(ledPin, HIGH);
    Serial.println("Button Pressed - LED ON");
  } else {  // Button not pressed
    digitalWrite(ledPin, LOW);
    Serial.println("Button Released - LED OFF");
  }

  delay(100); // Small delay for stability
}

Enter fullscreen mode Exit fullscreen mode

🧠 Explanation

  • pinMode(buttonPin, INPUT_PULLUP) enables the internal resistor that keeps the button HIGH until pressed.
  • When you press the button, the pin reads LOW.
  • The LED turns ON when the button is pressed and OFF when released.
  • The Serial Monitor displays real-time button states.

🖥️ Step 3: Upload and Test

  1. - Connect your ESP32 to your computer.
  2. - Select the correct Board and Port.
  3. - Click Upload.
  4. - Open Tools → Serial Monitor and set the baud rate to 115200.

Now press and release the button — you should see:

Button Pressed - LED ON
Button Released - LED OFF
Enter fullscreen mode Exit fullscreen mode

Your ESP32 should look like this
1.LED doesn’t turn on:

  • Check your wiring (especially button → GPIO21 → GND).
  • Make sure the LED’s anode (+) is connected to the correct GPIO pin.
  • Verify resistor placement (in series with LED).

2.Serial Monitor not showing messages:

  • Ensure baud rate = 115200.
  • Confirm you added Serial.begin(115200); in setup().

🎯 What’s Next

You’ve now learned how to:
✅ Read digital input from a button
✅ Control an LED based on input
✅ Display data in the Serial Monitor

In the next post, we’ll take it online 🌐 — you’ll learn how to control your LED using a simple web server hosted on the ESP32 itself!
That means you’ll be able to turn your LED on and off right from your browser or phone — no button required. 💡📱

💬 Your Turn!
Did your LED respond to the button? 🕹️💡
Share your results in the comments — I’d love to see your setup and help troubleshoot if needed.

Top comments (0)