#############################################################
# Varry LED Brightness using Potentiometer #
#############################################################
#
# LED Brightness Control uing Potentiometer on Raspberry Pi Pico with MicroPython
#
# Check out the link for Code explanation and Hardware details
# Link:
# http://tech.arunkumarn.in/blogs/raspberry-pi-pico/
#
# duty cycle value in raspberry pi pico is 16 bit data.
# minimum value will be 0 for 0% duty cycle
# maximum value will be 65535 for 100% duty cycle
from machine import Pin, PWM, ADC
from time import sleep_ms
# Led is connected to GPIO 2
# setting PWM frequency to 1k Hz and duty cycle to 0
led = PWM(2, freq=1000, duty_u16=0)
# Potentiometer is connected to GPIO 26 (ADC 0)
control = ADC(26)
while True:
# Read the position of Potentiometer
brightness = control.read_u16()
# Set PWM value according to the POT position
led.duty_u16(brightness)
sleep_ms(100)
LED
470 Ω
Potentiometer