#########################################
# Varry LED Brightness #
#########################################
#
# LED Brightness Control on Raspberry Pi Pico with MicroPython & PWM
#
# Check out the link for Code explanation and Hardware details
# Link:
# http://tech.arunkumarn.in/blogs/raspberry-pi-pico/led-brightness-control-on-raspberry-pi-pico-with-micropython-and-pwm/
#
# 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
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)
while True:
# Increasing the duty cyle value from 0% to 100%
for i in range(65536):
led.duty_u16(i)
sleep_ms(1000)
# Decreasing the duty cyle value from 100% to 0%
for i in range(65535, -1, -1):
led.duty_u16(i)
sleep_ms(1000)
LED
Raspberry Pi Pico
GND
Resistor
470 Ω