from machine import Pin, PWM,ADC
from time import sleep
pwm0 = PWM(Pin(21))
pwm0.freq(50)
adc = ADC(Pin(32))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)
def map(x, in_min, in_max, out_min, out_max):
return int((x - in_min)*(out_max - out_min) / (in_max - in_min)+out_min)
while True:
for step in range(25, 125):
pwm0.duty(step)
sleep(0.1)
for step in range(125, 25, -1):
pwm0.duty(step)
sleep(0.1)
adc_value = adc.read()
pwm0.duty(map(adc_value , 0, 4095, 25, 125))
volts = (3.3* adc_value)/4095
input_voltage = " The input voltage is {:0.2f} volts and raw value is {:4d}"
print(input_voltage.format(volts, adc_value), end = "\r")
if t<0 and t >0.5:
pwm0.duty(step)
elif t<0.5 and t >1:
pwm0.duty(306)
elif t <1 and t >1.5:
pwm0.duty(767)
else:
pwm0.freq(50)
pwm0.duty(511)