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
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 board | RED | GREEN | BLUE |
|---|---|---|---|
| Arduino Uno | D9 | D10 | D11 |
| ESP32 | GPIO 25 | GPIO 26 | GPIO 27 |
| ESP32-S3 | GPIO 4 | GPIO 5 | GPIO 6 |
| Raspberry Pi Pico | GP13 | GP14 | GP15 |
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
offOn 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 - 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.
The same sequence in MicroPython, for an ESP32, an ESP32-S3 or a Pico. Pin.OUT makes each pin an output, and value(1) and value(0) set it HIGH and LOW.
"""
RGB LED - the first colour, MicroPython 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 -> GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3,
GP13 on a Raspberry Pi Pico
GREEN -> GPIO 26, GPIO 5, GP14
BLUE -> GPIO 27, GPIO 6, GP15
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save it to the board as main.py to run it on every power-up.
Nothing to install: machine and time are built in.
"""
from machine import Pin
import time
# ESP32: 25, 26, 27. ESP32-S3: 4, 5, 6. Pico: 13, 14, 15.
red = Pin(4, Pin.OUT)
green = Pin(5, Pin.OUT)
blue = Pin(6, Pin.OUT)
def show(r, g, b, name):
red.value(r) # 1 lights: common cathode
green.value(g)
blue.value(b)
print(name)
time.sleep_ms(1000)
while True:
show(1, 0, 0, "red")
show(0, 1, 0, "green")
show(0, 0, 1, "blue")
show(0, 0, 0, "off")There is no Uno here: an Uno cannot run MicroPython. The pin numbers are GPIO numbers, as in the Arduino sketch. Stop it with Ctrl-C in Thonny's shell, and the LED stays in whatever colour it was showing.
When it does not work
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.
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.
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.
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.
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
Questions about this product
See what other owners have asked, and read their solutions.
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.