RGB LED/One colour at a time/03. The first colour
One colour at a time · 03 of 9

The first colour

Four wires, no library, and a sketch that shows red, green and blue in turn, one second each. On a 5 V board all three are bright; on a 3.3 V board red is bright and green and blue are dimmer, and both are right.

Four wires

Four wires
Your board
Wires
4
RED, GREEN, BLUE to
4, 5, 6
Logic
3.3 V
GND to GND, then RED, GREEN and BLUE to GPIO 4, GPIO 5, GPIO 6. On the ESP32-S3 these three are free: none is a strapping pin and none is used by the flash. Any output pin can do PWM.

GND to GND. RED, GREEN and BLUE to three pins that can do PWM. The two NC pins stay unconnected, because nothing on the board is attached to them.

The pins are chosen so that nothing else uses them at boot:

Your boardREDGREENBLUE
Arduino UnoD9D10D11
ESP32GPIO 25GPIO 26GPIO 27
ESP32-S3GPIO 4GPIO 5GPIO 6
Raspberry Pi PicoGP13GP14GP15

The ESP32 row is not the ESP32-S3's. On a classic ESP32, GPIO 6 to 11 are the flash chip's pins, and a sketch that drives one of them stops the board. On an ESP32-S3 the flash is elsewhere and GPIO 6 is a free pin.

The sketch

show() sets all three pins at once, prints the colour's name and waits a second. loop() calls it four times: red, green, blue, off. Every pin is written every time, so the LED never shows a leftover colour from the step before.

HIGH lights a colour because the board is common cathode; the previous article has the reason. The sketch has no 255 - value anywhere, and it should not.

What you should see

Red, green and blue, one second each, then a second of dark, and the serial monitor at 115200 printing:

red
green
blue
off

On an Uno all three are bright. On an ESP32, an ESP32-S3 or a Pico, red is plainly lit but green and blue are much dimmer. Nothing is wrong: from 3.3 V the green and blue LEDs get very little current. The next article works out how little.

The code

No library. pinMode makes the three pins outputs, and digitalWrite sets each HIGH or LOW. Change the three pin numbers to the pins you wired RED, GREEN and BLUE to.

rgb_led_first_colour.ino
/*
  RGB LED - the first colour                              TK02 / /p/tk02

  Wiring. Count from the square pad on the TinkerBlock board, LED side
  up, header at the bottom:

    GND    -> GND
    NC     -> nothing   (both NC pins are unconnected on the board)
    NC     -> nothing
    RED    -> D9 on an Uno, GPIO 25 on an ESP32,
              GPIO 4 on an ESP32-S3, GP13 on a Raspberry Pi Pico
    GREEN  -> D10, GPIO 26, GPIO 5, GP14
    BLUE   -> D11, GPIO 27, GPIO 6, GP15

  Arduino IDE
    Tools > Board                 your board, e.g. ESP32S3 Dev Module
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    No library needed.
*/

// Uno: 9, 10, 11. ESP32: 25, 26, 27. ESP32-S3: 4, 5, 6. Pico: 13, 14, 15.
const int RED_PIN = 4;
const int GREEN_PIN = 5;
const int BLUE_PIN = 6;

void show(bool r, bool g, bool b, const char *name) {
  digitalWrite(RED_PIN, r ? HIGH : LOW);   // HIGH lights: common cathode
  digitalWrite(GREEN_PIN, g ? HIGH : LOW);
  digitalWrite(BLUE_PIN, b ? HIGH : LOW);
  Serial.println(name);
  delay(1000);
}

void setup() {
  Serial.begin(115200);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  show(true, false, false, "red");
  show(false, true, false, "green");
  show(false, false, true, "blue");
  show(false, false, false, "off");
}

The pin numbers are the numbers your board prints beside its pins: 9, 10 and 11 are D9, D10 and D11 on an Uno, and 25, 26 and 27 are GPIO 25, 26 and 27 on an ESP32. All three are PWM pins on every board, so the same wiring works for the mixing sketches in chapter 3.

When it does not work

One colour never comes on.

Check that its wire is on the pin the sketch names for it. The numbers are the board's own GPIO numbers as printed beside the pins, not positions along the header. Then count from the square pad: RED is the fourth pin, GREEN the fifth, BLUE the sixth.

The colours come in the wrong order.

Two wires are crossed, or the three pin numbers in the sketch are in a different order from the wires. Swap the numbers in the sketch rather than the wires. The serial monitor names the colour the sketch meant, so you can see which is which.

Why not GPIO 4, 5 and 6 on my ESP32, as on the ESP32-S3?

On a classic ESP32, GPIO 6 to 11 are wired to the flash chip that holds your program. Driving one of them stops the board, often at boot. GPIO 25, 26 and 27 are free outputs on the common ESP32 dev boards, so the book uses those.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print. The colours changing tells you the sketch itself is fine.

Where this goes next

Why the same resistor on every colour does not mean the same current.

Three resistors, three currents

Edit this page — content/books/rgb-led/the-first-colour.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

RGB LED

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum