Learn

Controlling an RGB LED on a Raspberry Pi 4B (Common Cathode & Common Anode)

Wire a common-cathode RGB LED to three Raspberry Pi 4B GPIO pins and mix red, green, and blue in Python — plus exactly how the Common Anode variant wires and drives differently.

Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.

What you’ll need

  • Raspberry Pi 4B
  • RGB LED (Common Cathode)
  • 3 Resistors (~220Ω each, one per color channel)

Step by step

  1. Drag Raspberry Pi 4B and the RGB LED (Common Cathode) onto the Canvas.
  2. Hover over its 4 pins to confirm the labels: R, COM, G, B.
  3. Wire the shared pin first: RGB LED's COM → a Pi GND pin. On a common-cathode part, COM is the pin every color channel shares to ground — each color pin lights when driven HIGH, the same way an ordinary LED's anode does.
  4. Wire each color channel through its own resistor: R → a 220Ω resistor → a GPIO pin (e.g. pin 37). G → a 220Ω resistor → a different GPIO pin (e.g. pin 35). B → a 220Ω resistor → a third GPIO pin (e.g. pin 33).
  5. Go to the Code tab and write:
import RPi.GPIO as GPIO
import asyncio
 
r, g, b = 37, 35, 33
 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(r, GPIO.OUT)
GPIO.setup(g, GPIO.OUT)
GPIO.setup(b, GPIO.OUT)
 
async def show(name, red, green, blue):
print(f"Showing {name}")
GPIO.output(r, red)
GPIO.output(g, green)
GPIO.output(b, blue)
await asyncio.sleep(1.5)
 
await show("Red", 1, 0, 0)
await show("Green", 0, 1, 0)
await show("Blue", 0, 0, 1)
await show("White (all three)", 1, 1, 1)
await show("Off", 0, 0, 0)
 
GPIO.cleanup()
  1. Click Start.

What “working correctly” looks like

  • The dome lights a solid, correctly-colored red, then green, then blue, then a blended white (all three at once), then goes dark — each held for about 1.5 seconds.
  • The wire feeding whichever color is currently lit visibly shows that same color (red/green/blue); an unlit channel's wire stays a plain neutral gray.

If something’s wrong

  • Nothing lights at all → confirm COM is wired to a real Pi GND pin, not left disconnected — every channel needs the shared COM leg grounded to complete the circuit, even if its own color pin is being driven correctly.
  • One color never lights while the others do → check that channel's own resistor is actually wired between the GPIO pin and the LED (not skipped), and that you're driving the same GPIO pin number in your script that you wired on the canvas.
  • Using the Common Anode variant instead? The wiring inverts: COM goes to a Pi power pin (3.3V or 5V) instead of GND, and each channel lights when its own GPIO pin is driven LOW (0) instead of HIGH (1) — take the exact script above and swap every 1/0 in the show() calls to get the same sequence on Common Anode.