from machine import Pin, PWM, ADC, I2C
from time import sleep
import lcd_api
import i2c_lcd
red_pin = PWM(Pin(13), freq=5000)
green_pin = PWM(Pin(12), freq=5000)
blue_pin = PWM(Pin(14), freq=5000)
pot_pin = ADC(Pin(34))
pot_pin.atten(ADC.ATTN_11DB)
i2c = I2C(1, scl=Pin(22), sda=Pin(21), freq=100000)
lcd = i2c_lcd.I2cLcd(i2c, 0x27, 2, 16) # Adjust I2C address if necessary
def set_rgb(r, g, b):
red_pin.duty(1023 - r)
green_pin.duty(1023 - g)
blue_pin.duty(1023 - b)
def get_color_from_pot_value(pot_value):
if pot_value < 146:
return 1023, 0, 0, "Red"
elif pot_value < 292:
return 0, 1023, 0, "Green"
elif pot_value < 438:
return 0, 0, 1023, "Blue"
elif pot_value < 584:
return 1023, 1023, 0, "Yellow"
elif pot_value < 730:
return 0, 1023, 1023, "Cyan"
elif pot_value < 876:
return 1023, 0, 1023, "Magenta"
else:
return 1023, 1023, 1023, "White"
while True:
pot_value = pot_pin.read()
pot_value_scaled = pot_value // 4
r, g, b, color_name = get_color_from_pot_value(pot_value_scaled)
# Set the RGB LED color
set_rgb(r, g, b)
# Display potentiometer value on LCD
lcd.clear()
lcd.putstr("Pot Value: {}\n".format(pot_value_scaled))
lcd.putstr("Color: {}".format(color_name))
# Add small delay to avoid flickering
sleep(0.2)