GPIO Basics: Blinking an LED

Blinking an LED is the "hello world" of embedded electronics, and it is the fastest way to prove your Arduino IDE setup, board selection, and upload process are all working correctly before moving on to anything more complex.

This page's code and wiring instructions are a preview, not something you can run right on this website - this server runs PHP, not the ESP32's own firmware. Upload the code below to your own ESP32 board to actually see it run.

Option 1: The Onboard LED (No Wiring Needed)

Many popular ESP32 DevKitC-style development boards - the common blue-silkscreen boards with USB and 30 or 38 pins that most beginners buy under a generic "ESP32 dev board" listing - have a small LED already built onto the board and wired to GPIO2. If your board has one, you can blink it with zero extra wiring. This is common on that popular board style, but it is not a universal guarantee across every ESP32 board - check your own board's silkscreen or documentation to confirm whether it has a built-in LED and which GPIO it is actually wired to before assuming GPIO2 will light anything up.

Option 2: An External LED (More Universal)

Wiring your own external LED works on any ESP32 board regardless of whether it happens to have a built-in LED, so it is the more reliable example to learn from. Use a safe general-purpose GPIO such as GPIO4 (not one of the input-only, flash-reserved, or strapping pins covered in the GPIO Pinout & Restrictions lesson). You will need:

  • One LED
  • One current-limiting resistor, commonly in the 220-330 ohm range
  • Jumper wires and a breadboard

The resistor matters: an LED connected directly across a GPIO pin with no resistor will try to draw far more current than the pin is designed to supply, which can damage the LED and potentially the GPIO pin itself. Wire the GPIO pin to the resistor, the resistor to the LED's positive (anode) leg, and the LED's negative (cathode) leg to ground (GND).

3.3V only: the ESP32's GPIO pins are 3.3V logic and are not 5V tolerant. Do not power this circuit from a 5V source wired into the GPIO pin itself - drive the LED directly from the 3.3V-logic GPIO pin through the resistor as described above.

The Code

This sketch works for either option - just change the pin number to match GPIO2 (onboard LED, if present) or GPIO4 (external LED):

const int LED_PIN = 4;  // change to 2 if using a confirmed onboard LED

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(1000);
  digitalWrite(LED_PIN, LOW);
  delay(1000);
}

Line by line:

  • const int LED_PIN = 4; - stores the GPIO number in one place so you only need to change it once.
  • pinMode(LED_PIN, OUTPUT); - runs once at startup, telling the chip this pin will be driven as an output rather than read as an input.
  • digitalWrite(LED_PIN, HIGH); - sets the pin to 3.3V, turning the LED on.
  • delay(1000); - pauses for 1000 milliseconds (one second).
  • digitalWrite(LED_PIN, LOW); - sets the pin to 0V (ground), turning the LED off.
  • The loop() function repeats forever, so the LED keeps blinking on a one-second on/off cycle.
Recap: Many popular dev boards have a built-in LED on GPIO2 (verify on your own board first); an external LED with a 220-330 ohm resistor on a safe pin like GPIO4 is the more universal test. Either way, the code is the same three-function pattern: pinMode once in setup(), then digitalWrite and delay repeated in loop().
Next steps: Continue to Connecting to Wi-Fi to get your ESP32 talking to a network.