# Control de brillo del LED utilizando ADC en la Pi Pico
from machine import Pin, PWM, ADC
from utime import sleep
# create PWM object from a pin and set the frequency of slice 0
# and duty cycle for channel A
pwm0 = PWM(Pin(5), freq=2000, duty_u16=32768)
print("frecuencia: " + str(pwm0.freq())) # get the current frequency of slice 0
pwm0.freq(1000) # set/change the frequency of slice 0
print("frecuencia: " + str(pwm0.freq())) # get the current frequency of slice 0
print("frecuencia: " + str(pwm0.duty_u16()))
pwm0.duty_u16(200) # set the duty cycle of channel A, range 0-65535
print("frecuencia: " + str(pwm0.duty_u16()))
pwm0.duty_u16(0) # stop the output at channel A
print("frecuencia: " + str(pwm0.duty_u16()))
print(pwm0) # show the properties of the PWM object.
pwm0.deinit() # turn off PWM of slice 0, stopping channels A and B
print(pwm0) # show the properties of the PWM object.
# Bucle infinito para leer el valor del ADC y ajustar el brillo del LED
while True:
pwm0.duty_u16(10000) # Ajusta el ciclo de trabajo del PWM según el valor leído
sleep(0.01) # Espera 0.01 segundos antes de la siguiente lectura