-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADS1x15.py
405 lines (372 loc) · 20 KB
/
ADS1x15.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Copyright (c) 2016 Adafruit Industries
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Slightly modified by Aegidius Pluess (www.aplu.ch), to remove references to other modules
import time
import smbus
# Register and other configuration values:
ADS1x15_DEFAULT_ADDRESS = 0x48
ADS1x15_POINTER_CONVERSION = 0x00
ADS1x15_POINTER_CONFIG = 0x01
ADS1x15_POINTER_LOW_THRESHOLD = 0x02
ADS1x15_POINTER_HIGH_THRESHOLD = 0x03
ADS1x15_CONFIG_OS_SINGLE = 0x8000
ADS1x15_CONFIG_MUX_OFFSET = 12
# Maping of gain values to config register values.
ADS1x15_CONFIG_GAIN = {
2/3: 0x0000,
1: 0x0200,
2: 0x0400,
4: 0x0600,
8: 0x0800,
16: 0x0A00
}
ADS1x15_CONFIG_MODE_CONTINUOUS = 0x0000
ADS1x15_CONFIG_MODE_SINGLE = 0x0100
# Mapping of data/sample rate to config register values for ADS1015 (faster).
ADS1015_CONFIG_DR = {
128: 0x0000,
250: 0x0020,
490: 0x0040,
920: 0x0060,
1600: 0x0080,
2400: 0x00A0,
3300: 0x00C0
}
# Mapping of data/sample rate to config register values for ADS1115 (slower).
ADS1115_CONFIG_DR = {
8: 0x0000,
16: 0x0020,
32: 0x0040,
64: 0x0060,
128: 0x0080,
250: 0x00A0,
475: 0x00C0,
860: 0x00E0
}
ADS1x15_CONFIG_COMP_WINDOW = 0x0010
ADS1x15_CONFIG_COMP_ACTIVE_HIGH = 0x0008
ADS1x15_CONFIG_COMP_LATCHING = 0x0004
ADS1x15_CONFIG_COMP_QUE = {
1: 0x0000,
2: 0x0001,
4: 0x0002
}
ADS1x15_CONFIG_COMP_QUE_DISABLE = 0x0003
class ADS1x15(object):
"""Base functionality for ADS1x15 analog to digital converters."""
def __init__(self, address = ADS1x15_DEFAULT_ADDRESS, busnum = 1):
self._address = address
self._bus = smbus.SMBus(busnum)
def _data_rate_default(self):
"""Retrieve the default data rate for this ADC (in samples per second).
Should be implemented by subclasses.
"""
raise NotImplementedError('Subclasses must implement _data_rate_default!')
def _data_rate_config(self, data_rate):
"""Subclasses should override this function and return a 16-bit value
that can be OR'ed with the config register to set the specified
data rate. If a value of None is specified then a default data_rate
setting should be returned. If an invalid or unsupported data_rate is
provided then an exception should be thrown.
"""
raise NotImplementedError('Subclass must implement _data_rate_config function!')
def _conversion_value(self, low, high):
"""Subclasses should override this function that takes the low and high
byte of a conversion result and returns a signed integer value.
"""
raise NotImplementedError('Subclass must implement _conversion_value function!')
def _read(self, mux, gain, data_rate, mode):
"""Perform an ADC read with the provided mux, gain, data_rate, and mode
values. Returns the signed integer result of the read.
"""
config = ADS1x15_CONFIG_OS_SINGLE # Go out of power-down mode for conversion.
# Specify mux value.
config |= (mux & 0x07) << ADS1x15_CONFIG_MUX_OFFSET
# Validate the passed in gain and then set it in the config.
if gain not in ADS1x15_CONFIG_GAIN:
raise ValueError('Gain must be one of: 2/3, 1, 2, 4, 8, 16')
config |= ADS1x15_CONFIG_GAIN[gain]
# Set the mode (continuous or single shot).
config |= mode
# Get the default data rate if none is specified (default differs between
# ADS1015 and ADS1115).
if data_rate is None:
data_rate = self._data_rate_default()
# Set the data rate (this is controlled by the subclass as it differs
# between ADS1015 and ADS1115).
config |= self._data_rate_config(data_rate)
config |= ADS1x15_CONFIG_COMP_QUE_DISABLE # Disble comparator mode.
# Send the config value to start the ADC conversion.
# Explicitly break the 16-bit value down to a big endian pair of bytes.
self.writeList(ADS1x15_POINTER_CONFIG, [(config >> 8) & 0xFF, config & 0xFF])
# Wait for the ADC sample to finish based on the sample rate plus a
# small offset to be sure (0.1 millisecond).
time.sleep(1.0/data_rate+0.0001)
# Retrieve the result.
result = self.readList(ADS1x15_POINTER_CONVERSION, 2)
return self._conversion_value(result[1], result[0])
def _read_comparator(self, mux, gain, data_rate, mode, high_threshold,
low_threshold, active_low, traditional, latching,
num_readings):
"""Perform an ADC read with the provided mux, gain, data_rate, and mode
values and with the comparator enabled as specified. Returns the signed
integer result of the read.
"""
assert num_readings == 1 or num_readings == 2 or num_readings == 4, 'Num readings must be 1, 2, or 4!'
# Set high and low threshold register values.
self.writeList(ADS1x15_POINTER_HIGH_THRESHOLD, [(high_threshold >> 8) & 0xFF, high_threshold & 0xFF])
self.writeList(ADS1x15_POINTER_LOW_THRESHOLD, [(low_threshold >> 8) & 0xFF, low_threshold & 0xFF])
# Now build up the appropriate config register value.
config = ADS1x15_CONFIG_OS_SINGLE # Go out of power-down mode for conversion.
# Specify mux value.
config |= (mux & 0x07) << ADS1x15_CONFIG_MUX_OFFSET
# Validate the passed in gain and then set it in the config.
if gain not in ADS1x15_CONFIG_GAIN:
raise ValueError('Gain must be one of: 2/3, 1, 2, 4, 8, 16')
config |= ADS1x15_CONFIG_GAIN[gain]
# Set the mode (continuous or single shot).
config |= mode
# Get the default data rate if none is specified (default differs between
# ADS1015 and ADS1115).
if data_rate is None:
data_rate = self._data_rate_default()
# Set the data rate (this is controlled by the subclass as it differs
# between ADS1015 and ADS1115).
config |= self._data_rate_config(data_rate)
# Enable window mode if required.
if not traditional:
config |= ADS1x15_CONFIG_COMP_WINDOW
# Enable active high mode if required.
if not active_low:
config |= ADS1x15_CONFIG_COMP_ACTIVE_HIGH
# Enable latching mode if required.
if latching:
config |= ADS1x15_CONFIG_COMP_LATCHING
# Set number of comparator hits before alerting.
config |= ADS1x15_CONFIG_COMP_QUE[num_readings]
# Send the config value to start the ADC conversion.
# Explicitly break the 16-bit value down to a big endian pair of bytes.
self.writeList(ADS1x15_POINTER_CONFIG, [(config >> 8) & 0xFF, config & 0xFF])
# Wait for the ADC sample to finish based on the sample rate plus a
# small offset to be sure (0.1 millisecond).
time.sleep(1.0/data_rate+0.0001)
# Retrieve the result.
result = self.readList(ADS1x15_POINTER_CONVERSION, 2)
return self._conversion_value(result[1], result[0])
def read_adc(self, channel, gain=1, data_rate=None):
"""Read a single ADC channel and return the ADC value as a signed integer
result. Channel must be a value within 0-3.
You can also pass in an optional data_rate parameter that controls
the ADC conversion time (in samples/second). Each chip has a different
set of allowed data rate values, see datasheet Table 9 config register
DR bit values.
ADS1015: 128, 250, 490, 920, 1600(default), 2400, 3300
ADS1115: 8, 16, 32, 64, 128 (default), 250, 475, 860
Each returned value will be a 12 or 16 bit signed integer value depending on the
ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
Gain of 1 for reading voltages from 0 to 4.09V.
Or pick a different gain to change the range of voltages that are read:
- 2/3 = +/-6.144V
- 1 = +/-4.096V
- 2 = +/-2.048V
- 4 = +/-1.024V
- 8 = +/-0.512V
- 16 = +/-0.256V
See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
"""
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
# Perform a single shot read and set the mux value to the channel plus
# the highest bit (bit 3) set.
return self._read(channel + 0x04, gain, data_rate, ADS1x15_CONFIG_MODE_SINGLE)
def read_adc_difference(self, differential, gain=1, data_rate=None):
"""Read the difference between two ADC channels and return the ADC value
as a signed integer result. Differential must be one of:
- 0 = Channel 0 minus channel 1
- 1 = Channel 0 minus channel 3
- 2 = Channel 1 minus channel 3
- 3 = Channel 2 minus channel 3
"""
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
# Perform a single shot read using the provided differential value
# as the mux value (which will enable differential mode).
return self._read(differential, gain, data_rate, ADS1x15_CONFIG_MODE_SINGLE)
def start_adc(self, channel, gain=1, data_rate=None):
"""Start continuous ADC conversions on the specified channel (0-3). Will
return an initial conversion result, then call the get_last_result()
function to read the most recent conversion result. Call stop_adc() to
stop conversions.
You can also pass in an optional data_rate parameter that controls
the ADC conversion time (in samples/second). Each chip has a different
set of allowed data rate values, see datasheet Table 9 config register
DR bit values.
ADS1015: 128, 250, 490, 920, 1600(default), 2400, 3300
ADS1115: 8, 16, 32, 64, 128 (default), 250, 475, 860
Each returned value will be a 12 or 16 bit signed integer value depending on the
ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
Gain of 1 for reading voltages from 0 to 4.09V.
Or pick a different gain to change the range of voltages that are read:
- 2/3 = +/-6.144V
- 1 = +/-4.096V
- 2 = +/-2.048V
- 4 = +/-1.024V
- 8 = +/-0.512V
- 16 = +/-0.256V
See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
"""
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
# Start continuous reads and set the mux value to the channel plus
# the highest bit (bit 3) set.
return self._read(channel + 0x04, gain, data_rate, ADS1x15_CONFIG_MODE_CONTINUOUS)
def start_adc_difference(self, differential, gain=1, data_rate=None):
"""Start continuous ADC conversions between two ADC channels. Differential
must be one of:
- 0 = Channel 0 minus channel 1
- 1 = Channel 0 minus channel 3
- 2 = Channel 1 minus channel 3
- 3 = Channel 2 minus channel 3
Will return an initial conversion result, then call the get_last_result()
function continuously to read the most recent conversion result. Call
stop_adc() to stop conversions.
"""
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
# Perform a single shot read using the provided differential value
# as the mux value (which will enable differential mode).
return self._read(differential, gain, data_rate, ADS1x15_CONFIG_MODE_CONTINUOUS)
def start_adc_comparator(self, channel, high_threshold, low_threshold,
gain=1, data_rate=None, active_low=True,
traditional=True, latching=False, num_readings=1):
"""Start continuous ADC conversions on the specified channel (0-3) with
the comparator enabled. When enabled, the comparator checks if
the ADC value is within the high_threshold & low_threshold value (both
should be signed 16-bit integers) and trigger the ALERT pin. The
behavior can be controlled by the following parameters:
- active_low: Boolean that indicates if ALERT is pulled low or high
when active/triggered. Default is true, active low.
- traditional: Boolean that indicates if the comparator is in traditional
mode where it fires when the value is within the threshold,
or in window mode where it fires when the value is _outside_
the threshold range. Default is true, traditional mode.
- latching: Boolean that indicates if the alert should be held until
get_last_result() is called to read the value and clear
the alert. Default is false, non-latching.
- num_readings: The number of readings that match the comparator before
triggering the alert. Can be 1, 2, or 4. Default is 1.
Will return an initial conversion result, then call the get_last_result()
function continuously to read the most recent conversion result. Call
stop_adc() to stop conversions.
"""
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
# Start continuous reads with comparator and set the mux value to the
# channel plus the highest bit (bit 3) set.
return self._read_comparator(channel + 0x04, gain, data_rate,
ADS1x15_CONFIG_MODE_CONTINUOUS,
high_threshold, low_threshold, active_low,
traditional, latching, num_readings)
def start_adc_difference_comparator(self, differential, high_threshold, low_threshold,
gain=1, data_rate=None, active_low=True,
traditional=True, latching=False, num_readings=1):
"""Start continuous ADC conversions between two channels with
the comparator enabled. See start_adc_difference for valid differential
parameter values and their meaning. When enabled the comparator to will
check if the ADC value is within the high_threshold & low_threshold value
(both should be signed 16-bit integers) and trigger the ALERT pin. The
behavior can be controlled by the following parameters:
- active_low: Boolean that indicates if ALERT is pulled low or high
when active/triggered. Default is true, active low.
- traditional: Boolean that indicates if the comparator is in traditional
mode where it fires when the value is within the threshold,
or in window mode where it fires when the value is _outside_
the threshold range. Default is true, traditional mode.
- latching: Boolean that indicates if the alert should be held until
get_last_result() is called to read the value and clear
the alert. Default is false, non-latching.
- num_readings: The number of readings that match the comparator before
triggering the alert. Can be 1, 2, or 4. Default is 1.
Will return an initial conversion result, then call the get_last_result()
function continuously to read the most recent conversion result. Call
stop_adc() to stop conversions.
"""
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
# Start continuous reads with comparator and set the mux value to the
# channel plus the highest bit (bit 3) set.
return self._read_comparator(differential, gain, data_rate,
ADS1x15_CONFIG_MODE_CONTINUOUS,
high_threshold, low_threshold, active_low,
traditional, latching, num_readings)
def stop_adc(self):
"""Stop all continuous ADC conversions (either normal or difference mode).
"""
# Set the config register to its default value of 0x8583 to stop
# continuous conversions.
config = 0x8583
self.writeList(ADS1x15_POINTER_CONFIG, [(config >> 8) & 0xFF, config & 0xFF])
def get_last_result(self):
"""Read the last conversion result when in continuous conversion mode.
Will return a signed integer value.
"""
# Retrieve the conversion register value, convert to a signed int, and
# return it.
result = self.readList(ADS1x15_POINTER_CONVERSION, 2)
return self._conversion_value(result[1], result[0])
def readList(self, register, length):
"""Read a length number of bytes from the specified register. Results
will be returned as a bytearray."""
results = self._bus.read_i2c_block_data(self._address, register, length)
return results
def writeList(self, register, data):
"""Write bytes to the specified register."""
self._bus.write_i2c_block_data(self._address, register, data)
class ADS1115(ADS1x15):
"""ADS1115 16-bit analog to digital converter instance."""
def __init__(self, *args, **kwargs):
super(ADS1115, self).__init__(*args, **kwargs)
def _data_rate_default(self):
# Default from datasheet page 16, config register DR bit default.
return 128
def _data_rate_config(self, data_rate):
if data_rate not in ADS1115_CONFIG_DR:
raise ValueError('Data rate must be one of: 8, 16, 32, 64, 128, 250, 475, 860')
return ADS1115_CONFIG_DR[data_rate]
def _conversion_value(self, low, high):
# Convert to 16-bit signed value.
value = ((high & 0xFF) << 8) | (low & 0xFF)
# Check for sign bit and turn into a negative value if set.
if value & 0x8000 != 0:
value -= 1 << 16
return value
class ADS1015(ADS1x15):
"""ADS1015 12-bit analog to digital converter instance."""
def __init__(self, *args, **kwargs):
super(ADS1015, self).__init__(*args, **kwargs)
def _data_rate_default(self):
# Default from datasheet page 19, config register DR bit default.
return 1600
def _data_rate_config(self, data_rate):
if data_rate not in ADS1015_CONFIG_DR:
raise ValueError('Data rate must be one of: 128, 250, 490, 920, 1600, 2400, 3300')
return ADS1015_CONFIG_DR[data_rate]
def _conversion_value(self, low, high):
# Convert to 12-bit signed value.
value = ((high & 0xFF) << 4) | ((low & 0xFF) >> 4)
# Check for sign bit and turn into a negative value if set.
if value & 0x800 != 0:
value -= 1 << 12
return value