ads1x15_gain_example.py
1 import time 2 import board 3 import busio 4 5 # import adafruit_ads1x15.ads1015 as ADS 6 import adafruit_ads1x15.ads1115 as ADS 7 from adafruit_ads1x15.analog_in import AnalogIn 8 9 # Create the I2C bus 10 i2c = busio.I2C(board.SCL, board.SDA) 11 12 # Create the ADS object 13 # ads = ADS.ADS1015(i2c) 14 ads = ADS.ADS1115(i2c) 15 16 # Create a sinlge ended channel on Pin 0 17 # Max counts for ADS1015 = 2047 18 # ADS1115 = 32767 19 chan = AnalogIn(ads, ADS.P0) 20 21 # The ADS1015 and ADS1115 both have the same gain options. 22 # 23 # GAIN RANGE (V) 24 # ---- --------- 25 # 2/3 +/- 6.144 26 # 1 +/- 4.096 27 # 2 +/- 2.048 28 # 4 +/- 1.024 29 # 8 +/- 0.512 30 # 16 +/- 0.256 31 # 32 gains = (2 / 3, 1, 2, 4, 8, 16) 33 34 while True: 35 ads.gain = gains[0] 36 print("{:5} {:5.3f}".format(chan.value, chan.voltage), end="") 37 for gain in gains[1:]: 38 ads.gain = gain 39 print(" | {:5} {:5.3f}".format(chan.value, chan.voltage), end="") 40 print() 41 time.sleep(0.5)