import time
from machine import ADC, Pin, PWM
# Pin definitions
led_pin = 27 # Assuming LED is connected to ADC pin 0
pot_last_value = -1
# Create ADC object
adc = ADC(Pin(26))
def getBrightness():
# Read potentiometer value (0-4095) and map it to 0-100
pot_value = adc.read_u16()
mapped_value = int((pot_value / 4095) * 100) # Map potentiometer value to 0-100 range
print("Mapped brightness:", mapped_value)
return mapped_value
def setup():
global pot_last_value
pwm = PWM(Pin(led_pin))
pwm.freq(1000) # Set PWM frequency to 1000 Hz
while True:
pot_current_value = adc.read_u16()
if pot_current_value != pot_last_value:
brightness = getBrightness()
duty_cycle = int((brightness / 100) * 65535)
pwm.duty_u16(duty_cycle) # Set duty cycle as a 16-bit value
pot_last_value = pot_current_value
time.sleep_ms(50) # Adjust delay as needed
if __name__ == '__main__':
setup()