Raspberry Pi Pico RGB LED Color Cycle (3-Channel PWM)
One LED, millions of colors! An RGB LED actually holds 3 separate LEDs: red, green and blue. Set the brightness of each one separately with the Pico's 3 PWM channels and the colors mix: red + green = yellow, green + blue = cyan, all three = white. In this project the LED cycles through 8 colors.

Video
Circuit Diagram

Source Code
1# ─────────────────────────────────────────
2# BlueGrays · bluegrays.com
3# Raspberry Pi Pico RGB LED Color Cycle (3-Channel PWM, GP11-13)
4# ─────────────────────────────────────────
5# Color mixing on an RGB LED with three PWM channels: the LED cycles
6# through 8 colors, one every 0.5 s. set_color() converts 0-255 values to
7# 16-bit PWM. Set COMMON_ANODE = True for a common anode LED.
8#
9# Pins:
10# GP13 -> RGB LED R (with a 220 ohm resistor)
11# GP12 -> RGB LED G (with a 220 ohm resistor)
12# GP11 -> RGB LED B (with a 220 ohm resistor)
13# Common cathode -> GND
14
15from machine import Pin, PWM
16from time import sleep_ms
17
18COMMON_ANODE = False # True for a common anode LED
19
20# ============ PWM CHANNELS ============
21r = PWM(Pin(13)); r.freq(1000) # Red
22g = PWM(Pin(12)); g.freq(1000) # Green
23b = PWM(Pin(11)); b.freq(1000) # Blue
24
25# Sets the color: each channel 0-255
26def set_color(red, green, blue):
27 # Convert 0-255 to 16-bit (x257)
28 for channel, value in ((r, red), (g, green), (b, blue)):
29 if COMMON_ANODE: # Inverted for common anode
30 value = 255 - value
31 channel.duty_u16(value * 257)
32
33# ============ COLOR LIST ============
34COLORS = [
35 (255, 0, 0), # Red
36 (255, 165, 0), # Orange
37 (255, 255, 0), # Yellow
38 (0, 255, 0), # Green
39 (0, 255, 255), # Cyan
40 (0, 0, 255), # Blue
41 (128, 0, 128), # Purple
42 (255, 255, 255), # White
43]
44
45# ============ MAIN LOOP ============
46while True:
47 # Each color stays for 0.5 s
48 for k, y, m in COLORS:
49 set_color(k, y, m)
50 sleep_ms(500)
51FAQ
Why is the 8-bit color value multiplied by 257?
Colors are usually written in the 0–255 range, while the Pico's PWM works in the 0–65535 range. Since 255 × 257 = 65535, multiplying by 257 maps the 8-bit value exactly onto the 16-bit range.
I'm using a common-anode RGB LED. What should I change?
The common leg goes to 3.3 V and brightness works in reverse: write the duty value as 65535 - value. Otherwise the colors appear inverted.
Why don't the colors look perfectly white?
The red, green and blue LEDs aren't equally bright. You can adjust the white balance by applying a separate factor to each channel (e.g. red at 80%).
Can I fade smoothly between colors?
Yes, change the R, G and B values in small steps from one color to the next and you'll get smooth, rainbow-like transitions.
