Skip to content

Commit 017fb9f

Browse files
committed
Added Comparator functionality
Added controls for the COMP_QUE fields in the Config Register that handle the Comparator output. Also added an example of use.
1 parent acc1daa commit 017fb9f

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

adafruit_ads1x15/ads1x15.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,17 @@
3333
_ADS1X15_DEFAULT_ADDRESS = const(0x48)
3434
_ADS1X15_POINTER_CONVERSION = const(0x00)
3535
_ADS1X15_POINTER_CONFIG = const(0x01)
36+
_ADS1X15_POINTER_LO_THRES = const(0x02)
37+
_ADS1X15_POINTER_HI_THRES = const(0x03)
38+
3639
_ADS1X15_CONFIG_OS_SINGLE = const(0x8000)
3740
_ADS1X15_CONFIG_MUX_OFFSET = const(12)
38-
_ADS1X15_CONFIG_COMP_QUE_DISABLE = const(0x0003)
41+
_ADS1X15_CONFIG_COMP_QUEUE = {
42+
0: 0x0003,
43+
1: 0x0000,
44+
2: 0x0001,
45+
4: 0x0002,
46+
}
3947
_ADS1X15_CONFIG_GAIN = {
4048
2 / 3: 0x0000,
4149
1: 0x0200,
@@ -75,6 +83,7 @@ def __init__(
7583
gain: float = 1,
7684
data_rate: Optional[int] = None,
7785
mode: int = Mode.SINGLE,
86+
compqueue: int = 0,
7887
address: int = _ADS1X15_DEFAULT_ADDRESS,
7988
):
8089
# pylint: disable=too-many-arguments
@@ -83,6 +92,7 @@ def __init__(
8392
self.gain = gain
8493
self.data_rate = self._data_rate_default() if data_rate is None else data_rate
8594
self.mode = mode
95+
self.compqueue = compqueue
8696
self.i2c_device = I2CDevice(i2c, address)
8797

8898
@property
@@ -131,6 +141,25 @@ def gains(self) -> List[float]:
131141
g.sort()
132142
return g
133143

144+
@property
145+
def compqueue(self) -> int:
146+
"""The ADC Comparator Queue."""
147+
return self._compqueue
148+
149+
@compqueue.setter
150+
def compqueue(self, compqueue: int) -> None:
151+
possible_compqueues = self.compqueues
152+
if compqueue not in possible_compqueues:
153+
raise ValueError("Comparator Queue must be one of: {}".format(possible_compqueues))
154+
self._compqueue = compqueue
155+
156+
@property
157+
def compqueues(self) -> List[int]:
158+
"""Possible gain settings."""
159+
g = list(_ADS1X15_CONFIG_COMP_QUEUE.keys())
160+
g.sort()
161+
return g
162+
134163
@property
135164
def mode(self) -> int:
136165
"""The ADC conversion mode."""
@@ -183,7 +212,7 @@ def _read(self, pin: Pin) -> int:
183212
config |= _ADS1X15_CONFIG_GAIN[self.gain]
184213
config |= self.mode
185214
config |= self.rate_config[self.data_rate]
186-
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
215+
config |= _ADS1X15_CONFIG_COMP_QUEUE[self.compqueue]
187216
self._write_register(_ADS1X15_POINTER_CONFIG, config)
188217

189218
# Wait for conversion to complete
@@ -222,6 +251,22 @@ def _write_register(self, reg: int, value: int):
222251
with self.i2c_device as i2c:
223252
i2c.write(self.buf)
224253

254+
def write_comparator_low_threshold(self, value: int):
255+
"""Write 16 bit value to register."""
256+
self.buf[0] = _ADS1X15_POINTER_LO_THRES
257+
self.buf[1] = (value >> 8) & 0xFF
258+
self.buf[2] = value & 0xFF
259+
with self.i2c_device as i2c:
260+
i2c.write(self.buf)
261+
262+
def write_comparator_high_threshold(self, value: int):
263+
"""Write 16 bit value to register."""
264+
self.buf[0] = _ADS1X15_POINTER_HI_THRES
265+
self.buf[1] = (value >> 8) & 0xFF
266+
self.buf[2] = value & 0xFF
267+
with self.i2c_device as i2c:
268+
i2c.write(self.buf)
269+
225270
def _read_register(self, reg: int, fast: bool = False) -> int:
226271
"""Read 16 bit register value. If fast is True, the pointer register
227272
is not updated.

adafruit_ads1x15/analog_in.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ def voltage(self) -> float:
6464
"""Returns the voltage from the ADC pin as a floating point value."""
6565
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
6666
return volts
67+
68+
def ADC_value(self, volts: float) -> int:
69+
value = int((volts * 32767) / _ADS1X15_PGA_RANGE[self._ads.gain])
70+
return value
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import busio
7+
import countio
8+
9+
import adafruit_ads1x15.ads1015 as ADS
10+
# import adafruit_ads1x15.ads1115 as ADS
11+
from adafruit_ads1x15.analog_in import AnalogIn
12+
13+
# Create the I2C bus
14+
i2c = busio.I2C(board.SCL, board.SDA)
15+
16+
# Create the ADS object
17+
ads = ADS.ADS1015(i2c)
18+
# ads = ADS.ADS1115(i2c)
19+
20+
# Create a single-ended channel on Pin 0
21+
# Max counts for ADS1015 = 2047
22+
# ADS1115 = 32767
23+
chan = AnalogIn(ads, ADS.P0)
24+
25+
# Create Interrupt-driven input to track comparator changes
26+
int_pin = countio.Counter(board.GP9, edge=countio.Edge.RISE)
27+
28+
ads.compqueue = 1
29+
ads.write_comparator_low_threshold(0x3E80)
30+
ads.write_comparator_high_threshold(0x3E90)
31+
32+
count = 0
33+
while True:
34+
print(chan.value, chan.voltage) #This initiates new ADC reading
35+
if int_pin.count > count:
36+
print("Comparator Triggered")
37+
count = int_pin.count
38+
time.sleep(2)

0 commit comments

Comments
 (0)