Pico W RGB LED mixes orange with PWM
MicroPython on a Pico W: an RGB LED with red on GP13, green on GP14 and blue on GP15 mixes orange from PWM levels given as 0 to 255. The whole build, a Raspberry Pi Pico W and 4 more parts, 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.40. On Mokxi they're free.
What you'll learn
- Mixing colors from red, green and blue
- PWM levels as color brightness
- A common-ground RGB LED
- Using a function to hold a color recipe
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Raspberry Pi Pico W
- Parts: 5
How it works
The RGB LED has three tiny LEDs in one case, red, green and blue, sharing a common leg C that goes to ground. Red is on GP13 through a 330 ohm resistor. Green is on GP14 and blue on GP15, each through a 220 ohm resistor.
Each pin runs PWM at 1000 Hz, so its level sets how bright that color is. The program calls color once with 255, 128 and 0, which mixes orange, prints orange and then idles.
Parts list
5 parts, plus the jumper wires. They retail for about $6.40; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × RGB LEDIn Starter Projects Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 330 Ω pin 2 | Raspberry Pi Pico W pin GP13 |
| Resistor, 220 Ω (1) pin 2 | Raspberry Pi Pico W pin GP14 |
| Resistor, 220 Ω (2) pin 2 | Raspberry Pi Pico W pin GP15 |
| Resistor, 330 Ω pin 1 | RGB LED pin R |
| RGB LED pin C | Ground |
| Resistor, 220 Ω (1) pin 1 | RGB LED pin G |
| Resistor, 220 Ω (2) pin 1 | RGB LED pin B |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Colorful light: an RGB LED with red on GP13, green on GP14 and blue on
# GP15, each through its own resistor, mixed with PWM. color() takes red,
# green and blue from 0 to 255, the way colors are written on screens.
from machine import Pin, PWM
import time
channels = [PWM(Pin(n)) for n in (13, 14, 15)]
for ch in channels:
ch.freq(1000)
def color(r, g, b):
for ch, level in zip(channels, (r, g, b)):
ch.duty_u16(level * 65535 // 255)
color(255, 128, 0) # orange: all the red, half the green
print("orange")
while True:
time.sleep(1)
Code walkthrough
The key functions of the sketch, and what each one does.
color main.py
Takes red, green and blue from 0 to 255, like colors on a screen. It scales each one to the PWM range of 0 to 65535 and sets the duty on its pin. Try color(0, 0, 255) for blue.
def color(r, g, b):
for ch, level in zip(channels, (r, g, b)):
ch.duty_u16(level * 65535 // 255)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The color looks wrong. This project
Check each wire. Red is GP13, green is GP14 and blue is GP15, in the same order as the list of pins. - The LED does not light. This project
The common leg C must go to ground. - The colors are wrong or one color never lights. RGB LED
Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on. - One color is much brighter than the others, or the LED runs too hot. RGB LED
Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts. - WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.