import RPi.GPIO as GPIO import time from smbus import SMBus bus = SMBus(1) an0 = 0 led1 = 23 led2 = 22 led3 = 25 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(led1,GPIO.OUT) GPIO.setup(led2,GPIO.OUT) GPIO.setup(led3,GPIO.OUT) GPIO.output(led1,GPIO.LOW) GPIO.output(led2,GPIO.LOW) GPIO.output(led3,GPIO.LOW) 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(0) val = 160 - an0 # PULL UP on 3.3V ledv = val + 70 # > 150 turn LED ON (50%) print str(an0)+" - "+str(val) print "-" bus.write_byte_data(0x48, 0x40, ledv) # analog LED if val > 30 and val < 70: GPIO.output(led1,GPIO.HIGH) else: GPIO.output(led1,GPIO.LOW) if val > 69 and val < 110: GPIO.output(led2,GPIO.HIGH) else: GPIO.output(led2,GPIO.LOW) if val > 109: GPIO.output(led3,GPIO.HIGH) else: GPIO.output(led3,GPIO.LOW) except KeyboardInterrupt: GPIO.cleanup() # must be executed before program stop