Programmer une LED RGB

Le montage

R : GP14 :

Com : GND

G : GP13

B : GP12

Avec Picozero

"""
LED RGB
R sur GP14
G sur GP13
B sur GP12'''  '''
"""

from picozero import RGBLED
from time import sleep

rgb = RGBLED(red = 14, green = 13, blue = 12)

rgb.blink(on_times=(1,0.5,2), colors=((255,0,0),(0,255,0),(0,0,255)), wait=True, n=3)
rgb.off()

# for k in range(3):
#     rgb.color = (255, 0, 0)
#     sleep(1)
#     rgb.color = (0, 255, 0)
#     sleep(0.5)
#     rgb.color = (0, 0, 255)
#     sleep(2)
# rgb.off()

Sans Picozero

Il faut transformer les pattes en PWM.

Pour simplifier l’écriture des niveaux de couleurs, on peut se servir d’une classe.

from machine import Pin,PWM
from time import sleep

class ledPWM(PWM):
    def __init__(self, pin: Pin):
        super().__init__(pin)
    def color(self,val):
        super().duty_u16(65535*val//255)
        
def changer_couleurs(cols):
    led_red.color(cols[0])
    led_green.color(cols[1])
    led_blue.color(cols[2])

led_red = ledPWM(Pin(14))
led_green = ledPWM(Pin(13))
led_blue = ledPWM(Pin(12))


for k in range(3):
    changer_couleurs((80,120,0))
    sleep(0.5)
    changer_couleurs((100,0,120))               
    sleep(0.5)
    changer_couleurs((0,150,255))               
    sleep(0.5)
    
led_red.color(0)
led_green.color(0)
led_blue.color(0)