Skip to content
49 changes: 47 additions & 2 deletions adafruit_ads1x15/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@
_ADS1X15_DEFAULT_ADDRESS = const(0x48)
_ADS1X15_POINTER_CONVERSION = const(0x00)
_ADS1X15_POINTER_CONFIG = const(0x01)
_ADS1X15_POINTER_LO_THRES = const(0x02)
_ADS1X15_POINTER_HI_THRES = const(0x03)

_ADS1X15_CONFIG_OS_SINGLE = const(0x8000)
_ADS1X15_CONFIG_MUX_OFFSET = const(12)
_ADS1X15_CONFIG_COMP_QUE_DISABLE = const(0x0003)
_ADS1X15_CONFIG_COMP_QUEUE = {
0: 0x0003,
1: 0x0000,
2: 0x0001,
4: 0x0002,
}
_ADS1X15_CONFIG_GAIN = {
2 / 3: 0x0000,
1: 0x0200,
Expand Down Expand Up @@ -75,6 +83,7 @@ def __init__(
gain: float = 1,
data_rate: Optional[int] = None,
mode: int = Mode.SINGLE,
compqueue: int = 0,
address: int = _ADS1X15_DEFAULT_ADDRESS,
):
# pylint: disable=too-many-arguments
Expand All @@ -83,6 +92,7 @@ def __init__(
self.gain = gain
self.data_rate = self._data_rate_default() if data_rate is None else data_rate
self.mode = mode
self.compqueue = compqueue
self.i2c_device = I2CDevice(i2c, address)

@property
Expand Down Expand Up @@ -131,6 +141,25 @@ def gains(self) -> List[float]:
g.sort()
return g

@property
def compqueue(self) -> int:
"""The ADC Comparator Queue."""
return self._compqueue

@compqueue.setter
def compqueue(self, compqueue: int) -> None:
possible_compqueues = self.compqueues
if compqueue not in possible_compqueues:
raise ValueError("Comparator Queue must be one of: {}".format(possible_compqueues))
self._compqueue = compqueue

@property
def compqueues(self) -> List[int]:
"""Possible gain settings."""
g = list(_ADS1X15_CONFIG_COMP_QUEUE.keys())
g.sort()
return g

@property
def mode(self) -> int:
"""The ADC conversion mode."""
Expand Down Expand Up @@ -183,7 +212,7 @@ def _read(self, pin: Pin) -> int:
config |= _ADS1X15_CONFIG_GAIN[self.gain]
config |= self.mode
config |= self.rate_config[self.data_rate]
config |= _ADS1X15_CONFIG_COMP_QUE_DISABLE
config |= _ADS1X15_CONFIG_COMP_QUEUE[self.compqueue]
self._write_register(_ADS1X15_POINTER_CONFIG, config)

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

def write_comparator_low_threshold(self, value: int):
"""Write 16 bit value to Comparator Low Threshold register."""
self.buf[0] = _ADS1X15_POINTER_LO_THRES
self.buf[1] = (value >> 8) & 0xFF
self.buf[2] = value & 0xFF
with self.i2c_device as i2c:
i2c.write(self.buf)

def write_comparator_high_threshold(self, value: int):
"""Write 16 bit value to Comparator High Threshold register."""
self.buf[0] = _ADS1X15_POINTER_HI_THRES
self.buf[1] = (value >> 8) & 0xFF
self.buf[2] = value & 0xFF
with self.i2c_device as i2c:
i2c.write(self.buf)

def _read_register(self, reg: int, fast: bool = False) -> int:
"""Read 16 bit register value. If fast is True, the pointer register
is not updated.
Expand Down
5 changes: 5 additions & 0 deletions adafruit_ads1x15/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ def voltage(self) -> float:
"""Returns the voltage from the ADC pin as a floating point value."""
volts = self.value * _ADS1X15_PGA_RANGE[self._ads.gain] / 32767
return volts

def ADC_value(self, volts: float) -> int:
"""Calculates integer for threshold registers from voltage level input"""
value = int((volts * 32767) / _ADS1X15_PGA_RANGE[self._ads.gain])
return value
41 changes: 41 additions & 0 deletions examples/ads1x15_comparator_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
import countio

import adafruit_ads1x15.ads1015 as ADS
# import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)

# Create the ADS object
ads = ADS.ADS1015(i2c)
# ads = ADS.ADS1115(i2c)

# Create a single-ended channel on Pin 0
# Max counts for ADS1015 = 2047
# ADS1115 = 32767
chan = AnalogIn(ads, ADS.P0)

# Create Interrupt-driven input to track comparator changes
int_pin = countio.Counter(board.GP9, edge=countio.Edge.RISE)

# Set comparator to assert after 1 ADC conversion
ads.compqueue = 1
# Set comparator low threshold to 2V
ads.write_comparator_low_threshold(chan.ADC_value(2))
# Set comparator high threshold to 2.002V. High threshold must be above low threshold
ads.write_comparator_high_threshold(chan.ADC_value(2.002))

count = 0
while True:
print(chan.value, chan.voltage) #This initiates new ADC reading
if int_pin.count > count:
print("Comparator Triggered")
count = int_pin.count
time.sleep(2)