1 · What the code does
const int buttonPin = 4; // pushbutton on GPIO 4 (digital input) const int ledPin = 5; // LED on GPIO 5 (digital output) pinMode(buttonPin, INPUT); // read the button pinMode(ledPin, OUTPUT); // drive the LED buttonState = digitalRead(buttonPin); // HIGH or LOW if (buttonState == HIGH) digitalWrite(ledPin, HIGH); // pressed -> LED on else digitalWrite(ledPin, LOW); // released -> LED off
Key fact: the LED lights when the button pin reads HIGH. So the button is wired so that pressing it connects the pin to 3.3 V (HIGH), and an external 10 kΩ pull‑down resistor holds it at GND (LOW) when released.
pinMode(buttonPin, INPUT) enables no internal pull resistor,
the external 10 kΩ pull‑down is required: otherwise the input "floats" and reads random noise.
(See §6 for a software‑only alternative using the built‑in pull‑up.)2 · The pins: analog or digital?
| Signal | GPIO | Used as | Here it is | Analog capable? |
|---|---|---|---|---|
| Button input | GPIO 4 | digitalRead() |
DIGITAL input | YES: ADC2_CH0 & touch T0 (not used here) |
| LED output | GPIO 5 | digitalWrite() |
DIGITAL output | NO: GPIO 5 has no ADC (PWM only, for dimming) |
- Digital = only two states: HIGH (≈3.3 V) or LOW (0 V). Both pins are used this way.
- Analog = reading a range of voltage (0–3.3 V → a number 0–4095) with the ADC. Not used in this project. GPIO 4 could do it; GPIO 5 cannot.
- Logic level is 3.3 V. Use the board's
3V3pin (never5V/VIN) for the button's HIGH side. - GPIO 5 is a strapping pin (read at boot). Driving an LED through a resistor is fine. Prefer to keep the other strapping pins: GPIO 0, 2, 12, 15: free for sensitive inputs.
3 · Parts (all in the LAFVIN kit)
| Qty | Part | Notes |
|---|---|---|
| 1 | ESP32 DevKit board | The ESP‑32S module on the breadboard |
| 1 | Breadboard | Half or full size |
| 1 | LED | Any color: has polarity (see the diagram) |
| 1 | Resistor 220 Ω (LED) | 5‑band: Red Red Black Black Brown (±1%) |
| 1 | Resistor 10 kΩ (pull‑down) | 5‑band: Brown Black Black Red Brown (±1%) |
| 1 | Pushbutton | 4‑leg tactile button |
| 6+ | Jumper wires | Male‑to‑male |
Quick tell‑apart: the first band gives it away: Red starts the 220 Ω, Brown starts the 10 kΩ. Read with the tolerance band (Brown) on the right.
4 · Wiring diagrams
3.3 V wire · GND wire · signal (GPIO) wire
5 · Wiring: step by step
The ESP32 straddles the center gap of the breadboard. The long side rails are power: + (red) and − (blue).
A · Power rails
- ESP32
3V3→ breadboard + (red) rail. - ESP32
GND→ breadboard − (blue) rail.
B · LED circuit (output, GPIO 5)
- GPIO 5 → one end of the 220 Ω resistor.
- Other end of 220 Ω → LED anode (long leg, +).
- LED cathode (short leg, −) → − (GND) rail.
C · Button circuit (input, GPIO 4)
- Place the button across the center gap (its two sides on different rows).
- One side of the button → + (3.3 V) rail.
- Other side of the button → GPIO 4.
- From that same GPIO 4 node → 10 kΩ resistor → − (GND) rail.
6 · Upload & expected serial output
- Use a data USB cable. In Arduino IDE: Tools → Board → ESP32 Dev Module, pick the Port.
- Upload. If it hangs at "Connecting…", hold the BOOT button briefly.
- Open Serial Monitor at 115200 baud (matches
Serial.begin).
======================================== Project 1: ESP32 Inputs & Outputs ======================================== Button (input) -> GPIO 4 LED (output) -> GPIO 5 Press the button to turn the LED ON. Waiting for button presses... ---------------------------------------- [EVENT] Button PRESSED -> LED is ON [EVENT] Button RELEASED -> LED is OFF
7 · Optional: no external resistor (software pull‑up)
Prefer not to wire the 10 kΩ? Use the ESP32's built‑in pull‑up. This inverts the logic, so the code changes too. Wire the button between GPIO 4 and GND (remove the 3.3 V and the 10 kΩ):
pinMode(buttonPin, INPUT_PULLUP); // reads HIGH when NOT pressed if (buttonState == LOW) digitalWrite(ledPin, HIGH); // pressed -> LED on else digitalWrite(ledPin, LOW); // released -> LED off
INPUT_PULLUP + the flipped code above. Don't mix them.8 · Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| LED never lights | LED reversed | Flip it: long leg toward the 220 Ω / GPIO 5 side |
| LED never lights | Wrong resistor / broken row | Confirm 220 Ω in the LED path; check breadboard rows |
| LED always on / flickers | Floating input: missing pull‑down | Add the 10 kΩ from GPIO 4 to GND |
| Serial Monitor blank / garbage | Wrong baud | Set it to 115200 |
| Button seems inverted | INPUT_PULLUP with original code | Match code and wiring: see §7 |
| Nothing uploads | Charge‑only cable / wrong port | Use a data cable; select correct port; hold BOOT |
| LED very dim | Resistor too large in LED path | Use 220 Ω, not 10 kΩ, for the LED |