1
+ import time
2
+ import smbus
3
+
4
+ __all__ = ['ADS1115' ]
5
+
6
+ from I2C import Adafruit_I2C
7
+
8
+ # ===========================================================================
9
+ # ADS1115 Class
10
+ # ===========================================================================
11
+ class ADS1115 :
12
+ i2c = None
13
+
14
+ # IC Identifiers
15
+ __IC_ADS1115 = 0x01
16
+
17
+ # Config Register
18
+ __ADS1115_REG_CONFIG_DR_8SPS = 0x0000 # 8 samples per second
19
+ __ADS1115_REG_CONFIG_DR_16SPS = 0x0020 # 16 samples per second
20
+ __ADS1115_REG_CONFIG_DR_32SPS = 0x0040 # 32 samples per second
21
+ __ADS1115_REG_CONFIG_DR_64SPS = 0x0060 # 64 samples per second
22
+ __ADS1115_REG_CONFIG_DR_128SPS = 0x0080 # 128 samples per second
23
+ __ADS1115_REG_CONFIG_DR_250SPS = 0x00A0 # 250 samples per second (default)
24
+ __ADS1115_REG_CONFIG_DR_475SPS = 0x00C0 # 475 samples per second
25
+ __ADS1115_REG_CONFIG_DR_860SPS = 0x00E0 # 860 samples per second
26
+
27
+ __ADS1015_REG_CONFIG_CQUE_MASK = 0x0003
28
+ __ADS1015_REG_CONFIG_CQUE_1CONV = 0x0000 # Assert ALERT/RDY after one conversions
29
+ __ADS1015_REG_CONFIG_CQUE_2CONV = 0x0001 # Assert ALERT/RDY after two conversions
30
+ __ADS1015_REG_CONFIG_CQUE_4CONV = 0x0002 # Assert ALERT/RDY after four conversions
31
+ __ADS1015_REG_CONFIG_CQUE_NONE = 0x0003 # Disable the comparator and put ALERT/RDY in high state (default)
32
+
33
+ __ADS1015_REG_CONFIG_CMODE_MASK = 0x0010
34
+ __ADS1015_REG_CONFIG_CMODE_TRAD = 0x0000 # Traditional comparator with hysteresis (default)
35
+ __ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010 # Window comparator
36
+
37
+ __ADS1015_REG_CONFIG_CPOL_MASK = 0x0008
38
+ __ADS1015_REG_CONFIG_CPOL_ACTVLOW = 0x0000 # ALERT/RDY pin is low when active (default)
39
+ __ADS1015_REG_CONFIG_CPOL_ACTVHI = 0x0008 # ALERT/RDY pin is high when active
40
+
41
+ __ADS1015_REG_CONFIG_CLAT_MASK = 0x0004 # Determines if ALERT/RDY pin latches once asserted
42
+ __ADS1015_REG_CONFIG_CLAT_NONLAT = 0x0000 # Non-latching comparator (default)
43
+ __ADS1015_REG_CONFIG_CLAT_LATCH = 0x0004 # Latching comparator
44
+
45
+ __ADS1015_REG_CONFIG_MODE_MASK = 0x0100
46
+ __ADS1015_REG_CONFIG_MODE_CONTIN = 0x0000 # Continuous conversion mode
47
+ __ADS1015_REG_CONFIG_MODE_SINGLE = 0x0100 # Power-down single-shot mode (default)
48
+
49
+ __ADS1015_REG_CONFIG_PGA_MASK = 0x0E00
50
+ __ADS1015_REG_CONFIG_PGA_6_144V = 0x0000 # +/-6.144V range
51
+ __ADS1015_REG_CONFIG_PGA_4_096V = 0x0200 # +/-4.096V range
52
+ __ADS1015_REG_CONFIG_PGA_2_048V = 0x0400 # +/-2.048V range (default)
53
+ __ADS1015_REG_CONFIG_PGA_1_024V = 0x0600 # +/-1.024V range
54
+ __ADS1015_REG_CONFIG_PGA_0_512V = 0x0800 # +/-0.512V range
55
+ __ADS1015_REG_CONFIG_PGA_0_256V = 0x0A00 # +/-0.256V range
56
+
57
+ __ADS1015_REG_CONFIG_MUX_MASK = 0x7000
58
+ __ADS1015_REG_CONFIG_MUX_DIFF_0_1 = 0x0000 # Differential P = AIN0, N = AIN1 (default)
59
+ __ADS1015_REG_CONFIG_MUX_DIFF_0_3 = 0x1000 # Differential P = AIN0, N = AIN3
60
+ __ADS1015_REG_CONFIG_MUX_DIFF_1_3 = 0x2000 # Differential P = AIN1, N = AIN3
61
+ __ADS1015_REG_CONFIG_MUX_DIFF_2_3 = 0x3000 # Differential P = AIN2, N = AIN3
62
+ __ADS1015_REG_CONFIG_MUX_SINGLE_0 = 0x4000 # Single-ended AIN0
63
+ __ADS1015_REG_CONFIG_MUX_SINGLE_1 = 0x5000 # Single-ended AIN1
64
+ __ADS1015_REG_CONFIG_MUX_SINGLE_2 = 0x6000 # Single-ended AIN2
65
+ __ADS1015_REG_CONFIG_MUX_SINGLE_3 = 0x7000 # Single-ended AIN3
66
+
67
+ # Config Register
68
+ __ADS1015_REG_CONFIG_OS_MASK = 0x8000
69
+ __ADS1015_REG_CONFIG_OS_SINGLE = 0x8000 # Write: Set to start a single-conversion
70
+ __ADS1015_REG_CONFIG_OS_BUSY = 0x0000 # Read: Bit = 0 when conversion is in progress
71
+ __ADS1015_REG_CONFIG_OS_NOTBUSY = 0x8000 # Read: Bit = 1 when device is not performing a conversion
72
+
73
+ # Pointer Register
74
+ __ADS1015_REG_POINTER_MASK = 0x03
75
+ __ADS1015_REG_POINTER_CONVERT = 0x00
76
+ __ADS1015_REG_POINTER_CONFIG = 0x01
77
+ __ADS1015_REG_POINTER_LOWTHRESH = 0x02
78
+ __ADS1015_REG_POINTER_HITHRESH = 0x03
79
+
80
+ # Dictionaries with the sampling speed values
81
+ # These simplify and clean the code (avoid the abuse of if/elif/else clauses)
82
+ spsADS1115 = {
83
+ 8 :__ADS1115_REG_CONFIG_DR_8SPS ,
84
+ 16 :__ADS1115_REG_CONFIG_DR_16SPS ,
85
+ 32 :__ADS1115_REG_CONFIG_DR_32SPS ,
86
+ 64 :__ADS1115_REG_CONFIG_DR_64SPS ,
87
+ 128 :__ADS1115_REG_CONFIG_DR_128SPS ,
88
+ 250 :__ADS1115_REG_CONFIG_DR_250SPS ,
89
+ 475 :__ADS1115_REG_CONFIG_DR_475SPS ,
90
+ 860 :__ADS1115_REG_CONFIG_DR_860SPS
91
+ }
92
+
93
+ # Dictionariy with the programable gains
94
+ pgaADS1x15 = {
95
+ 6144 :__ADS1015_REG_CONFIG_PGA_6_144V ,
96
+ 4096 :__ADS1015_REG_CONFIG_PGA_4_096V ,
97
+ 2048 :__ADS1015_REG_CONFIG_PGA_2_048V ,
98
+ 1024 :__ADS1015_REG_CONFIG_PGA_1_024V ,
99
+ 512 :__ADS1015_REG_CONFIG_PGA_0_512V ,
100
+ 256 :__ADS1015_REG_CONFIG_PGA_0_256V
101
+ }
102
+
103
+ # Constructor
104
+ def __init__ (self , address = 0x48 , ic = __IC_ADS1115 , debug = False ):
105
+ self .i2c = Adafruit_I2C (address )
106
+ self .address = address
107
+ self .debug = debug
108
+
109
+ # Make sure the IC specified is valid
110
+ if (ic > self .__IC_ADS1115 ):
111
+ return - 1
112
+ else :
113
+ self .ic = ic
114
+
115
+ # Set pga value, so that getLastConversionResult() can use it,
116
+ # any function that accepts a pga value must update this.
117
+ self .pga = 6144
118
+
119
+ def readADCSingleEnded (self , channel = 0 , pga = 6144 , sps = 250 ):
120
+ '''
121
+ Gets a single-ended ADC reading from the specified channel in mV.
122
+ The sample rate for this mode (single-shot) can be used to lower the noise
123
+ (low sps) or to lower the power consumption (high sps) by duty cycling,
124
+ see datasheet page 14 for more info.
125
+ The pga must be given in mV, see page 13 for the supported values.
126
+ '''
127
+
128
+ # With invalid channel return -1
129
+ if (channel > 3 ):
130
+ return - 1
131
+
132
+ # Disable comparator, Non-latching, Alert/Rdy active low
133
+ # traditional comparator, single-shot mode
134
+ config = self .__ADS1015_REG_CONFIG_CQUE_NONE | \
135
+ self .__ADS1015_REG_CONFIG_CLAT_NONLAT | \
136
+ self .__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
137
+ self .__ADS1015_REG_CONFIG_CMODE_TRAD | \
138
+ self .__ADS1015_REG_CONFIG_MODE_SINGLE
139
+
140
+ # Set sample per seconds, defaults to 250sps
141
+ config |= self .spsADS1115 .setdefault (sps , self .__ADS1115_REG_CONFIG_DR_250SPS )
142
+
143
+ # Set PGA/voltage range, defaults to +-6.144V
144
+ config |= self .pgaADS1x15 .setdefault (pga , self .__ADS1015_REG_CONFIG_PGA_6_144V )
145
+ self .pga = pga
146
+
147
+ # Set the channel to be converted
148
+ if channel == 3 :
149
+ config |= self .__ADS1015_REG_CONFIG_MUX_SINGLE_3
150
+ elif channel == 2 :
151
+ config |= self .__ADS1015_REG_CONFIG_MUX_SINGLE_2
152
+ elif channel == 1 :
153
+ config |= self .__ADS1015_REG_CONFIG_MUX_SINGLE_1
154
+ else :
155
+ config |= self .__ADS1015_REG_CONFIG_MUX_SINGLE_0
156
+
157
+ # Set 'start single-conversion' bit
158
+ config |= self .__ADS1015_REG_CONFIG_OS_SINGLE
159
+
160
+ # Write config register to the ADC
161
+ bytes = [(config >> 8 ) & 0xFF , config & 0xFF ]
162
+ self .i2c .writeList (self .__ADS1015_REG_POINTER_CONFIG , bytes )
163
+
164
+ # Wait for the ADC conversion to complete
165
+ # The minimum delay depends on the sps: delay >= 1/sps
166
+ # We add 0.1ms to be sure
167
+ delay = 1.0 / sps + 0.0001
168
+ time .sleep (delay )
169
+
170
+ # Read the conversion results
171
+ result = self .i2c .readList (self .__ADS1015_REG_POINTER_CONVERT , 2 )
172
+ # Return a mV value for the ADS1115
173
+ # (Take signed values into account as well)
174
+ val = (result [0 ] << 8 ) | (result [1 ])
175
+ if val > 0x7FFF :
176
+ return (val - 0xFFFF )* pga / 32768.0
177
+ else :
178
+ return ( (result [0 ] << 8 ) | (result [1 ]) )* pga / 32768.0
179
+
180
+
181
+ # ===========================================================================
182
+ # Adafruit_I2C Class
183
+ # ===========================================================================
184
+
185
+ class Adafruit_I2C (object ):
186
+
187
+ @staticmethod
188
+ def getPiRevision ():
189
+ "Gets the version number of the Raspberry Pi board"
190
+ # Revision list available at: http://elinux.org/RPi_HardwareHistory#Board_Revision_History
191
+ try :
192
+ with open ('/proc/cpuinfo' , 'r' ) as infile :
193
+ for line in infile :
194
+ # Match a line of the form "Revision : 0002" while ignoring extra
195
+ # info in front of the revsion (like 1000 when the Pi was over-volted).
196
+ match = re .match ('Revision\s+:\s+.*(\w{4})$' , line )
197
+ if match and match .group (1 ) in ['0000' , '0002' , '0003' ]:
198
+ # Return revision 1 if revision ends with 0000, 0002 or 0003.
199
+ return 1
200
+ elif match :
201
+ # Assume revision 2 if revision ends with any other 4 chars.
202
+ return 2
203
+ # Couldn't find the revision, assume revision 0 like older code for compatibility.
204
+ return 0
205
+ except :
206
+ return 0
207
+
208
+ @staticmethod
209
+ def getPiI2CBusNumber ():
210
+ # Gets the I2C bus number /dev/i2c#
211
+ return 1 if Adafruit_I2C .getPiRevision () > 1 else 0
212
+
213
+ def __init__ (self , address , busnum = - 1 , debug = False ):
214
+ self .address = address
215
+ # By default, the correct I2C bus is auto-detected using /proc/cpuinfo
216
+ # Alternatively, you can hard-code the bus version below:
217
+ # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
218
+ # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's)
219
+ self .bus = smbus .SMBus (busnum if busnum >= 0 else Adafruit_I2C .getPiI2CBusNumber ())
220
+ self .debug = debug
221
+
222
+ def reverseByteOrder (self , data ):
223
+ ''' Reverses the byte order of an int (16-bit) or long (32-bit) value '''
224
+ byteCount = len (hex (data )[2 :].replace ('L' ,'' )[::2 ])
225
+ val = 0
226
+ for i in range (byteCount ):
227
+ val = (val << 8 ) | (data & 0xff )
228
+ data >>= 8
229
+ return val
230
+
231
+ def errMsg (self ):
232
+ print ("Error accessing 0x%02X: Check your I2C address" % self .address )
233
+ return - 1
234
+
235
+ def write8 (self , reg , value ):
236
+ "Writes an 8-bit value to the specified register/address"
237
+ try :
238
+ self .bus .write_byte_data (self .address , reg , value )
239
+ if self .debug :
240
+ print ("I2C: Wrote 0x%02X to register 0x%02X" % (value , reg ))
241
+ except IOError :
242
+ return self .errMsg ()
243
+
244
+ def write16 (self , reg , value ):
245
+ "Writes a 16-bit value to the specified register/address pair"
246
+ try :
247
+ self .bus .write_word_data (self .address , reg , value )
248
+ if self .debug :
249
+ print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" % (value , reg , reg + 1 ))
250
+ except IOError :
251
+ return self .errMsg ()
252
+
253
+ def writeRaw8 (self , value ):
254
+ "Writes an 8-bit value on the bus"
255
+ try :
256
+ self .bus .write_byte (self .address , value )
257
+ if self .debug :
258
+ print ("I2C: Wrote 0x%02X" % value )
259
+ except IOError :
260
+ return self .errMsg ()
261
+
262
+ def writeList (self , reg , list ):
263
+ "Writes an array of bytes using I2C format"
264
+ try :
265
+ if self .debug :
266
+ print ("I2C: Writing list to register 0x%02X:" % reg )
267
+ print (list )
268
+ self .bus .write_i2c_block_data (self .address , reg , list )
269
+ except IOError :
270
+ return self .errMsg ()
271
+
272
+ def readList (self , reg , length ):
273
+ "Read a list of bytes from the I2C device"
274
+ try :
275
+ results = self .bus .read_i2c_block_data (self .address , reg , length )
276
+ if self .debug :
277
+ print ("I2C: Device 0x%02X returned the following from reg 0x%02X" % (self .address , reg ))
278
+ print (results )
279
+ return results
280
+ except IOError :
281
+ return self .errMsg ()
282
+
283
+ def readU8 (self , reg ):
284
+ "Read an unsigned byte from the I2C device"
285
+ try :
286
+ result = self .bus .read_byte_data (self .address , reg )
287
+ if self .debug :
288
+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg ))
289
+ return (result )
290
+ except IOError :
291
+ return self .errMsg ()
292
+
293
+ def readS8 (self , reg ):
294
+ "Reads a signed byte from the I2C device"
295
+ try :
296
+ result = self .bus .read_byte_data (self .address , reg )
297
+ if result > 127 : result -= 256
298
+ if self .debug :
299
+ print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self .address , result & 0xFF , reg ))
300
+ return result
301
+ except IOError :
302
+ return self .errMsg ()
303
+
304
+ def readU16 (self , reg , little_endian = True ):
305
+ "Reads an unsigned 16-bit value from the I2C device"
306
+ try :
307
+ result = self .bus .read_word_data (self .address ,reg )
308
+ # Swap bytes if using big endian because read_word_data assumes little
309
+ # endian on ARM (little endian) systems.
310
+ if not little_endian :
311
+ result = ((result << 8 ) & 0xFF00 ) + (result >> 8 )
312
+ if (self .debug ):
313
+ print ("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self .address , result & 0xFFFF , reg ))
314
+ return result
315
+ except IOError :
316
+ return self .errMsg ()
317
+
318
+ def readS16 (self , reg , little_endian = True ):
319
+ "Reads a signed 16-bit value from the I2C device"
320
+ try :
321
+ result = self .readU16 (reg ,little_endian )
322
+ if result > 32767 : result -= 65536
323
+ return result
324
+ except IOError :
325
+ return self .errMsg ()
0 commit comments