Saturday, 4 March 2023

TTB05: Reading Analog Voltages Using a Potentiometer

import machine from time import sleep potPin = 28 myPot = machine.ADC(potPin) while True: potVal=myPot.read_u16() #print('PotVal =',potVal) #max=65535, min=1024, middle=65535-1024=64511 voltage = (3.3/64511)*potVal - (1024*3.3/64511) #print('Voltage =',voltage) percentage = (100/64511)*potVal-(1024*100/64511) #print('Percentage = ',int(percentage)) print('PotVal =',potVal, 'Voltage =',voltage, 'Percentage = ',int(percentage)) sleep(1) #https://www.youtube.com/watch?v=ODWwErH_iGA&list=PLGs0VKk2DiYz8js1SJog21cDhkBqyAhC5&index=5

No comments: