PROJECT 1 · DIGITAL I/O

Inputs & Outputs — Button → LED

Press a pushbutton → an LED turns on; release it → the LED turns off. The "hello world" of physical computing.

New here? Start with Project 0 — Introduction & Setup.

⬇ Download the sketch Save Project_1_ESP32_Inputs_Outputs.ino, then open it in the Arduino IDE (setup: Project 0b).
Pushbutton pressed turns LED on; not pressed turns LED off
The whole idea: button pressed → LED on · button released → LED off.

1 · What the sketch does

pinMode(buttonPin, INPUT);   // GPIO 4 — read the button
pinMode(ledPin,   OUTPUT);  // GPIO 5 — drive the LED

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) digitalWrite(ledPin, HIGH); // pressed  -> LED on
else                     digitalWrite(ledPin, LOW);  // released -> LED off

The LED lights when the pin reads HIGH, so the button sends 3.3 V to GPIO 4 when pressed, and a 10 kΩ pull‑down holds the pin LOW when released.

Plain INPUT has no internal pull resistor, so the external 10 kΩ pull‑down is required — otherwise the pin "floats" and reads random noise. (§7 shows a software‑only alternative.)

2 · The pins — analog or digital?

SignalGPIOUsed asHere it isAnalog capable?
ButtonGPIO 4digitalRead()DIGITAL inputYes — ADC2_CH0 + touch T0 (unused)
LEDGPIO 5digitalWrite()DIGITAL outputNo — no ADC (PWM only)

Both pins are used digitally (HIGH/LOW). Use 3V3 (never 5V) for the button's HIGH side.

3 · Parts

QtyPartNotes
1ESP32 DEVKIT V1 · breadboard · jumper wires
1LEDAny color — has polarity (§5)
1220 Ω resistorLED limit · Red‑Red‑Black‑Black‑Brown
110 kΩ resistorPull‑down · Brown‑Black‑Black‑Red‑Brown
1Pushbutton4‑leg tactile

4 · Wiring

Breadboard schematic: LED on GPIO 5, button on GPIO 4 with 10 kilo-ohm pull-down
Official kit schematic — LED on GPIO 5 (220 Ω), button on GPIO 4 (10 kΩ pull‑down to GND, 3.3 V on the other side).

LED circuit (output, GPIO 5)

  1. GPIO 5 → 220 Ω → LED anode (long leg, +)
  2. LED cathode (short leg, −)GND rail

Button circuit (input, GPIO 4)

  1. One side of the button → 3.3 V rail
  2. Other side → GPIO 4
  3. From that same GPIO 4 node → 10 kΩGND rail
Behavior: released → pulled to GND through 10 kΩ → LOW → LED off. Pressed → connected to 3.3 V → HIGH → LED on.

5 · LED polarity (don't skip)

LONG leg  = Anode  (+)  -> toward the 220 Ω / GPIO 5 side
SHORT leg = Cathode (-)  -> toward GND   (flat notch on the rim = - side)

If the LED never lights, check first that it isn't reversed.

6 · Code & expected serial output

The sketch Project_1_ESP32_Inputs_Outputs.ino prints event‑based logs (only when the state changes, so the monitor stays readable). Open Serial Monitor at 115200:

========================================
 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

A 50 ms debounce delay stops one press from being logged many times.

7 · Optional — no external resistor (software pull‑up)

Wire the button between GPIO 4 and GND and enable the internal pull‑up. This inverts the logic:

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, or INPUT_PULLUP + flipped code. Don't mix them.

8 · Demonstration

LED lit while the button is pressed
Press → LED lights and the monitor prints a PRESSED event. Release → off.

9 · Troubleshooting

SymptomLikely causeFix
LED never lightsLED reversedFlip it — long leg toward the 220 Ω side
LED always on / flickersFloating input (missing pull‑down)Add the 10 kΩ from GPIO 4 to GND
Monitor blank / garbageWrong baudSet it to 115200
Button seems invertedINPUT_PULLUP with original codeMatch code and wiring (§7)
Nothing uploadsCharge‑only cable / wrong portData cable; correct port; hold BOOT
LED very dim10 kΩ in the LED pathUse 220 Ω for the LED
← Project 0 · Introduction & Setup Project 2 · Analog Inputs →