"""
#1
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
#1
"""
#*******************************
# ✅ Code MicroPython minimal
# Le Pico possède le module neopixel intégré 👍
# Matrice de 64 leds (8x8)
###
#2
import machine, neopixel
np = neopixel.NeoPixel(machine.Pin(16), 64) # 64 LED pour 8x8
np[0] = (255, 0, 0) # Allume la première LED en rouge
from machine import Pin
import neopixel
import time
PIN = 16 # GPIO utilisé
NB =64 # nombre de LEDs
np = neopixel.NeoPixel(Pin(PIN), NB) # objet neopixel class NeoPixel
# allumer la première LED en rouge
np[0] = (255, 0, 0) # 0>1er led,1>2ieme led etc
np.write()
time.sleep(1)
# tout éteindre
for i in range(NB):
np[i] = (0,0,0)
np.write()
#2
"""
#**********************
# 🧠 Code MicroPython basique
# MicroPython possède le module neopixel.
# Exemple : faire clignoter toutes les LEDs
"""
#3
from machine import Pin
import neopixel
import time
PIN = 16 # GPIO utilisé
NUM_LEDS =64 # nombre de LEDs
np = neopixel.NeoPixel(Pin(PIN), NUM_LEDS) # objet neopixel class NeoPixel
def clear():
for i in range(NUM_LEDS):
np[i] = (0, 0, 0)
np.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)
#3
"""
#*****************************************************
"""
#4
# 🎨 Exemple : effet arc-en-ciel
from machine import Pin
import neopixel
import time
time.sleep(.020)
PIN = 16 # GPIO utilisé
NUM_LEDS =64 # nombre de LEDs
np = neopixel.NeoPixel(Pin(PIN), NUM_LEDS) # objet neopixel class NeoPixel
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)
#4
"""
#*************************************************
"""
#5
# ✅ 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 = 64
LED_PIN = 16 # GP16
BUTTON_PIN = 15
# ----- 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]) # call fonction set_color
# ----- BOUCLE PRINCIPALE -----
while True:
if button.value() == 0: # Bouton appuyé
time.sleep(0.2) # Anti-rebond simple
print(color_index,'avant')
color_index = (color_index + 1) % len(colors)
print(len(colors))
print(color_index,'apres')
set_color(colors[color_index])
# Attendre relâchement
while button.value() == 0:
time.sleep(.020)
pass
#5
"""
#*******************************************************
"""
#6
# 💻 Code avec interruption (IRQ)
from machine import Pin
import neopixel
import time
# ----- CONFIGURATION -----
NUM_LEDS = 8
LED_PIN =16
BUTTON_PIN = 15
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) > 100:
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:
print('p')
time.sleep(1)
#6
"""
#******************************************************