1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import time
25 import smbus
26
27
28 ADS1x15_DEFAULT_ADDRESS = 0x48
29 ADS1x15_POINTER_CONVERSION = 0x00
30 ADS1x15_POINTER_CONFIG = 0x01
31 ADS1x15_POINTER_LOW_THRESHOLD = 0x02
32 ADS1x15_POINTER_HIGH_THRESHOLD = 0x03
33 ADS1x15_CONFIG_OS_SINGLE = 0x8000
34 ADS1x15_CONFIG_MUX_OFFSET = 12
35
36 ADS1x15_CONFIG_GAIN = {
37 2/3: 0x0000,
38 1: 0x0200,
39 2: 0x0400,
40 4: 0x0600,
41 8: 0x0800,
42 16: 0x0A00
43 }
44 ADS1x15_CONFIG_MODE_CONTINUOUS = 0x0000
45 ADS1x15_CONFIG_MODE_SINGLE = 0x0100
46
47 ADS1015_CONFIG_DR = {
48 128: 0x0000,
49 250: 0x0020,
50 490: 0x0040,
51 920: 0x0060,
52 1600: 0x0080,
53 2400: 0x00A0,
54 3300: 0x00C0
55 }
56
57 ADS1115_CONFIG_DR = {
58 8: 0x0000,
59 16: 0x0020,
60 32: 0x0040,
61 64: 0x0060,
62 128: 0x0080,
63 250: 0x00A0,
64 475: 0x00C0,
65 860: 0x00E0
66 }
67 ADS1x15_CONFIG_COMP_WINDOW = 0x0010
68 ADS1x15_CONFIG_COMP_ACTIVE_HIGH = 0x0008
69 ADS1x15_CONFIG_COMP_LATCHING = 0x0004
70 ADS1x15_CONFIG_COMP_QUE = {
71 1: 0x0000,
72 2: 0x0001,
73 4: 0x0002
74 }
75 ADS1x15_CONFIG_COMP_QUE_DISABLE = 0x0003
76
77
79 """Base functionality for ADS1x15 analog to digital converters."""
80
82 self._address = address
83 self._bus = smbus.SMBus(busnum)
84
86 """Retrieve the default data rate for this ADC (in samples per second).
87 Should be implemented by subclasses.
88 """
89 raise NotImplementedError('Subclasses must implement _data_rate_default!')
90
92 """Subclasses should override this function and return a 16-bit value
93 that can be OR'ed with the config register to set the specified
94 data rate. If a value of None is specified then a default data_rate
95 setting should be returned. If an invalid or unsupported data_rate is
96 provided then an exception should be thrown.
97 """
98 raise NotImplementedError('Subclass must implement _data_rate_config function!')
99
101 """Subclasses should override this function that takes the low and high
102 byte of a conversion result and returns a signed integer value.
103 """
104 raise NotImplementedError('Subclass must implement _conversion_value function!')
105
106 - def _read(self, mux, gain, data_rate, mode):
107 """Perform an ADC read with the provided mux, gain, data_rate, and mode
108 values. Returns the signed integer result of the read.
109 """
110 config = ADS1x15_CONFIG_OS_SINGLE
111
112 config |= (mux & 0x07) << ADS1x15_CONFIG_MUX_OFFSET
113
114 if gain not in ADS1x15_CONFIG_GAIN:
115 raise ValueError('Gain must be one of: 2/3, 1, 2, 4, 8, 16')
116 config |= ADS1x15_CONFIG_GAIN[gain]
117
118 config |= mode
119
120
121 if data_rate is None:
122 data_rate = self._data_rate_default()
123
124
125 config |= self._data_rate_config(data_rate)
126 config |= ADS1x15_CONFIG_COMP_QUE_DISABLE
127
128
129 self.writeList(ADS1x15_POINTER_CONFIG, [(config >> 8) & 0xFF, config & 0xFF])
130
131
132 time.sleep(1.0/data_rate+0.0001)
133
134 result = self.readList(ADS1x15_POINTER_CONVERSION, 2)
135 return self._conversion_value(result[1], result[0])
136
137 - def _read_comparator(self, mux, gain, data_rate, mode, high_threshold,
138 low_threshold, active_low, traditional, latching,
139 num_readings):
140 """Perform an ADC read with the provided mux, gain, data_rate, and mode
141 values and with the comparator enabled as specified. Returns the signed
142 integer result of the read.
143 """
144 assert num_readings == 1 or num_readings == 2 or num_readings == 4, 'Num readings must be 1, 2, or 4!'
145
146 self.writeList(ADS1x15_POINTER_HIGH_THRESHOLD, [(high_threshold >> 8) & 0xFF, high_threshold & 0xFF])
147 self.writeList(ADS1x15_POINTER_LOW_THRESHOLD, [(low_threshold >> 8) & 0xFF, low_threshold & 0xFF])
148
149 config = ADS1x15_CONFIG_OS_SINGLE
150
151 config |= (mux & 0x07) << ADS1x15_CONFIG_MUX_OFFSET
152
153 if gain not in ADS1x15_CONFIG_GAIN:
154 raise ValueError('Gain must be one of: 2/3, 1, 2, 4, 8, 16')
155 config |= ADS1x15_CONFIG_GAIN[gain]
156
157 config |= mode
158
159
160 if data_rate is None:
161 data_rate = self._data_rate_default()
162
163
164 config |= self._data_rate_config(data_rate)
165
166 if not traditional:
167 config |= ADS1x15_CONFIG_COMP_WINDOW
168
169 if not active_low:
170 config |= ADS1x15_CONFIG_COMP_ACTIVE_HIGH
171
172 if latching:
173 config |= ADS1x15_CONFIG_COMP_LATCHING
174
175 config |= ADS1x15_CONFIG_COMP_QUE[num_readings]
176
177
178 self.writeList(ADS1x15_POINTER_CONFIG, [(config >> 8) & 0xFF, config & 0xFF])
179
180
181 time.sleep(1.0/data_rate+0.0001)
182
183 result = self.readList(ADS1x15_POINTER_CONVERSION, 2)
184 return self._conversion_value(result[1], result[0])
185
186 - def read_adc(self, channel, gain=1, data_rate=None):
187 """Read a single ADC channel and return the ADC value as a signed integer
188 result. Channel must be a value within 0-3.
189 You can also pass in an optional data_rate parameter that controls
190 the ADC conversion time (in samples/second). Each chip has a different
191 set of allowed data rate values, see datasheet Table 9 config register
192 DR bit values.
193 ADS1015: 128, 250, 490, 920, 1600(default), 2400, 3300
194 ADS1115: 8, 16, 32, 64, 128 (default), 250, 475, 860
195 Each returned value will be a 12 or 16 bit signed integer value depending on the
196 ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
197 Gain of 1 for reading voltages from 0 to 4.09V.
198 Or pick a different gain to change the range of voltages that are read:
199 - 2/3 = +/-6.144V
200 - 1 = +/-4.096V
201 - 2 = +/-2.048V
202 - 4 = +/-1.024V
203 - 8 = +/-0.512V
204 - 16 = +/-0.256V
205 See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
206 """
207 assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
208
209
210 return self._read(channel + 0x04, gain, data_rate, ADS1x15_CONFIG_MODE_SINGLE)
211
213 """Read the difference between two ADC channels and return the ADC value
214 as a signed integer result. Differential must be one of:
215 - 0 = Channel 0 minus channel 1
216 - 1 = Channel 0 minus channel 3
217 - 2 = Channel 1 minus channel 3
218 - 3 = Channel 2 minus channel 3
219 """
220 assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
221
222
223 return self._read(differential, gain, data_rate, ADS1x15_CONFIG_MODE_SINGLE)
224
225 - def start_adc(self, channel, gain=1, data_rate=None):
226 """Start continuous ADC conversions on the specified channel (0-3). Will
227 return an initial conversion result, then call the get_last_result()
228 function to read the most recent conversion result. Call stop_adc() to
229 stop conversions.
230 You can also pass in an optional data_rate parameter that controls
231 the ADC conversion time (in samples/second). Each chip has a different
232 set of allowed data rate values, see datasheet Table 9 config register
233 DR bit values.
234 ADS1015: 128, 250, 490, 920, 1600(default), 2400, 3300
235 ADS1115: 8, 16, 32, 64, 128 (default), 250, 475, 860
236 Each returned value will be a 12 or 16 bit signed integer value depending on the
237 ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
238 Gain of 1 for reading voltages from 0 to 4.09V.
239 Or pick a different gain to change the range of voltages that are read:
240 - 2/3 = +/-6.144V
241 - 1 = +/-4.096V
242 - 2 = +/-2.048V
243 - 4 = +/-1.024V
244 - 8 = +/-0.512V
245 - 16 = +/-0.256V
246 See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
247 """
248 assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
249
250
251 return self._read(channel + 0x04, gain, data_rate, ADS1x15_CONFIG_MODE_CONTINUOUS)
252
254 """Start continuous ADC conversions between two ADC channels. Differential
255 must be one of:
256 - 0 = Channel 0 minus channel 1
257 - 1 = Channel 0 minus channel 3
258 - 2 = Channel 1 minus channel 3
259 - 3 = Channel 2 minus channel 3
260 Will return an initial conversion result, then call the get_last_result()
261 function continuously to read the most recent conversion result. Call
262 stop_adc() to stop conversions.
263 """
264 assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
265
266
267 return self._read(differential, gain, data_rate, ADS1x15_CONFIG_MODE_CONTINUOUS)
268
269 - def start_adc_comparator(self, channel, high_threshold, low_threshold,
270 gain=1, data_rate=None, active_low=True,
271 traditional=True, latching=False, num_readings=1):
272 """Start continuous ADC conversions on the specified channel (0-3) with
273 the comparator enabled. When enabled, the comparator checks if
274 the ADC value is within the high_threshold & low_threshold value (both
275 should be signed 16-bit integers) and trigger the ALERT pin. The
276 behavior can be controlled by the following parameters:
277 - active_low: Boolean that indicates if ALERT is pulled low or high
278 when active/triggered. Default is true, active low.
279 - traditional: Boolean that indicates if the comparator is in traditional
280 mode where it fires when the value is within the threshold,
281 or in window mode where it fires when the value is _outside_
282 the threshold range. Default is true, traditional mode.
283 - latching: Boolean that indicates if the alert should be held until
284 get_last_result() is called to read the value and clear
285 the alert. Default is false, non-latching.
286 - num_readings: The number of readings that match the comparator before
287 triggering the alert. Can be 1, 2, or 4. Default is 1.
288 Will return an initial conversion result, then call the get_last_result()
289 function continuously to read the most recent conversion result. Call
290 stop_adc() to stop conversions.
291 """
292 assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
293
294
295 return self._read_comparator(channel + 0x04, gain, data_rate,
296 ADS1x15_CONFIG_MODE_CONTINUOUS,
297 high_threshold, low_threshold, active_low,
298 traditional, latching, num_readings)
299
300 - def start_adc_difference_comparator(self, differential, high_threshold, low_threshold,
301 gain=1, data_rate=None, active_low=True,
302 traditional=True, latching=False, num_readings=1):
303 """Start continuous ADC conversions between two channels with
304 the comparator enabled. See start_adc_difference for valid differential
305 parameter values and their meaning. When enabled the comparator to will
306 check if the ADC value is within the high_threshold & low_threshold value
307 (both should be signed 16-bit integers) and trigger the ALERT pin. The
308 behavior can be controlled by the following parameters:
309 - active_low: Boolean that indicates if ALERT is pulled low or high
310 when active/triggered. Default is true, active low.
311 - traditional: Boolean that indicates if the comparator is in traditional
312 mode where it fires when the value is within the threshold,
313 or in window mode where it fires when the value is _outside_
314 the threshold range. Default is true, traditional mode.
315 - latching: Boolean that indicates if the alert should be held until
316 get_last_result() is called to read the value and clear
317 the alert. Default is false, non-latching.
318 - num_readings: The number of readings that match the comparator before
319 triggering the alert. Can be 1, 2, or 4. Default is 1.
320 Will return an initial conversion result, then call the get_last_result()
321 function continuously to read the most recent conversion result. Call
322 stop_adc() to stop conversions.
323 """
324 assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
325
326
327 return self._read_comparator(differential, gain, data_rate,
328 ADS1x15_CONFIG_MODE_CONTINUOUS,
329 high_threshold, low_threshold, active_low,
330 traditional, latching, num_readings)
331
333 """Stop all continuous ADC conversions (either normal or difference mode).
334 """
335
336
337 config = 0x8583
338 self.writeList(ADS1x15_POINTER_CONFIG, [(config >> 8) & 0xFF, config & 0xFF])
339
348
350 """Read a length number of bytes from the specified register. Results
351 will be returned as a bytearray."""
352 results = self._bus.read_i2c_block_data(self._address, register, length)
353 return results
354
355
357 """Write bytes to the specified register."""
358 self._bus.write_i2c_block_data(self._address, register, data)
359
360
362 """ADS1115 16-bit analog to digital converter instance."""
363
366
370
375
377
378 value = ((high & 0xFF) << 8) | (low & 0xFF)
379
380 if value & 0x8000 != 0:
381 value -= 1 << 16
382 return value
383
385 """ADS1015 12-bit analog to digital converter instance."""
386
389
393
398
400
401 value = ((high & 0xFF) << 4) | ((low & 0xFF) >> 4)
402
403 if value & 0x800 != 0:
404 value -= 1 << 12
405 return value
406