neopixel.py

Raspberry Pi Pico 2 · File : neopixel.py · Language : Python · Use case : Animation de type « tête qui avance » sur ruban NeoPixel

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 2 (RP2350 dual Cortex-M33 @ 150 MHz) 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 2 » MicroPython build (hold BOOTSEL, plug USB, drop the .uf2), then copy the script via Thonny. Auto-starts when named main.py.

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.

← Back to the board