Your second ESP32 project starts where the first one ended. In the first project you made an LED blink. Read it here: https://techknowlogy.net/your-first-esp32-project-blinking-an-led-complete-tutorial/ Now you will add a push button, so the board can listen as well as act. Press the button, and the LED turns on. Release it, and the LED turns off. Then you will change the code so each press toggles the LED, like a light switch. That teaches you input, output, and debouncing, the ideas behind almost every ESP32 project that follows.
TL;DR: second ESP32 project in one minute
Wire a push button between GPIO 13 and GND, and use the onboard LED on GPIO 2, or an external LED with a 220 ohm resistor. Set the button pin to INPUT_PULLUP, so it reads HIGH when released and LOW when pressed. Read it with digitalRead, turn the LED on while pressed, then add a 50 millisecond debounce check, so one press counts as one press.
What you need for this second ESP32 project
You need the same kit as the first project, plus one push button: an ESP32 board, a USB data cable, a breadboard and jumper wires, a computer with Arduino IDE and ESP32 support installed, and one LED with a 220 ohm resistor if you want an external LED. If you used the onboard LED before, use it again here. If Arduino IDE is not set up yet, set it up first. Read our guide here: https://techknowlogy.net/arduino-ide-setup-for-esp32-2026/ If you need help picking a board, read our guide here: https://techknowlogy.net/best-esp32-board-2026/

GPIO basics, in plain language
GPIO stands for General Purpose Input Output. A GPIO pin is a connection your code can control. In output mode, it can turn something on or off, like an LED. In input mode, it can read something, like a button. The Espressif GPIO docs put it in short form: GPIO controls or reads the state of a pin, and it is widely used to blink an LED or to read a button.
Three facts matter. First, ESP32 GPIO pins use 3.3 volt logic, not 5 volt logic, so do not feed 5 volts into a pin. Second, a pin that is not connected to anything floats, and its reading drifts at random. A pull up resistor fixes that by holding the pin HIGH until the button pulls it LOW. Third, on a classic ESP32, GPIO 34 to GPIO 39 are input only, with no internal pull up, so do not use them here. For this second ESP32 project, use GPIO 13 for the button and GPIO 2 for the LED. GPIO 13 has an internal pull up resistor. GPIO 2 is the onboard LED pin on most classic boards. Check your board silkscreen first, because ESP32 S3 and ESP32 C3 boards use different pins.
Wiring a button to an ESP32
Power off the board first. Connect one side of the push button to GPIO 13, and the other side to any GND pin. That is the whole button circuit for this second ESP32 project. There is no external resistor, because the code will turn on the internal pull up resistor. If you want an external LED, connect its long leg to GPIO 2 through a 220 ohm resistor, and its short leg to GND. If you use the onboard LED, skip this step. The logic is upside down from what most beginners expect. With INPUT_PULLUP, the pin reads HIGH when released, and LOW when pressed. So your code will look for LOW, not HIGH.
The code, explained part by part
Open Arduino IDE, create a new sketch, and type the code below. Each part is explained after it, so you can change it with confidence.
const int BUTTON_PIN = 13;
const int LED_PIN = 2;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}

First, the two constants name the pins, BUTTON_PIN is 13, and LED_PIN is 2, so you only change one line if you move a wire later. Second, setup runs once when the board starts. pinMode(LED_PIN, OUTPUT) sets GPIO 2 to drive an LED. pinMode(BUTTON_PIN, INPUT_PULLUP) sets GPIO 13 to read a button, with the internal pull up resistor turned on. That resistor is about 45k ohm, according to the Espressif GPIO docs, and it is strong enough for a button on a short wire.
Third, loop runs again and again. digitalRead(BUTTON_PIN) reads the button pin. If the reading is LOW, the button is pressed, so digitalWrite(LED_PIN, HIGH) turns the LED on. If not, digitalWrite(LED_PIN, LOW) turns it off. Upload the sketch for this second ESP32 project. If it fails, do not change the code first. Check the USB cable, board, and port. Read our guide here: https://techknowlogy.net/esp32-upload-error-fix/
What the button does, and what debouncing means
Test the first version now. Hold the button down, and the LED should stay on. Release it, and it should turn off. That is a momentary switch, and it already works as a doorbell or a trigger for a sensor reading. Now try the toggle version. Each press flips the LED on or off, like a light switch.
A mechanical button does not make one clean contact when you press it. The contacts bounce several times in a few milliseconds, and the ESP32 is fast enough to see each bounce as a separate press, so one press can toggle the LED two or three times. Debouncing means waiting until the button reading stays steady before you count it.
A common starting value is 50 milliseconds, long enough for most small buttons to settle, and short enough that you will not notice it. Use millis, which counts the milliseconds since the board started, to note the time of the last change, and only count the press once the reading has stayed the same for 50 milliseconds. That way the board keeps running while it waits, instead of freezing with delay. Start with the hold to light version, then add the debounce check as step two.
Common mistakes in a second ESP32 project
Wiring the button to a pin with no internal pull up, usually GPIO 34 to GPIO 39, makes the reading flicker. Move it to GPIO 13. Reading the button backwards is next. With INPUT_PULLUP, pressed is LOW. If your LED turns on when you release the button, test for LOW instead of HIGH.
Using a charge only USB cable is another. It powers the board, but it carries no data, so the upload fails. Use a USB data cable. Forgetting the resistor on an external LED is another. A bare LED from GPIO 2 to GND can draw too much current. Use a 220 ohm resistor in series, on the long leg side. Picking the wrong board or port in Arduino IDE is the last one. For a classic board, use ESP32 Dev Module. If the port list is empty, the cable or the driver is the problem, not the sketch.
3 simple variations to try next
First, make the button toggle the LED. Each press flips it on or off, with the 50 millisecond debounce check above, so one press always means one flip. Second, make the button count presses. Add a counter, increase it by one on each debounced press, and print it with Serial.println at 115200 baud. Third, add a second button to this second ESP32 project on GPIO 14, also wired to GND with INPUT_PULLUP. Make one button turn the LED on, and the other turn it off.
FAQs about your second ESP32 project
Do I need an external pull up resistor for the button
No, not for this project. The ESP32 has an internal pull up resistor of about 45k ohm, and pinMode with INPUT_PULLUP turns it on. Use an external 10k ohm resistor only if you use an input only pin like GPIO 34 to GPIO 39, which has no internal pull up.
Why does the LED turn on when I release the button
Your code is testing for the wrong level. With INPUT_PULLUP, released is HIGH and pressed is LOW. Test for LOW, and the LED will turn on while you hold the button.
What is debouncing, in one sentence
Debouncing is waiting until a mechanical button stops bouncing, usually about 50 milliseconds, before you count it, so one physical press counts as one press.
Can I use a different GPIO pin for the button
Yes, if the pin supports an internal pull up. GPIO 13 is safe on a classic ESP32. Avoid GPIO 34 to GPIO 39, with no internal pull up, and GPIO 0, GPIO 2, GPIO 5, GPIO 12, and GPIO 15, because they affect how the board boots. Check your board pinout first.
What should I build after this project
Add a sensor next. A DHT11 or DHT22 temperature sensor uses the same idea, a pin reads a value, and the code acts on it.
The bottom line on your second ESP32 project
Your second ESP32 project is short, but it changes what you can build. The first project made the board act. This one makes it listen. A button is an input, an LED is an output, and debouncing makes the two work together. Once that works, you can add a second button, a counter, or a sensor.
