neopixel.py
The built-in neopixel module drives a WS2812B strip on pin16: a rotating rainbow ramp here, stopped by button A. Each LED takes an (R, G, B) tuple and write() sends the full pulse frame.
Required hardware
A BBC micro:bit V2 (nRF52833, 5×5 LED matrix, A/B buttons, accelerometer + compass, microphone and speaker) powered over USB or a battery pack. 1 strip or ring of 12 WS2812B NeoPixels; external 5 V supply beyond 8 LEDs.
Wiring
Data (DIN) to pin16 through a 330 Ω resistor, VCC to external 5 V, GND tied to the micro:bit.
How it works
NeoPixel(pin16, NB) reserves the buffer; ruban[i] = (r, g, b) fills it; write() emits the WS2812 protocol in one burst. The modulo shifts the ramp each round.
Running it
Open python.microbit.org, paste the MicroPython program and send it to the board (the micro:bit shows up as a MICROBIT drive). Press reset to restart.
Variants
Map the light level (read_light_level) to a background color, or scroll the color along the strip on a shake gesture.
neopixel.py
# Ruban NeoPixel/WS2812 sur pin16 avec le module neopixel integre.
import neopixel
from microbit import *
NB = 12
ruban = neopixel.NeoPixel(pin16, NB)
def arc_en_ciel(decalage):
for i in range(NB):
# Rampe rouge-vert-bleu deplacee par le decalage.
p = (i * 3 + decalage) % 256
if p < 85:
ruban[i] = (255 - p * 3, p * 3, 0)
elif p < 170:
q = p - 85
ruban[i] = (0, 255 - q * 3, q * 3)
else:
q = p - 170
ruban[i] = (q * 3, 0, 255 - q * 3)
tete = 0
while True:
arc_en_ciel(tete)
ruban.write()
tete = (tete + 8) % 256
if button_a.was_pressed():
for i in range(NB):
ruban[i] = (0, 0, 0)
ruban.write()
display.show(Image.NO)
sleep(60)
# 12 LED a pleine intensite > 700 mA : alimenter par source separee.