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
- ESP32 development board (e.g., ESP32 DevKit C)
- 1 ร LED (any color)
- 1 ร 220 ฮฉ resistor (for the LED)
- 1 ร Push button
- Breadboard
- Jumper wires
- 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
}
๐ง 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
- - Connect your ESP32 to your computer.
- - Select the correct Board and Port.
- - Click Upload.
- - 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
- 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)