Project 1: ESP32 Inputs & Outputs

Board: LAFVIN Basic Starter Kit for ESP32 (ESP‑32S / ESP‑WROOM‑32 DevKit)

Goal: press the pushbutton → the LED turns on; release it → the LED turns off.

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.

Because 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?

SignalGPIOUsed asHere it isAnalog capable?
Button inputGPIO 4digitalRead() DIGITAL input YES: ADC2_CH0 & touch T0 (not used here)
LED outputGPIO 5digitalWrite() 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 3V3 pin (never 5V/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)

QtyPartNotes
1ESP32 DevKit boardThe ESP‑32S module on the breadboard
1BreadboardHalf or full size
1LEDAny color: has polarity (see the diagram)
1Resistor 220 Ω (LED) 5‑band: Red Red Black Black Brown (±1%)
1Resistor 10 kΩ (pull‑down) 5‑band: Brown Black Black Red Brown (±1%)
1Pushbutton4‑leg tactile button
6+Jumper wiresMale‑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

GPIO 5 digital output 220 Ω ±1% LED anode (+) long leg cathode (−) short/flat GND
LED circuit (output): GPIO 5 → 220 Ω → LED → GND.
3.3 V Push button GPIO 4 digital input 10 kΩ pull‑down ±1% GND
Button circuit (input): 3.3 V → button → GPIO 4 & 10 kΩ pull‑down → GND.

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

  1. ESP32 3V3 → breadboard + (red) rail.
  2. ESP32 GND → breadboard (blue) rail.

B · LED circuit (output, GPIO 5)

  1. GPIO 5 → one end of the 220 Ω resistor.
  2. Other end of 220 Ω → LED anode (long leg, +).
  3. LED cathode (short leg, −) (GND) rail.

C · Button circuit (input, GPIO 4)

  1. Place the button across the center gap (its two sides on different rows).
  2. One side of the button → + (3.3 V) rail.
  3. Other side of the button → GPIO 4.
  4. From that same GPIO 4 node → 10 kΩ resistor → (GND) rail.
Behavior: released → GPIO 4 pulled to GND through 10 kΩ → reads LOW → LED off. Pressed → GPIO 4 connects to 3.3 V → reads HIGH → LED on.

6 · Upload & expected serial output

  1. Use a data USB cable. In Arduino IDE: Tools → Board → ESP32 Dev Module, pick the Port.
  2. Upload. If it hangs at "Connecting…", hold the BOOT button briefly.
  3. 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
Pick one approach: 10 kΩ pull‑down + original code (§5), or INPUT_PULLUP + the flipped code above. Don't mix them.

8 · Troubleshooting

SymptomLikely causeFix
LED never lightsLED reversedFlip it: long leg toward the 220 Ω / GPIO 5 side
LED never lightsWrong resistor / broken rowConfirm 220 Ω in the LED path; check breadboard rows
LED always on / flickersFloating input: missing pull‑downAdd the 10 kΩ from GPIO 4 to GND
Serial Monitor blank / garbageWrong baudSet it to 115200
Button seems invertedINPUT_PULLUP with original codeMatch code and wiring: see §7
Nothing uploadsCharge‑only cable / wrong portUse a data cable; select correct port; hold BOOT
LED very dimResistor too large in LED pathUse 220 Ω, not 10 kΩ, for the LED