Understanding the Concept
The Blinking LED project is the fundamental starting point for any embedded systems journey. In this module, you will learn how a microcontroller like the ESP32 can control external hardware using its General Purpose Input/Output (GPIO) pins.
By the end of this module, you will understand how digital output signals work, how to write your first Arduino sketch, and how to physically connect an LED to a breadboard circuit.
What is GPIO?
GPIO stands for General Purpose Input/Output. These are programmable digital pins on the ESP32 that can be configured as either inputs (reading sensor data) or outputs (controlling devices like LEDs, motors, and relays).
In this project, we configure GPIO 2 as a digital output and toggle it between HIGH (3.3V) and LOW (0V) with a 1-second delay — causing the LED to blink on and off in a repeating cycle.
Why Start with an LED?
Blinking an LED teaches you the three fundamental concepts used in every IoT project:
- Pin configuration — telling the microcontroller how to use a pin
- Digital write — sending voltage to control external components
- Timing — controlling how long actions last using delays
Once you understand these concepts, you can control motors, relays, speakers, displays, and any other output device.
How It Works — Step by Step
Watch the execution flow animate step-by-step inside the ESP32.
Interactive 3D Hardware
Rotate: drag · Zoom: scroll · Click any component for details.
What You'll Need
Hardware Connections
| # | Component / Pin | Connects To | Wire | Notes |
|---|---|---|---|---|
| 1 | ESP32 GPIO 2 | 220Ω Resistor (Leg 1) | Use a red jumper wire | |
| 2 | 220Ω Resistor (Leg 2) | LED Anode (+) | Connect directly on breadboard | |
| 3 | LED Cathode (−) | ESP32 GND | Use a black jumper wire for GND | |
| 4 | ESP32 3V3 (optional) | Breadboard + Rail | Only if using breadboard power rail | |
| 5 | ESP32 GND (optional) | Breadboard − Rail | Connect GND rail for cleaner wiring |
Arduino / ESP32 Code
// ══════════════════════════════════════════════
// Mindrevel IoT Workshop — Session 1
// Project : Blinking LED with ESP32
// Board : ESP32 Dev Module
// GPIO Pin : 2 (Built-in blue LED on most boards)
// Author : Mindrevel IoT Team
// ══════════════════════════════════════════════
const int LED_PIN = 2; // GPIO pin number
const int BLINK_RATE = 1000; // Blink interval in ms
// ── setup() ── Runs ONCE on power-on ──────────
void setup() {
Serial.begin(115200); // Start serial monitor
pinMode(LED_PIN, OUTPUT); // Set GPIO 2 as output
Serial.println("Mindrevel IoT — Blinking LED Ready!");
}
// ── loop() ── Runs CONTINUOUSLY ───────────────
void loop() {
// Turn LED ON
digitalWrite(LED_PIN, HIGH);
Serial.println("[ON] LED is ON — GPIO 2 = 3.3V");
delay(BLINK_RATE);
// Turn LED OFF
digitalWrite(LED_PIN, LOW);
Serial.println("[OFF] LED is OFF — GPIO 2 = 0V");
delay(BLINK_RATE);
}
const int LED_PIN = 2;
Declares a constant variable storing the GPIO pin number. Using a named constant (instead of the number 2 directly) makes the code easier to read and modify. Change this number to use a different pin.
const int BLINK_RATE = 1000;
Stores the blink delay in milliseconds. 1000ms = 1 second. Change to 500 for faster blinking, or 2000 for slower. Using a constant makes it easy to adjust the speed without searching through the code.
void setup()
This function runs exactly ONCE — immediately after the ESP32 powers on or resets. Use it for initialization: setting pin modes, starting serial communication, connecting to WiFi, etc.
Serial.begin(115200);
Initializes serial communication at 115200 bits per second (baud rate). This allows the ESP32 to send text messages to the Arduino IDE Serial Monitor for debugging. Open Serial Monitor with Ctrl+Shift+M.
pinMode(LED_PIN, OUTPUT);
Configures GPIO 2 as a DIGITAL OUTPUT pin. After this line, the ESP32 can drive the pin to HIGH (3.3V) or LOW (0V). Without this, the pin behavior is undefined and the LED may not work correctly.
void loop()
This function runs CONTINUOUSLY in an infinite loop after setup() completes. All your main program logic goes here. The ESP32 repeats this function thousands of times per second (limited by your delays).
digitalWrite(LED_PIN, HIGH);
Applies 3.3V to GPIO 2. This creates a voltage difference across the circuit: 3.3V at the GPIO pin vs 0V at GND. The resulting current (~6mA through the 220Ω resistor) flows through the LED, turning it ON.
delay(BLINK_RATE);
Pauses the entire program for 1000 milliseconds. During this pause, the LED stays in its current state (ON or OFF). This is a blocking delay — the ESP32 cannot do anything else while waiting.
digitalWrite(LED_PIN, LOW);
Sets GPIO 2 to 0V. Both ends of the LED circuit are now at the same potential (0V), so no current flows. The LED turns OFF. The next delay keeps it off for 1 second before the cycle repeats.