# this is to give an PWM analog output as per user's input from 0 to 3.3 v. The program changes volts to a range between 0-65535
from machine import Pin, PWM
from time import sleep
outPin=15 #GPIO Pin number not the physical pin number
# use following formula to calculate any analog input for calculation myPotVal is the reading for Pico pin
potMin=0 #X1 , lowest resistance value
potMax=65535 #X2 , highest resistance value
valueMin=0 #this is the lowest value to read in the range - Y1
valueMax=3.3 #this is the higest value to read in the range, change to 100 for percentage - Y2
# slope of the line m= (Y2-Y1)/(X2-X1)
slopeM= (valueMax-valueMin)/(potMax-potMin)
#myVolts=slopeM*(potVal-potMin) + valueMin , in this potVal is the input from pot (0-655535) & myVal is the voltage or %outPin=16
analogOut=PWM(Pin(outPin)) #makes outpin as analogOut PWM pin
analogOut.freq(1000) #fixes output frequency
analogOut.duty_u16(0) #fixes duty cycle to 0% means no light or no output
while True:
myVolts=float(input('what voltage you want? ')) #ask for user input
PWMval=slopeM*(myVolts-potMin) + valueMin # changes volts to the input value between 0-65535
print(PWMval)
analogOut.duty_u16(int(PWMval)) #output PMW value to analog pin 16