ESP32-S3 Lab · Day 9 of 30

Mix any colour
from three lights

Today the board becomes a painter. You wire one small dome that holds three lights — red, green, and blue — to three PWM pins, then set a brightness on each. Your eye adds the three glows into a single colour, which is how every screen you own builds its picture from just three numbers.

About 25 minutesArduino firstMicroPython optionalNo electronics assumed
Agent assist code TSK-DAY09-RGB

Hand this to an agent so it can pull the lesson packet and coach you step by step.

01 First, know the pieces

What you need

Six things, most of them small. Tap Define on any part you haven't met — the answer opens as a field note you can read and dismiss without losing your place.

Official manual photo of the ESP32-S3 development board.
Manual photo

ESP32-S3 board

The brain that runs your uploaded sketch.

Official manual image of the ESP32-S3 GPIO extension board.
Manual photo

GPIO extension board

Spreads the pins into rows you can reach and label.

Official manual photo of an RGB LED showing its long common pin and three colour legs.
Manual photo

RGB LED

Three lights — red, green, blue — in one dome with a shared pin.

Official manual photo of a resistor with coloured value bands.
Manual photo

3 × 220 Ω resistors

One in series with each colour leg to keep the current gentle.

Official manual image of a jumper wire.
Manual photo

Jumper wires

Temporary, solder-free connections.

Official manual screenshot of the Arduino IDE interface.
Manual screenshot

Arduino IDE

Uploads the sketch to the board.

02 Make the physical circuit

Chart the circuit

The official Freenove diagram is your chart — schematic on top, the same circuit built on a breadboard below. Click it to enlarge. Each connection tells you where the wire goes and why.

Official Freenove circuit — C Tutorial, Chapter 5 (RGB LED), page 72.
Red leg (via 220 Ω) GPIO 38 One PWM channel sets how much red shows.
Green leg (via 220 Ω) GPIO 39 One PWM channel sets how much green shows.
Blue leg (via 220 Ω) GPIO 40 One PWM channel sets how much blue shows.
Common (long) pin 3.3V All three share this positive pin so a colour lights when its pin goes LOW.

The common pin goes to 3.3V. This is a common-anode LED, so the long common pin goes to 3.3V and not to ground — the opposite of a single LED. Each colour leg reaches its own GPIO through a 220 Ω resistor. Unplug USB before you move any wire.

03 One action at a time

Build it

This is the main path — you can finish the day without opening a single field note. Tap each step as you go to keep your place.

0 / 9 done
  1. Seat the ESP32-S3 on the GPIO extension board and keep USB unplugged while you wire.

  2. Find the RGB LED's long pin — that's the common one shared by all three colours.

  3. Wire the red leg through a 220 Ω resistor to GPIO 38.

  4. Wire the green leg through a 220 Ω resistor to GPIO 39.

  5. Wire the blue leg through a 220 Ω resistor to GPIO 40.

  6. Run the long common pin to 3.3V, not to ground.

  7. Compare every wire to the chart before you plug in USB.

  8. Open Sketch_05.1_RandomColorLight.ino in Arduino IDE and upload it.

  9. Watch the dome change colour again and again.

One dome, every colour.

The board is mixing red, green, and blue for you now. Head to Test & debug to confirm the colours keep changing.

04 Read just enough code

Read the code

The sketch sets up three PWM channels, then paints a colour by giving each one a brightness. Switch to MicroPython if you'd rather see the same idea in Python — the wiring never changes.

Sketch_05.1_RandomColorLight.ino
const byte ledPins[] = {38, 39, 40};   // R, G, B
const byte chns[]    = {0, 1, 2};

void setup() {
  for (int i = 0; i < 3; i++) {
    ledcAttachChannel(ledPins[i], 1000, 8, chns[i]);
  }
}

