🌟 Fundamentals of ESP32 Programming – Practical Tutorial for Running Lights
Today, we will start exploring the ESP32 from scratch! As a classic introductory project in embedded development, running lights can help you quickly grasp microcontroller programming logic and hardware control. Even complete beginners can easily get started, so prepare your development board and LED lights, and follow the tutorial to embark on your ESP32 journey!

📖 1. Learning Objectives | You Will Master These
✅ Hardware Basics: Learn ESP32 pin configuration and LED circuit setup ✅ Programming Logic: Understand loop structures, delay functions, and digital IO control ✅ Debugging Skills: Master the Arduino IDE programming process and troubleshooting ✅ Creative Extensions: Lay the foundation for more complex projects (like breathing lights and running lights)
🛠️ 2. Hardware List | Essential Low-Cost Starter Kit
|
Material Name |
Quantity |
Function |
|
ESP32 Development Board |
1 piece |
Core controller |
|
5mm LED Lights |
8 pieces |
Light-emitting components |
|
220Ω Resistors |
8 pieces |
Current limiting protection (1 for each LED) |
|
Breadboard + Dupont Wires |
1 set |
Circuit building platform |
💡 Beginner Tip: Simply search for “ESP32 starter kit” to purchase all materials at once
🔌 3. Hardware Connections | Set Up the Circuit in 5 Minutes (with Wiring Diagram)
📸 Wiring Steps
LED Anode → 220Ω Resistor → ESP32 Pins D2-D9 (corresponding to LEDs 1-8 in order)LED Cathode → Connect to the GND pin of the ESP32Power Supply for Development Board: Connect via USB cable to a computer or 5V power adapter
💡 Error Prevention Guide: The longer leg of the LED is the anode, the shorter leg/cut leg is the cathode, and the resistor has no polarity and can be connected in any direction
💻 4. Software Environment Setup | Step-by-Step Guide
1. Install Arduino IDE
👉 Direct link to the official website ✅ Select the corresponding system version (Windows/macOS/Linux), and check “Add desktop shortcut” during installation
2. Activate ESP32 Development Support
🔧 Complete in 3 steps: ① Open IDE → “File” → “Preferences” → Paste in “Additional Board Manager URLs”: https://dl.espressif.com/dl/package_esp32_index.json ② “Tools” → “Board” → “Board Manager” → Search for “ESP32” → Click Install ③ Wait for 1 minute, and after completion, find “ESP32 DevKitC” in the board list
3. Connect the Development Board
Connect the ESP32 using a USB cable, open Device Manager to confirm the COM port (e.g., COM3), and select this port for subsequent programming
📝 5. Core Code Writing | Detailed Explanation with Comments
// ESP32 Running Light Program (3 effects: left-right flow + all blink)
const int ledPins[] = {2, 4, 5, 12, 13, 14, 15, 27}; // Define 8 LED pins
const int ledCount = sizeof(ledPins) / sizeof(int); // Automatically calculate the number of LEDs
const int delayTime = 300; // Light change speed (milliseconds)
void setup() {
for (int i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT); // Initialize all pins as output mode
}
}
void loop() {
// Effect 1: Flow from left to right
for (int i = 0; i < ledCount; i++) {
digitalWrite(ledPins[i], HIGH); // Light up the current LED
delay(delayTime); // Wait for the specified time
digitalWrite(ledPins[i], LOW); // Turn off the current LED
}
// Effect 2: Flow from right to left
for (int i = ledCount - 1; i >= 0; i--) {
digitalWrite(ledPins[i], HIGH);
delay(delayTime);
digitalWrite(ledPins[i], LOW);
}
// Effect 3: All LEDs blink synchronously 3 times
for (int i = 0; i < 3; i++) {
for (int j = 0; j < ledCount; j++) {
digitalWrite(ledPins[j], HIGH); // Light up all LEDs
}
delay(delayTime);
for (int j = 0; j < ledCount; j++) {
digitalWrite(ledPins[j], LOW); // Turn off all LEDs
}
delay(delayTime);
}
}
💡 Key Points of the Code
Array Batch Control: Use ledPins[] to store all pins, and operate uniformly through loops to avoid duplicate codeAutomatic Length Calculation: sizeof(ledPins)/sizeof(int) adapts to any number of LEDs, allowing for flexible expansionThree Effects in One: Implement different effects through three independent loops, with clear and easy-to-modify logicDelay Principle: The larger the value of delayTime, the slower the speed; it is recommended to start debugging from 200ms
🚀 6. Programming and Running | Complete Debugging in 3 Steps
1. Configure IDE Parameters
⚙️ Settings in the “Tools” menu:
Board: Select “ESP32 DevKitC”Port: Select the COM port displayed in Device Manager (e.g., COM3)Other defaults: Crystal frequency 40MHz, Flash memory 4MB
2. Upload Code
🔄 Click the “Upload” button in the upper left corner, wait for the progress bar to complete (about 10 seconds) ✅ After successful upload, the serial monitor will display initialization information, and the development board will automatically reset and run
3. Physical Testing
After powering on, the LED lights will cycle through:
① 🌞 Flow from left to right: A single LED lights up sequentially like a wave
② 🌛 Flow from right to left: Reverse repeating flow effect
③ 💥 All blink: 8 LEDs blink synchronously 3 times (it is recommended to observe the effect in a dark environment for better visibility!)
❓ 7. Frequently Asked Questions | Beginner’s Pitfall Guide
Q1: What if the LED does not light up at all?
🔧 3-Step Troubleshooting:
① Check the wiring: Use a multimeter to measure the voltage across the LED, which should be around 3.3V (if there is no voltage, the circuit is open)
② Test individually: Add digitalWrite(2, HIGH); in setup() to light up only the first LED and confirm if it works
③ Code error: Check if the pin numbers exceed the available range for the ESP32 (common digital pins: 2-23, 25-27, 32-39)
Q2: The light flow order is chaotic?
💡 Solution: ✓ Check the order of the ledPins[] array to ensure it matches the physical wiring order (from left to right corresponding to D2-D9) ✓ Add serial printing in the code: Serial.println(ledPins[i]); to confirm the correct output of pin numbers during the loop
Q3: The upload process gets stuck at “Connecting…”?
🔍 Ultimate Solution: ① Manual Reset Method: After clicking upload, when the IDE shows “Connecting…”, quickly press the Reset button on the development board ② Change Port: Check Device Manager to confirm that the ESP32 COM port is not occupied by other software (like Bluetooth drivers) ③ Disable Antivirus Software: Some security software may block serial communication, temporarily disable it during programming
🌟 8. Extended Play | From Beginner to Advanced
1. Effect Upgrade – Get Creative
✨ Breathing Light Effect:
// Add PWM dimming (must use pins that support AnalogWrite, such as D2-D19)
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPins[0], brightness);
delay(10);
}
🎨 Rainbow Flow: Replace with RGB tri-color LEDs, achieving color gradients through different pin combinations (requires 3 independent control pins)
2. Hardware Expansion – Create a Matrix
🔌 8×8 LED Matrix: Use with 74HC595 shift register to control 64 LEDs with only 3 ESP32 pins, enabling text/pattern display (future tutorials will elaborate, stay tuned for updates!)
3. Creative Applications – Scenario-Based Development
🏠 Smart Home Indicator Light: Connect a temperature and humidity sensor, with green indicating a comfortable environment and red blinking indicating an anomaly
🎄 Holiday Decorative Lights: Customize Christmas animations (like flowing snowflakes) and pair with a buzzer to play festive music
💌 Conclusion
Congratulations on completing your first ESP32 programming project! Although the running lights are simple, they encompass the core logic of embedded development – hardware control + software logic + debugging skills. Every circuit connection and code debugging is a valuable experience accumulation~
👉 Give it a try! If you encounter any issues, feel free to leave a comment, and I will answer them one by one~ What project would you like to learn next? Temperature and humidity sensor? Bluetooth control? Let me know in the comments!