def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
r eturn value
def analogWrite(value):
bus. write_byte_data(address,cmd,value)
def setup():
g lobal p
GPIO. setmode(GPIO.BOARD)
GPIO. setup(ledPin,GPIO.OUT)
GPIO. output(ledPin,GPIO.LOW)
p = GPIO. PWM(ledPin,1000)
p. start(0)
def loop():
w hile Tr ue:
value = analogRead(0) #read A0 pin
p. ChangeDutyCycle(value*100/255) #Convert ADC value to duty cycle of PWM
voltage = value / 255.0 * 3.3 #calculate voltage
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
def destroy():
bus. close()
GPIO. cleanup()
if __name__ == '__main__':
p rint ('Program is starting ... ')
setup()
try:
loop()
e xcept KeyboardInterrupt:
destroy()