How to Control RGB LEDs with ESP32: Complete Wokwi Simulator Tutorial

ESP32 RGB LED Control Tutorial – Wokwi Simulation Guide | MakeMindz
Beginner Friendly

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 Simulation

What 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.

🎨
Color Cycling
Primary color patterns
🔌
GPIO Control
Digital HIGH / LOW output
🖥️
Wokwi Sim
No hardware needed
PWM Ready
Extendable with fading

What You Need (Virtual)

  • 🧠
    XIAO ESP32-S3
    Main microcontroller board
    ×1
  • 💡
    RGB LED – Common Cathode
    4-pin (R, G, B, GND)
    ×3
  • 🔧
    220Ω Resistors
    One per channel (R, G, B)
    ×9
  • 🔗
    Jumper Wires
    For all connections
    ×many
💡 Note: The diagram.json uses 1kΩ resistors for Wokwi simulation. For real hardware, use 220Ω–330Ω resistors for correct LED brightness.

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
100Red
010Green
001Blue
110Yellow
101Magenta
011Cyan
111White

Step-by-Step Wokwi Setup

01

Create a New Project

Go to wokwi.com → Click Start New Project → Select ESP32 board → Choose XIAO ESP32-S3.

02

Add Components

In the diagram editor, add 3 RGB LEDs and 9 resistors (220Ω). Arrange them neatly for easy wiring.

03

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.

04

Upload the Arduino Code

Open the sketch.ino tab, paste the code below, and click ▶ Play to start the simulation.

05

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.

💡 LED 1 — Red
Red channelD2
Green channelD3
Blue channelD4
CathodeGND
💡 LED 2 — Green
Red channelGPIO 8
Green channelGPIO 9
Blue channelGPIO 10
CathodeGND
💡 LED 3 — Blue
Red channelGPIO 11
Green channelGPIO 12
Blue channelGPIO 13
CathodeGND
⚡ Remember: Each color channel must connect through a 220Ω resistor before reaching the GPIO pin to protect both the LED and the ESP32.

diagram.json

Copy this into the diagram.json tab in your Wokwi project to instantly wire up the circuit.

diagram.json
{
  "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.

sketch.ino
// 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 Wokwi

How 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.

Applications of ESP32 RGB Projects

🏠 Smart home lighting indicators
📡 IoT device status monitoring
🔔 Notification alert systems
🎨 Interactive art installations
🤖 Robotics signal systems
💡 Decorative LED panels

Continue Learning

© 2026 MakeMindz · All ESP32 tutorials are tested in Wokwi simulation before publishing.

Comments

try for free