void loop() {
  int red   = random(0, 256);
  int green = random(0, 256);
  int blue  = random(0, 256);

  ledcWrite(ledPins[0], 255 - red);    // common anode: LOW = brighter
  ledcWrite(ledPins[1], 255 - green);
  ledcWrite(ledPins[2], 255 - blue);
  delay(200);
}
const byte ledPins[] = {38, 39, 40}The three pins are the red, green, and blue legs in that order.
ledcWrite(ledPins[0], 255 - red)Writes 255 minus the value because this common-anode LED lights when its pin goes LOW.
red = random(0, 256)Each colour gets a fresh brightness from 0 to 255, and setting all three at once mixes the shade.

05 Understand, don't memorise

Any colour is three brightnesses

Day 7 turned one pin into a dimmer with PWM. Today you run that same dimmer on three pins — one for red, one for green, one for blue — and the colours add up in your eye. That single idea is the whole of colour on every screen you own, built from three numbers.

1

Three PWM dials

Each colour leg gets its own PWM channel — the Day 7 dimmer, running three times over instead of once.

2

0 to 255 each

Each channel is set to 8-bit, so a colour has 256 brightness steps from 0 to 255; three of them together reach millions of shades.

3

Your eye adds them

The red, green, and blue glows overlap under one lens and arrive at your eye as a single blended colour.

4

Equal makes white

With no colour winning — red, green, and blue at the same level — the mix stays neutral, dim grey low down and white at full.

5

Anode or cathode

The one shared pin sets the polarity, deciding whether a colour lights when its pin goes LOW or when it goes HIGH.

The model colour = red + green + blue, each a PWM level from 0 to 255

Common anode vs common cathode

Three LEDs share one pin. Run that shared pin to 3.3V and you have a common anode, where each colour lights when its own pin goes LOW. Run it to ground and you have a common cathode, lit on HIGH. This kit is a common anode, so the sketch writes 255 minus your value and a bigger number still means brighter.

Why the numbers stop at 255

The channels are 8-bit, which gives 256 levels per colour, 0 through 255. That is the same ledc hardware from Day 7 doing the fast switching, while your loop just names one level for each of the three colours.

Why equal numbers look grey

When red, green, and blue are equal, none of them leads, so your eye finds no hue to settle on and reads neutral — grey at a low level, white at a high one.

06 Know it worked

Test & debug

Nothing prints to the screen today — the proof is the colour in the dome.

What you should see
RGB LED
  • The RGB LED glows a colour.
  • Every fraction of a second it jumps to a new random colour.
  • It keeps changing for as long as the board has power.

The colours look random on purpose — each one is a fresh mix of red, green, and blue.

If it doesn't
  • Only one colour ever appears? Check that colour's 220 Ω resistor and its GPIO wire — the other two legs may not be reaching their pins.
  • Nothing lights at all? Check the common pin — it goes to 3.3V, not to ground.
  • A colour is stuck full-on? Suspect the common-anode wiring or the wrong leg on that pin.
  • Upload fails? Swap in a data-capable USB cable.

07 Make the idea yours

Try this: predict a colour, then mix it

The random sketch proves the board can mix. This proves you know the recipe. You'll predict three colours from their red, green, and blue numbers, then set those numbers by hand and check your eye against your maths. It fits inside today's 25 minutes.

Predict three colours

On paper, write the three numbers from 0 to 255 for orange, then purple, then white. Orange is a lot of red with some green and no blue; purple is red and blue together with green held low; white is all three at full. Notice that equal numbers should read as neutral grey or white.

Mix it by hand

In the sketch, swap each random(0, 256) for your fixed red, green, and blue number, upload, and compare the dome with your prediction. The code still writes 255 minus each value for the common anode, so your bigger number is still the brighter one. If a colour looks off, nudge the number and re-upload.

08 Learn it with a hand on the tiller

Coach me through it

Every lesson ships with a code and a machine-readable packet, so an agent can guide you with full context.

Lesson code

TSK-DAY09-RGB

How the agent should behave: guide one physical connection at a time and wait for confirmation, teach that any colour is three PWM brightnesses from 0 to 255, explain the common-anode inversion on request, and help the learner predict then mix a named colour. Always check the common pin, wiring, board, and USB before changing code.

Keep your place

Finished Day 9?

Mark it complete — it shows on your course map, and your place is saved on this device.

Field note

Shortcut

Prompt copied