🛠 Piloter la LED RGB embarquée WS2812 d'une carte RP2040 générique
Objectives
"Generic" RP2040 boards (Waveshare RP2040 Zero, Adafruit Feather/QT Py RP2040, SparkFun Micro RP2040…) often do not have a simple LED like the Pico: they feature an addressable WS2812 ("NeoPixel") LED driven by a single-wire 800 kHz protocol that MicroPython generates via the RP2040's PIO. By the end of this tutorial, you will be able to find the LED pin on YOUR board and set it to any color.
Hardware
- 1 × generic RP2040 board. Common pinouts: GP16 on Waveshare RP2040 Zero (manufacturer page), GP17 on Adafruit Feather RP2040 (manufacturer page) — check your manufacturer's datasheet, this is THE key piece of information to find
- 1 × USB cable (data!): many Zero boards use USB-C
- MicroPython rp2 firmware flashed in BOOTSEL mode (hold BOOT while plugging in, the board appears as a
RPI-RP2drive)
Complete code
# main.py — LED WS2812 embarquée (GP16 Waveshare Zero, GP17 Feather)
import neopixel
from machine import Pin
import time
PIN = 16 # à adapter à ta carte !
led = neopixel.NeoPixel(Pin(PIN), 1) # chaîne d'une seule LED
for r, g, b in [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 64, 0)]:
led[0] = (r, g, b) # tuple (R, G, B) 0..255
led.write # la PIO envoie la trame, le CPU est libre
time.sleep_ms(500)
while True: # « arc-en-ciel » rapide
for i in range(256):
led[0] = (i, 255 - i, 128)
led.write
time.sleep_ms(5)
Expected verification
The LED turns red, green, blue, and orange at 500 ms intervals, then displays a continuous gradient. Unlike a standard LED, nothing blinks "off" between two colors: the LED retains its last frame, because the WS2812 stores its command. If your board has multiple WS2812 LEDs (some badges, QT Py with strobe), pass the actual length to NeoPixel(..., n).
Common errors
- Coding GP25 by "Pico" reflex: generic boards almost never have an LED on GP25 — the WS2812 is on GP16, GP17, or elsewhere depending on the manufacturer.
- Forgetting
led.write: the tuple is modified in memory but nothing is sent on the bus. - Looking for an
rp2module for the LED: the WS2812 uses the standardneopixelmodule (included in the rp2 firmware), notrp2.StateMachinedirectly. - Powering a long strip from the board's regulator: the Zero includes an 800 mA LDO (Waveshare datasheet), but a strip of 100 white LEDs draws ~6 A — separate power supply required.
- LED turning white/pink randomly at boot: RP2040 pins are reconfigured on reset; add
led.fill((0,0,0)); led.writeat the beginning of your script.
Other tutorials for this board
- Avoir une console série sur une carte sans pont USB (UART0)
- Jouer une mélodie avec un buzzer passif et le PWM
- Afficher sur un OLED I2C SSD1306 et déboguer le bus
- Logger sur carte microSD en SPI (mode carte)
- Interruptions GPIO et alarmes matérielles avec le Pico SDK
- Transformer la carte en clavier USB HID avec TinyUSB
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython