from machine import Pin
from time import sleep
# Button inputs (connected to GND, so use pull-up resistors)
sb = Pin(0, Pin.IN, Pin.PULL_UP) # Blue button (Pin 0)
sg = Pin(1, Pin.IN, Pin.PULL_UP) # Green button (Pin 1)
sr = Pin(2, Pin.IN, Pin.PULL_UP) # Red button (Pin 2)
# RGB LED outputs (common cathode)
r = Pin(20, Pin.OUT) # Red LED (Pin 20)
g = Pin(19, Pin.OUT) # Green LED (Pin 19)
b = Pin(18, Pin.OUT) # Blue LED (Pin 18)
while True:
# Read the button states
vb = sb.value() # Blue button state
vg = sg.value() # Green button state
vr = sr.value() # Red button state
# Turn off all LEDs initially
r.off()
g.off()
b.off()
# Check which button is pressed and light up the corresponding LED
if not vb: # Button pressed, pin pulled LOW
print("blue")
b.on() # Blue LED on
elif not vg: # Button pressed, pin pulled LOW
print("green")
g.on() # Green LED on
elif not vr: # Button pressed, pin pulled LOW
print("red")
r.on() # Red LED on
sleep(0.1)