Built-in project · ESP32-S3-DevKitC-1

ESP32-S3 rainbow

The DevKitC-1’s own RGB LED walking around the color wheel, one WS2812 frame from the RMT every 20 ms. The whole build, an ESP32-S3-DevKitC-1, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 5 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $20. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Driving an addressable RGB LED with rgbLedWrite
  • Mixing red, green and blue into a color wheel
  • Returning several values through pointers
  • Pacing an animation with delay
  • Difficulty: Beginner
  • Time: About 5 minutes
  • Board: ESP32-S3-DevKitC-1
  • Parts: 2

How it works

The ESP32-S3 DevKitC-1 has an RGB LED built in on GPIO48. It is a WS2812, which takes its color as 24 bits sent as timed pulses. The chip's RMT hardware makes those pulses, and the LED holds its color until the next frame arrives.

The sketch walks a position around a color wheel from 0 to 255 in steps of 4. For each step it works out a red, green and blue value, sends it to the LED, and waits 20 ms. When the position reaches the end, the loop starts again, so the colors cycle smoothly.

Parts list

2 parts, plus the jumper wires. They retail for about $20; on Mokxi they're free.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ESP32-S3-DevKitC-1
// Rainbow: the RGB LED on GPIO48 walks round the color wheel.
//
// rgbLedWrite(pin, red, green, blue) sends one WS2812 frame on RMT channel 0:
// twenty-four bits, green first, each a high pulse of 400 or 800 ns. The
// color holds until the next frame, so the sketch only has to send one
// every few milliseconds.

const int kLed = RGB_BUILTIN;

// One step round the wheel: 0..255 in, a color out, at full brightness / 4.
void wheel(int pos, int *r, int *g, int *b)
{
    pos = pos & 255;
    if (pos < 85) {
        *r = 255 - pos * 3;
        *g = pos * 3;
        *b = 0;
    } else if (pos < 170) {
        pos -= 85;
        *r = 0;
        *g = 255 - pos * 3;
        *b = pos * 3;
    } else {
        pos -= 170;
        *r = pos * 3;
        *g = 0;
        *b = 255 - pos * 3;
    }
    *r /= 4;
    *g /= 4;
    *b /= 4;
}

void setup()
{
    Serial.begin(115200);
    Serial.println("rainbow on the RGB LED, GPIO48");
}

void loop()
{
    for (int pos = 0; pos < 256; pos += 4) {
        int r, g, b;
        wheel(pos, &r, &g, &b);
        rgbLedWrite(kLed, r, g, b);
        delay(20);
    }
}

Code walkthrough

The key functions of the sketch, and what each one does.

wheel sketch.ino

Turns a position from 0 to 255 into a color. The wheel has three thirds. In each one, one color fades out while the next fades in. At the end it divides each value by 4 so the LED is not too bright. The results go back through the pointers r, g and b.

// One step round the wheel: 0..255 in, a color out, at full brightness / 4.
void wheel(int pos, int *r, int *g, int *b)
{
    pos = pos & 255;
    if (pos < 85) {
        *r = 255 - pos * 3;
        *g = pos * 3;
        *b = 0;
    } else if (pos < 170) {
        pos -= 85;
        *r = 0;
        *g = 255 - pos * 3;
        *b = pos * 3;
    } else {
        pos -= 170;
        *r = pos * 3;
        *g = 0;
        *b = 255 - pos * 3;
    }
    *r /= 4;
    *g /= 4;
    *b /= 4;
}

setup sketch.ino

Opens serial at 115200 baud and prints a short line saying the rainbow is running on GPIO48.

void setup()
{
    Serial.begin(115200);
    Serial.println("rainbow on the RGB LED, GPIO48");
}

loop sketch.ino

Steps pos from 0 to 252 in jumps of 4. For each one it calls wheel and sends the color with rgbLedWrite, then waits 20 ms. That is 64 steps per trip around the wheel.

void loop()
{
    for (int pos = 0; pos < 256; pos += 4) {
        int r, g, b;
        wheel(pos, &r, &g, &b);
        rgbLedWrite(kLed, r, g, b);
        delay(20);
    }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Some pins do not work or crash the board. ESP32-S3-DevKitC-1
    Avoid the pins used by flash and PSRAM (GPIO 26 to 37 on some modules) and the strapping pins (0, 3, 45, 46). Check the board pinout.
  • Pins are damaged or signals misread. ESP32-S3-DevKitC-1
    Logic is 3.3 V and not 5 V tolerant. Use level shifting for 5 V parts. Select the right board and USB mode so uploads and serial work.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.