ESP32 Blink: Your First Sketch and the On-board LED
The first program for any board: blink the LED on GPIO2. We cover setup() and loop(), pinMode, digitalWrite, delay and the Serial Monitor — the foundation for every project that follows.
About the project
"Blink" is the "Hello, world!" of microcontrollers. The sketch turns an LED on and off once a second. It sounds trivial, but it shows how every Arduino program is structured, how the board drives its pins, and how to see what is happening inside through the Serial Monitor.
Most ESP32 DevKit boards already have a blue LED on GPIO2, so there is nothing to solder.
What you need
- ESP32 DevKit (DOIT DevKit V1, ESP32-DevKitC, etc.)
- A USB cable that carries data (not a charge-only one)
- Arduino IDE with the esp32 board package by Espressif
Wiring
No external parts: the LED and its resistor are already on the board between GPIO2 and GND.
If the LED on your board does not blink, it may sit on another pin or not exist at all. Then connect an external LED through a 220 Ω resistor, as in the breadboard article.
How it works
Every Arduino program has two mandatory functions:
setup()runs once after power-up or reset. Pins, the serial port and sensors are configured here.loop()is called again and again for as long as the board is powered.
Three functions you will need in almost every project:
pinMode(pin, OUTPUT)makes the pin an output. Without itdigitalWrite()switches nothing on.digitalWrite(pin, HIGH / LOW)puts 3.3 V or 0 V on the pin.delay(ms)waits for the given number of milliseconds.
Serial.begin(115200) opens a link to the computer. In the Serial Monitor (also at 115200) you will see the messages from Serial.println(). It is your main debugging tool.
Code
// Blink: the on-board LED is connected to GPIO2
const int LED_PIN = 2;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
Serial.println("Blink started");
}
void loop() {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED on");
delay(500);
digitalWrite(LED_PIN, LOW);
Serial.println("LED off");
delay(500);
}
Common mistakes
- The upload fails ("Failed to connect"). Hold the BOOT button on the board until the IDE shows "Connecting…", then release it.
- Garbage in the Serial Monitor. The monitor speed does not match
Serial.begin(). Set it to 115200. pinMode()is missing. The pin stays an input and the LED glows faintly or not at all. The simulator warns about this.
What next
- Change the delays so the LED gives a short flash every two seconds, like a heartbeat indicator.
- Replace
delay()with amillis()check, soloop()can do other work while the LED waits. The debounced button project uses this technique.