import RPi.GPIO as GPIO import time from smbus import SMBus bus = SMBus(1) an0 = 0 led3 = 25 color_pw = 20 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(led3,GPIO.OUT) GPIO.setup(color_pw,GPIO.OUT) GPIO.output(led3,GPIO.LOW) GPIO.output(color_pw,GPIO.HIGH) def read_ain(i): global bus bus.write_byte(0x48, i) bus.read_byte(0x48) return bus.read_byte(0x48) print "CTRL+C = STOP program" try: while True: an0 = read_ain(1) val = 160 - an0 # PULL UP on 3.3V ledv = val + 70 # > 150 turn LED ON (50%) print str(an0) print "-" bus.write_byte_data(0x48, 0x40, ledv) # analog LED if an0 < 150: GPIO.output(led3,GPIO.HIGH) br3 = 0 else: GPIO.output(led3,GPIO.LOW) except KeyboardInterrupt: GPIO.cleanup() # must be executed before program stop