Control RGB LEDs with ESP32
Wokwi Simulation Guide
Learn GPIO control, RGB color mixing, and embedded programming fundamentals using the XIAO ESP32-S3 — all without any hardware.
Open Free SimulationWhat You'll Build
Three RGB LEDs (common cathode) connected to an XIAO ESP32-S3, cycling through red, green, and blue using digital GPIO pins. A perfect first project for understanding ESP32 output control.
What You Need (Virtual)
-
×1XIAO ESP32-S3Main microcontroller board
-
×3RGB LED – Common Cathode4-pin (R, G, B, GND)
-
×9220Ω ResistorsOne per channel (R, G, B)
-
×manyJumper WiresFor all connections
Understanding Common Cathode RGB LEDs
An RGB LED packs three separate LEDs (Red, Green, Blue) in one package sharing a common GND pin. Setting a GPIO pin HIGH turns that color ON — mix them for new colors.
| Red | Green | Blue | Result |
|---|---|---|---|
| 1 | 0 | 0 | Red |
| 0 | 1 | 0 | Green |
| 0 | 0 | 1 | Blue |
| 1 | 1 | 0 | Yellow |
| 1 | 0 | 1 | Magenta |
| 0 | 1 | 1 | Cyan |
| 1 | 1 | 1 | White |
Step-by-Step Wokwi Setup
Create a New Project
Go to wokwi.com → Click Start New Project → Select ESP32 board → Choose XIAO ESP32-S3.
Add Components
In the diagram editor, add 3 RGB LEDs and 9 resistors (220Ω). Arrange them neatly for easy wiring.
Paste the diagram.json
Click the diagram.json tab in Wokwi and replace its contents with the JSON provided below. This auto-wires all connections.
Upload the Arduino Code
Open the sketch.ino tab, paste the code below, and click ▶ Play to start the simulation.
Watch It Run
LEDs will cycle Red → Green → Blue every 500ms. Check the Serial Monitor for color name logs.
GPIO Pin Assignment
Each LED uses 3 GPIO pins (one per color channel). All cathodes connect to GND through a resistor.
diagram.json
Copy this into the diagram.json tab in your Wokwi project to instantly wire up the circuit.
{
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "board-xiao-esp32-s3", "id": "esp", "top": 38.97, "left": 13.78, "attrs": {} },
{ "type": "wokwi-led", "id": "led1", "top": 6, "left": -73, "attrs": { "color": "red" } },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 147.95,
"left": -57.6,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 15.6,
"left": -101.8,
"attrs": { "color": "green" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 167.15,
"left": -57.6,
"attrs": { "value": "1000" }
},
{ "type": "wokwi-junction", "id": "j1", "top": 148.8, "left": 24, "attrs": {} },
{
"type": "wokwi-led",
"id": "led3",
"top": 25.2,
"left": -130.6,
"attrs": { "color": "blue" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 186.35,
"left": -57.6,
"attrs": { "value": "1000" }
}
],
"connections": [
[ "esp:D2", "led1:A", "green", [ "h0" ] ],
[ "led1:C", "r1:1", "black", [ "v0" ] ],
[ "esp:D3", "led2:A", "green", [ "h0" ] ],
[ "led2:C", "r2:1", "black", [ "v0" ] ],
[ "r2:2", "j1:J", "black", [ "v0", "h27.6" ] ],
[ "j1:J", "r1:2", "black", [ "v0" ] ],
[ "esp:GND", "j1:J", "black", [ "h19.82", "v86.4" ] ],
[ "led3:A", "esp:D4", "green", [ "v0" ] ],
[ "led3:C", "r3:1", "black", [ "v0" ] ],
[ "j1:J", "r3:2", "black", [ "v0" ] ]
],
"dependencies": {}
}
sketch.ino — Full Source Code
This code initializes GPIO pins as outputs and cycles LEDs through Red → Green → Blue with 500ms delays, repeating indefinitely.
// ESP32 RGB LED Control — MakeMindz.com // Controls 3 LEDs on D2, D3, D4 with 500ms color cycling void setup() { Serial.begin(115200); pinMode(D2, OUTPUT); pinMode(D3, OUTPUT); pinMode(D4, OUTPUT); Serial.println(""); Serial.println("Hello, XIAO ESP32-S3!"); Serial.println("Welcome to Wokwi :-)"); } void loop() { // Red LED ON Serial.println("Red"); digitalWrite(D2, HIGH); delay(500); digitalWrite(D2, LOW); // Green LED ON Serial.println("Green"); digitalWrite(D3, HIGH); delay(500); digitalWrite(D3, LOW); // Blue LED ON Serial.println("Blue"); digitalWrite(D4, HIGH); delay(500); digitalWrite(D4, LOW); }
What This Code Does
-
1.
Initializes D2, D3, D4 as OUTPUT pins in
setup() - 2. Sets D2 HIGH (Red ON) → waits 500ms → sets LOW (Red OFF)
- 3. Sets D3 HIGH (Green ON) → waits 500ms → sets LOW
- 4. Sets D4 HIGH (Blue ON) → waits 500ms → sets LOW
- 5. Loops forever — LEDs cycle continuously through primary colors
Try It Instantly in Wokwi
No hardware needed. Click below to open the pre-wired simulation and run it in your browser.
Open Free Simulation on WokwiHow to Improve This Project
PWM Brightness
Use ledcWrite() for smooth 0–255 brightness control per channel.
Fade Transitions
Gradually increment PWM values to create smooth cross-fades between colors.
Color Mixing
Turn on multiple channels simultaneously for yellow, cyan, magenta and white.
Button Control
Add a push button to manually trigger color changes or mode switching.
Mood Lighting
Build a full RGB mood lamp with preset color scenes over Wi-Fi.
IoT Control
Use MQTT or a web server to control LED colors from a smartphone app.
Comments
Post a Comment