Tiny 2040 RGB LED control - Tutorial

This example was extremely useful for getting bootstrapped with the RGB LED on the Tiny. I primarily use CircuitPython (not sure why, just used to it I guess), so I ported it over to use CircuitPython libs. Figured I’d post it here in case anyone else was in the same boat.

# Tiny 2040 RBG LED control
# Original Micro Python code by Tony Goodhew 11th March 2021
# Ported to Circuit Python

import time
import board
import pwmio

#Setup RGB LED
# Construct PWM objects with RGB LED
rpwm = pwmio.PWMOut(board.LED_R, frequency=1000) # RED
gpwm = pwmio.PWMOut(board.LED_G, frequency=1000) # GREEN
bpwm = pwmio.PWMOut(board.LED_B, frequency=1000) # BLUE

# Turn off
rduty = 65535
gduty = 65535
bduty = 65535
rpwm.duty_cycle = rduty
gpwm.duty_cycle = gduty
bpwm.duty_cycle = bduty

def LED(r,g,b):
    rduty = int(65535 -(65535 * r/255))
    gduty = int(65535 -(65535 * g/255))
    bduty = int(65535 -(65535 * b/255))
#    print(rduty)
#    print(gduty)
#    print(bduty)
    rpwm.duty_cycle = rduty
    gpwm.duty_cycle = gduty
    bpwm.duty_cycle = bduty

LED(255,255,255)
time.sleep(0.3)
LED(255,0,0)
time.sleep(0.3)

# Blink
for i in range(4):
    LED(0,0,255)
    time.sleep(0.3)
    LED(0,0,0)
    time.sleep(0.3)
# Fade UP
for i in range(255):
    LED(i,i,0)
    time.sleep(0.01)
# Fade DOWN
for ii in range(255,-1,-1):
    LED(ii,ii,0)
    time.sleep(0.01)

2 Likes