from machine import Pin, PWM
import time
# Initialize PWM for each color channel of the RGB LED
red = PWM(Pin(15), freq=1000) # Red channel on GPIO 15
green = PWM(Pin(2), freq=1000) # Green channel on GPIO 2
blue = PWM(Pin(4), freq=1000) # Blue channel on GPIO 4
# Function to set RGB LED color
def set_color(r, g, b):
"""
Set the color of the RGB LED.
:param r: Red intensity (0-1023)
:param g: Green intensity (0-1023)
:param b: Blue intensity (0-1023)
"""
red.duty(r)
green.duty(g)
blue.duty(b)
# Function to gradually change color
def color_transition():
while True:
# Gradually transition through colors (Red -> Green -> Blue)
for i in range(1024):
set_color(i, 0, 0) # Fade Red in
time.sleep(0.01)
for i in range(1024):
set_color(1023 - i, i, 0) # Red to Green transition
time.sleep(0.01)
for i in range(1024):
set_color(0, 1023 - i, i) # Green to Blue transition
time.sleep(0.01)
for i in range(1024):
set_color(i, 0, 1023 - i) # Blue to Red transition
time.sleep(0.01)
# Main function
if __name__ == '__main__':
color_transition() # Start color transition effect