Board and part

Raspberry Pi Pico RGB LED Simulator: Wiring, Code and a Live Circuit

This is a common cathode RGB LED wired to a Raspberry Pi Pico through three 220 ohm resistors and stepping through red, green, blue and white live in your browser.

  • 3.3 V logic
  • No library: analogWrite()
  • Colors on 11, 12, 13
  • Common cathode, 3 x 220 ohm
Raspberry Pi Pico · RGB LEDlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the RGB LED to the Raspberry Pi Pico

The three color pins go to GP11 (physical pin 15), GP12 (physical pin 16) and GP13 (physical pin 17), all of which can do PWM, and the long leg goes to ground.

Every connection in the circuit above, with the board’s own pin names.

RGB LED pinRaspberry Pi Pico pinWhy
R (red)GP11 (physical pin 15)The red anode, through its own 220 ohm resistor. The pin must be able to do PWM.
G (green)GP12 (physical pin 16)The green anode, through its own resistor.
B (blue)GP13 (physical pin 17)The blue anode, through its own resistor.
C (common cathode)GNDThe longest leg joins all three cathodes and goes to ground.
Three 220 ohm resistorsOne in each color leadEach color is its own LED and needs its own resistor.

Power

Each color is a separate LED behind its own 220 ohm resistor, so each pin drives only a few milliamps at 3.3 V. This is a common cathode part: HIGH lights a color. A common anode part has its long leg on VBUS (5 V from USB) and lights on LOW, so analogWrite values must be inverted.

Pins to leave alone on the Raspberry Pi Pico

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

On a real bench

Solder headers on the Pico and press it into the breadboard across the center channel; the RGB LED goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

RGB LED on Raspberry Pi Pico GP11 (physical pin 15), GP12 (physical pin 16), GP13 (physical pin 17)
const int RED_PIN = 11;     // GP11 (physical pin 15)
const int GREEN_PIN = 12;   // GP12 (physical pin 16)
const int BLUE_PIN = 13;    // GP13 (physical pin 17)

void setColor(int r, int g, int b, const char* name) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
  Serial.println(name);
  delay(400);
}

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

void loop() {
  setColor(255, 0, 0, "Red");
  setColor(0, 255, 0, "Green");
  setColor(0, 0, 255, "Blue");
  setColor(255, 255, 255, "White");
}

analogWrite() sends a PWM signal whose duty is the value from 0 to 255, so each color can be set to any brightness and the three mix into any color. 255 on all three makes white, and 0 on all three turns the LED off.

setColor() writes the three duties, prints the color’s name and waits 400 ms, so the loop steps red, green, blue and white about every two seconds.

Serial.begin(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.

Pitfalls

Common mistakes with the RGB LED on the Raspberry Pi Pico

Using one resistor for all three colors

The three LEDs have different voltage drops, so on one shared resistor the color that drops least takes most of the current and the mix looks wrong. Give each color its own resistor.

Mixing up common cathode and common anode

A common cathode part has its long leg on ground and lights when a pin goes HIGH. A common anode part has its long leg on VBUS (5 V from USB) and lights when a pin goes LOW. With the wrong type everything looks inverted: white when you wanted off.

Choosing pins without PWM

analogWrite() only makes a smooth brightness on pins with PWM hardware. On a pin without it the color is either fully on or fully off. Every GP pin on the Pico has a PWM slice behind it.

FAQ

Questions and answers

How do I connect an RGB LED to a Raspberry Pi Pico?

Put a 220 ohm resistor in each color lead, wire them to GP11 (physical pin 15), GP12 (physical pin 16) and GP13 (physical pin 17), and join the longest leg to GND for a common cathode part. The sketch then sets each color with analogWrite().

How do I tell a common cathode from a common anode LED?

Connect the long leg to ground and a color leg to 5 V through a resistor. If the color lights, it is common cathode. If nothing lights, try the long leg on power and the color leg on ground.

How do I make other colors?

Mix the three duties: setColor(255, 128, 0, ...) is orange and setColor(128, 0, 255, ...) is violet. The eye does not see brightness in a straight line, so a gamma table gives smoother fades.

Why is the white tinted?

The three LEDs are not equally bright, so equal duties are not white. Lower the strongest color, usually green or blue, until the mix looks right.

Build your own RGB LED project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.