"""
OK Mario — voici la méthode simple et fiable pour utiliser un NeoPixel (WS2812 / RGB adressable) avec un Pi Pico en MicroPython 💡
Le Pi Pico est conçu par la Raspberry Pi Foundation
Les NeoPixels sont popularisés par Adafruit Industries
✅ Matériel
Tu as besoin de :
Raspberry Pi Pico
1 ou plusieurs NeoPixels (WS2812 / WS2812B)
Résistance 330 Ω (recommandée sur DATA)
Alim 5 V si plusieurs LEDs
Fil DATA (ex : GPIO15)
Connexion typique :
Pico GPIO15 ──330Ω── DATA NeoPixel
Pico GND ────────── GND NeoPixel
Pico VSYS/5V ────── VCC NeoPixel
⚠️ Important : GND commun obligatoire
"""
#*******************************
# ✅ Code MicroPython minimal
# Le Pico possède le module neopixel intégré 👍
from machine import Pin
import neopixel
import time
PIN = 0 # GPIO utilisé
NB = 12 # nombre de LEDs
PinStrip = 10 # GPIO utilisé
NbStrip = 12 # nombre de LEDs
Pin_Matrix=16 # GPIO utilisé
NbPinMatrix=64 # nombre de Leds 64 (8x8)
np = neopixel.NeoPixel(Pin(PIN), NB) # objet neopixel class NeoPixel
np2= neopixel.NeoPixel(Pin(PinStrip), NbStrip) # objet neopixel class NeoPixel
npMatrix=neopixel.NeoPixel(Pin(Pin_Matrix),NbPinMatrix) # objet neopixel class NeoPixel
# allumer la première LED en rouge
np[0] = (255, 0, 0) # 0>1er led,1>2ieme led etc
np2[0]= (255, 0, 0) # 0>1er led,1>2ieme led etc
npMatrix[0]=(255, 0, 0) # 0>1er led,1>2ieme led etc
np.write()
np2.write()
npMatrix.write()
time.sleep(1)
# tout éteindre
for i in range(NB):
np[i] = (0,0,0)
np2[i] = (0,0,0)
npMatrix[i] = (0,0,0)
np.write()
np2.write()
npMatrix.write()
#**********************
"""
from machine import Pin
import neopixel
import time
NUM_LEDS = 12
PIN = 0 # GP0
PIN_Matrix=16 # GP16
np = neopixel.NeoPixel(Pin(PIN), NUM_LEDS)
matrix=neopixel.NeoPixel(Pin(PIN_Matrix), NUM_LEDS)
#********************************
# 🧠 Code MicroPython basique
# MicroPython possède le module neopixel.
# Exemple : faire clignoter 8 LEDs
def clear():
for i in range(NUM_LEDS):
np[i] = (0, 0, 0)
np.write()
matrix.write()
while True:
# Rouge
for i in range(NUM_LEDS):
np[i] = (255, 0, 0)
np.write()
time.sleep(1)
# Vert
for i in range(NUM_LEDS):
np[i] = (0, 255, 0)
np.write()
time.sleep(1)
# Bleu
for i in range(NUM_LEDS):
np[i] = (0, 0, 255)
np.write()
time.sleep(1)
# Cyan
for i in range(NUM_LEDS):
np[i] = (0,255,255)
np.write()
time.sleep(1)
# Magenta
for i in range(NUM_LEDS):
np[i] = (255,0,255)
np.write()
time.sleep(1)
# Yellow
for i in range(NUM_LEDS):
np[i] = (255,255,0)
np.write()
time.sleep(1)
# Orange
for i in range(NUM_LEDS):
np[i] = (255,100,0)
np.write()
time.sleep(1)
# White
for i in range(NUM_LEDS):
np[i] = (255,255,255)
np.write()
time.sleep(1)
clear()
time.sleep(1)
"""
#*****************************************************
"""
# 🎨 Exemple : effet arc-en-ciel
def wheel(pos):
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
elif pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
else:
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
while True:
for j in range(255):
for i in range(NUM_LEDS):
np[i] = wheel((i + j) & 255)
np.write()
time.sleep(0.02)
"""
#*************************************************
"""
# ✅ Un exemple avec bouton
# Voici un exemple simple : un bouton pour changer la couleur
# des NeoPixel à chaque appui (Rouge → Vert → Bleu → Blanc → Éteint).
from machine import Pin
import neopixel
import time
# ----- CONFIGURATION -----
NUM_LEDS = 12
LED_PIN = 0
BUTTON_PIN = 14
# ----- INITIALISATION -----
np = neopixel.NeoPixel(Pin(LED_PIN), NUM_LEDS)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
colors = [
(255, 0, 0), # Rouge
(0, 255, 0), # Vert
(0, 0, 255), # Bleu
(255, 255, 255),# Blanc
(0, 0, 0) # Éteint
]
color_index = 0
def set_color(color):
for i in range(NUM_LEDS):
np[i] = color
np.write()
set_color(colors[color_index])
# ----- BOUCLE PRINCIPALE -----
while True:
if button.value() == 0: # Bouton appuyé
time.sleep(0.2) # Anti-rebond simple
color_index = (color_index + 1) % len(colors)
set_color(colors[color_index])
# Attendre relâchement
while button.value() == 0:
pass
"""
#*******************************************************
"""
# 💻 Code avec interruption (IRQ)
from machine import Pin
import neopixel
import time
# ----- CONFIGURATION -----
NUM_LEDS = 8
LED_PIN = 0
BUTTON_PIN = 14
np = neopixel.NeoPixel(Pin(LED_PIN), NUM_LEDS)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
colors = [
(255, 0, 0), # Rouge
(0, 255, 0), # Vert
(0, 0, 255), # Bleu
(255, 255, 255), # Blanc
(0, 0, 0) # Éteint
]
color_index = 0
last_press = 0 # Pour anti-rebond
def set_color(color):
for i in range(NUM_LEDS):
np[i] = color
np.write()
def button_irq(pin):
global color_index, last_press
current_time = time.ticks_ms()
# Anti-rebond logiciel (200 ms)
if time.ticks_diff(current_time, last_press) > 200:
color_index = (color_index + 1) % len(colors)
set_color(colors[color_index])
last_press = current_time
# Déclenchement sur front descendant (appui)
button.irq(trigger=Pin.IRQ_FALLING, handler=button_irq)
# Couleur initiale
set_color(colors[color_index])
# Boucle principale libre
while True:
time.sleep(1)
"""
#******************************************************