-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from RoaCode/comparator
Added Comparator functionality
- Loading branch information
Showing
6 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 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.comparator_queue_length = 1 | ||
|
||
# Set comparator low threshold to 2V | ||
ads.comparator_low_threshold = chan.convert_to_value(2.000) | ||
# Set comparator high threshold to 2.002V. High threshold must be above low threshold | ||
ads.comparator_high_threshold = chan.convert_to_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) |