from machine import Pin, PWM, ADC
from time import sleep
UP = "\x1B[3A"
CLR = "\x1B[OK"
#Configure ADC
adc = ADC(Pin(2))
adc.atten(ADC.ATTN_11DB) #set max input voltage to 3.6v
adc.width(ADC.WIDTH_10BIT) #set signal width to 12bits
pwm0 = PWM(Pin(21))
#Convertion as potiometer is changed
while True:
adc_val = adc.read()
volt_in = (3.3*adc_val)/1023
range1 = range(0,1)
range2 = range(1,2)
range3 = range(2,3)
range4 = range(3,4)
if volt_in in range1:
freq =2000
duty = 256
duty_perc = (duty/1023)*100
print(UP,"The input voltage is {:0.2f} volts".format(volt_in),CLR,"\n", "The frequency is{} Hz".format(freq),CLR,
"with a duty cycle of {:d}%".format(int(duty_perc)),CLR, "\n")
break
if volt_in in range2:
freq = 5000
duty = 767
duty_perc = (duty/1023)*100
print(UP,"The input voltage is {:0.2f} volts".format(volt_in),CLR,"\n", "The frequency is{} Hz".format(freq),CLR,
"with a duty cycle of {:d}%".format(int(duty_perc)),CLR, "\n")
break
if volt_in in range3:
freq = 60000
duty = 512
duty_perc = (duty/1023)*100
print(UP,"The input voltage is {:0.2f} volts".format(volt_in),CLR,"\n", "The frequency is{} Hz".format(freq),CLR,
"with a duty cycle of {:d}%".format(int(duty_perc)),CLR, "\n")
break
if volt_in in range4:
freq = 5
duty = 205
duty_perc = (duty/1023)*100
print(UP,"The input voltage is {:0.2f} volts".format(volt_in),CLR,"\n", "The frequency is{} Hz".format(freq),CLR,
"with a duty cycle of {:d}%".format(int(duty_perc)),CLR, "\n")
break