import RPi.GPIO as GPIO import time from smbus import SMBus bus = SMBus(1) an0 = 0 an0 = 1 leda = 20 ledb = 21 irpow = 23 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(leda,GPIO.OUT) GPIO.setup(ledb,GPIO.OUT) GPIO.setup(irpow,GPIO.OUT) GPIO.output(leda,GPIO.LOW) GPIO.output(ledb,GPIO.LOW) GPIO.output(irpow,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(0) an1 = read_ain(1) print str(an0)+" - "+str(an1) val = 160 - an0 # PULL UP on 3.3V ledv = val + 70 # > 150 turn LED ON (50%) bus.write_byte_data(0x48, 0x40, ledv) # analog LED if an0 > 100: GPIO.output(ledb,GPIO.HIGH) else: GPIO.output(ledb,GPIO.LOW) if an1 > 100: GPIO.output(leda,GPIO.HIGH) else: GPIO.output(leda,GPIO.LOW) except KeyboardInterrupt: GPIO.cleanup() # must be executed before program stop