11#!/usr/bin/python
22
33import time
4+ import smbus
45from Adafruit_I2C import Adafruit_I2C
56
67# ===========================================================================
@@ -58,6 +59,15 @@ class ADS1x15:
5859 __ADS1015_REG_CONFIG_DR_2400SPS = 0x00A0 # 2400 samples per second
5960 __ADS1015_REG_CONFIG_DR_3300SPS = 0x00C0 # 3300 samples per second
6061
62+ __ADS1115_REG_CONFIG_DR_8SPS = 0x0000 # 8 samples per second
63+ __ADS1115_REG_CONFIG_DR_16SPS = 0x0020 # 16 samples per second
64+ __ADS1115_REG_CONFIG_DR_32SPS = 0x0040 # 32 samples per second
65+ __ADS1115_REG_CONFIG_DR_64SPS = 0x0060 # 64 samples per second
66+ __ADS1115_REG_CONFIG_DR_128SPS = 0x0080 # 128 samples per second
67+ __ADS1115_REG_CONFIG_DR_250SPS = 0x00A0 # 250 samples per second (default)
68+ __ADS1115_REG_CONFIG_DR_475SPS = 0x00C0 # 475 samples per second
69+ __ADS1115_REG_CONFIG_DR_860SPS = 0x00E0 # 860 samples per second
70+
6171 __ADS1015_REG_CONFIG_CMODE_MASK = 0x0010
6272 __ADS1015_REG_CONFIG_CMODE_TRAD = 0x0000 # Traditional comparator with hysteresis (default)
6373 __ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010 # Window comparator
@@ -78,6 +88,11 @@ class ADS1x15:
7888
7989 # Constructor
8090 def __init__ (self , address = 0x48 , ic = __IC_ADS1015 , debug = False ):
91+ # Depending on if you have an old or a new Raspberry Pi, you
92+ # may need to change the I2C bus. Older Pis use SMBus 0,
93+ # whereas new Pis use SMBus 1. If you see an error like:
94+ # 'Error accessing 0x48: Check your I2C address '
95+ # change the SMBus number in the initializer below!
8196 self .i2c = Adafruit_I2C (address )
8297 self .address = address
8398 self .debug = debug
@@ -98,16 +113,24 @@ def readADCSingleEnded(self, channel=0):
98113 print "ADS1x15: Invalid channel specified: %d" % channel
99114 return - 1
100115
101- # Start with default values
102- config = self .__ADS1015_REG_CONFIG_CQUE_NONE | \
103- self .__ADS1015_REG_CONFIG_CLAT_NONLAT | \
104- self .__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
105- self .__ADS1015_REG_CONFIG_CMODE_TRAD | \
106- self .__ADS1015_REG_CONFIG_DR_1600SPS | \
107- self .__ADS1015_REG_CONFIG_MODE_SINGLE
116+ # Set default values
117+ if (self .ic == self .__IC_ADS1015 ):
118+ config = self .__ADS1015_REG_CONFIG_CQUE_NONE | \
119+ self .__ADS1015_REG_CONFIG_CLAT_NONLAT | \
120+ self .__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
121+ self .__ADS1015_REG_CONFIG_CMODE_TRAD | \
122+ self .__ADS1015_REG_CONFIG_DR_1600SPS | \
123+ self .__ADS1015_REG_CONFIG_MODE_SINGLE
124+ else :
125+ config = self .__ADS1015_REG_CONFIG_CQUE_NONE | \
126+ self .__ADS1015_REG_CONFIG_CLAT_NONLAT | \
127+ self .__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
128+ self .__ADS1015_REG_CONFIG_CMODE_TRAD | \
129+ self .__ADS1115_REG_CONFIG_DR_250SPS | \
130+ self .__ADS1015_REG_CONFIG_MODE_SINGLE
108131
109- # Set PGA/voltage range (1 bit = 3mV using the default range )
110- config |= self .__ADS1015_REG_CONFIG_PGA_6_144V # +/- 6.144V range
132+ # Set PGA/voltage range to max value (+/- 6.144V )
133+ config |= self .__ADS1015_REG_CONFIG_PGA_6_144V
111134
112135 if channel == 3 :
113136 config |= self .__ADS1015_REG_CONFIG_MUX_SINGLE_3
@@ -126,17 +149,25 @@ def readADCSingleEnded(self, channel=0):
126149 self .i2c .writeList (self .__ADS1015_REG_POINTER_CONFIG , bytes )
127150
128151 # Wait for the ADC conversion to complete
129- time .sleep (0.001 )
152+ if (self .ic == self .__IC_ADS1015 ):
153+ time .sleep (0.001 )
154+ else :
155+ time .sleep (0.008 )
130156
131157 # Read the conversion results
132158 result = self .i2c .readList (self .__ADS1015_REG_POINTER_CONVERT , 2 )
133159 if (self .ic == self .__IC_ADS1015 ):
134- # Shift right 4 bits for the 12-bit ADS1015
135- return ( ((result [0 ] << 8 ) | (result [1 ] & 0xFF )) >> 4 )
160+ # Shift right 4 bits for the 12-bit ADS1015
161+ return ( ((result [0 ] << 8 ) | (result [1 ] & 0xFF )) >> 4 )
136162 else :
137- # Return 16-bit value for the ADS1115
138- return ( (result [0 ] << 8 ) | (result [1 ] & 0xFF ) )
139-
163+ # Return 16-bit value for the ADS1115
164+ # (Take signed values into account as well)
165+ val = (result [0 ] << 8 ) | (result [1 ])
166+ if val > 0x7FFF :
167+ return val - 0xFFFF
168+ else :
169+ return (result [0 ] << 8 ) | (result [1 ])
170+
140171 def readADCDifferential01 (self ):
141172 "Gets a differential ADC reading from channels 0 and 1"
142173
@@ -148,4 +179,3 @@ def startSingleEndedComparator(self, channel, threshold):
148179
149180 def getLastConversionResults (self ):
150181 "Returns the last ADC conversion result"
151-
0 commit comments