neopixel.py
A white head followed by a red trail moves along a 12-LED NeoPixel (WS2812B) strip, driven natively by MicroPython through machine.NeoPixel. Each LED gets its color in a single frame sent by write().
Hardware
A Raspberry Pi Pico W (RP2040 + 2.4 GHz Wi-Fi radio chip) with MicroPython installed, powered over USB. 1 strip/ring of 12 NeoPixel (WS2812B) LEDs; external 5 V supply recommended.
Wiring
Data (DIN) to GP16 (330 Ω resistor advised), VCC to external 5 V, GND tied to the board.
How it works
ruban[i] = (R, G, B) writes into the buffer; write() generates the unique pulse sequence that addresses each LED. The % modulo wraps the head around.
Running it
Install the « Pico W » MicroPython build (BOOTSEL + .uf2), copy the script via Thonny. Since the user LED sits behind the radio chip, output examples use an external LED.
Variants
Use several NeoPixel objects on different pins for two strips, or animate colors with a sinusoidal gradient.
neopixel.py
# Animation sur ruban NeoPixel/WS2812 avec le module machine.NeoPixel.
from machine import NeoPixel, Pin
import time
NB_LED = 12
ruban = NeoPixel(Pin(16), NB_LED) # data sur GP16
tete = 0
while True:
# Eteindre tout puis dessiner la scene.
for i in range(NB_LED):
ruban[i] = (0, 0, 0)
ruban[tete] = (255, 255, 255) # tete blanche
ruban[(tete - 1) % NB_LED] = (200, 0, 0) # comete rouge
ruban[(tete - 2) % NB_LED] = (80, 0, 0) # trainee
ruban.write() # une trame vers les LED
tete = (tete + 1) % NB_LED
time.sleep_ms(80)
# Attention a l'intensite : 12 LED a 255 blanc tirent plus de
# 700 mA. Alimenter le ruban par une source 5 V externe separee.