from machine import Pin, ADC, PWM
import time
# Define pins
LED1_PIN = Pin(0, Pin.OUT) # GP0
LED2_PIN = Pin(1, Pin.OUT) # GP1
LED3_PIN = Pin(2, Pin.OUT) # GP2
# RGB LED pins with PWM
RGB_R_PIN = PWM(Pin(3))
RGB_G_PIN = PWM(Pin(4))
RGB_B_PIN = PWM(Pin(5))
# Potentiometer on analog input
POT_PIN = ADC(26) # GP26 is an ADC pin
# Initialize PWM frequency for RGB LED
RGB_R_PIN.freq(1000)
RGB_G_PIN.freq(1000)
RGB_B_PIN.freq(1000)
# Main loop
while True:
# Read potentiometer value and scale to match Arduino's 0-1023 range
pot_value = int(POT_PIN.read_u16() * 1023 / 65535)
# Based on potentiometer value, control LEDs and RGB LED
if pot_value <= 341:
# First range - Red
LED1_PIN.value(1)
LED2_PIN.value(0)
LED3_PIN.value(0)
RGB_R_PIN.duty_u16(65535) # Full brightness (equivalent to 255 in Arduino)
RGB_G_PIN.duty_u16(0)
RGB_B_PIN.duty_u16(0)
elif pot_value <= 682:
# Second range - Green
LED1_PIN.value(0)
LED2_PIN.value(1)
LED3_PIN.value(0)
RGB_R_PIN.duty_u16(0)
RGB_G_PIN.duty_u16(65535)
RGB_B_PIN.duty_u16(0)
else:
# Third range - Blue
LED1_PIN.value(0)
LED2_PIN.value(0)
LED3_PIN.value(1)
RGB_R_PIN.duty_u16(0)
RGB_G_PIN.duty_u16(0)
RGB_B_PIN.duty_u16(65535)
time.sleep(0.01) # Small delay to prevent CPU saturation
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
rgb1:R
rgb1:COM
rgb1:G
rgb1:B
pot1:VCC
pot1:SIG
pot1:GND