ESP32 BASICS · LESSON 04
Your First Sketch: Blinking an LED
The classic first program, explained line by line, plus what actually happens in the chip while it runs.
Every dev board has at least one LED wired to a GPIO pin, usually labelled LED_BUILTIN in code. Blinking it is the smallest possible program that proves your toolchain, board, and upload all work together.
blink.ino
#define LED_PIN 2
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
What each line does
pinMode(LED_PIN, OUTPUT)tells the chip's GPIO controller to drive that pin's voltage rather than read it.digitalWrite(..., HIGH)sets the pin to roughly 3.3 V;LOWsets it to 0 V.delay(500)pauses the whole program for 500 milliseconds — fine here, but something to avoid once a sketch needs to do several things at once.
Under the hood
Behind digitalWrite, the core sets a bit in a GPIO register that the chip's hardware reads continuously to decide the pin's output voltage. The Arduino functions exist to hide that register-level detail, but knowing it's there makes later, faster techniques easier to understand.
KEY IDEA
If pin 2 doesn't do anything visible on your board, check its silkscreen — not every board wires the onboard LED to the same GPIO.