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


This circuit shows three RGB LEDs controlled by an ESP32 (MAO ESP32-S2 board). Here's how to build and program it in Wokwi:

Circuit Components

  • 1x ESP32-S2 development board
  • 3x RGB LEDs (common cathode)
  • 3x 220Ω resistors (for red channels)
  • 3x 220Ω resistors (for green channels)
  • 3x 220Ω resistors (for blue channels)

Step-by-Step Wokwi Setup

Step 1: Create New Project

  1. Go to wokwi.com
  2. Click "Start New Project"
  3. Select "ESP32" as your board type

Step 2: Add Components

  1. Click the "+" button to add parts
  2. Add 3 RGB LEDs to your workspace
  3. Add 9 resistors (220Ω each)
  4. Position them as shown in the diagram

Step 3: Wire the Circuit

Based on the diagram, connect:

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": {}
}

LED 1 (Blue LED):

  • Red pin → 220Ω resistor → GPIO pin (e.g., GPIO 5)
  • Green pin → 220Ω resistor → GPIO pin (e.g., GPIO 6)
  • Blue pin → 220Ω resistor → GPIO pin (e.g., GPIO 7)
  • Common cathode → GND

LED 2 (Green LED):

  • Red pin → 220Ω resistor → GPIO pin (e.g., GPIO 8)
  • Green pin → 220Ω resistor → GPIO pin (e.g., GPIO 9)
  • Blue pin → 220Ω resistor → GPIO pin (e.g., GPIO 10)
  • Common cathode → GND

LED 3 (Red LED):

  • Red pin → 220Ω resistor → GPIO pin (e.g., GPIO 11)
  • Green pin → 220Ω resistor → GPIO pin (e.g., GPIO 12)
  • Blue pin → 220Ω resistor → GPIO pin (e.g., GPIO 13)
  • Common cathode → GND

Step 5: Run the Simulation

  1. Click the green "Start Simulation" button
  2. Watch the LEDs cycle through the colors
  3. Adjust timing in the delay() functions as needed
CODE:
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() {
  Serial.println("Red");
  digitalWrite(D2, HIGH);
  delay(500);
  digitalWrite(D2, LOW);

  Serial.println("Green");
  digitalWrite(D3, HIGH);
  delay(500);
  digitalWrite(D3, LOW);

  Serial.println("Blue");
  digitalWrite(D4, HIGH);
  delay(500);
  digitalWrite(D4, LOW);
}



Comments