from machine import Pin, PWM, ADC
from time import sleep
# Define LED PWM pins and frequencies
red_led = PWM(Pin(18), freq=1000)
green_led = PWM(Pin(19), freq=1000)
blue_led = PWM(Pin(21), freq=1000)
# Initialize potentiometer pins with ADC
red_pot = ADC(Pin(32))
green_pot = ADC(Pin(33))
blue_pot = ADC(Pin(35))
# Set ADC width and attenuation for better range
red_pot.width(ADC.WIDTH_12BIT)
green_pot.width(ADC.WIDTH_12BIT)
blue_pot.width(ADC.WIDTH_12BIT)
red_pot.atten(ADC.ATTN_11DB) # Full range (0-3.3V)
green_pot.atten(ADC.ATTN_11DB)
blue_pot.atten(ADC.ATTN_11DB)
def set_color(red_value, green_value, blue_value):
red_led.duty(red_value)
green_led.duty(green_value)
blue_led.duty(blue_value)
while True:
# Read potentiometer values (0 - 4095) and map to PWM range (0 - 1023)
red_val = int((red_pot.read() / 4095) * 1023)
green_val = int((green_pot.read() / 4095) * 1023)
blue_val = int((blue_pot.read() / 4095) * 1023)
# Set LED color based on potentiometer values
set_color(red_val, green_val, blue_val)
# Short delay to avoid rapid changes
sleep(0.01)