Skip to content

Update read for continuous #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2020
Merged

Update read for continuous #57

merged 1 commit into from
Jun 28, 2020

Conversation

caternuson
Copy link
Contributor

Possible fix for #55

This issue wasn't 100% reproducible. Sometimes was able to get things to work on a Pi0. Testing on a Pi4 seemed to make it happen all the time. The datasheet doesn't provide much information about what to do when switching ADC channels while in continuous mode. This fix simply adds a delay based on data rate. Seemed to fix, tested on Pi 4:

(blinka) pi@raspberrypi:~/repos/Adafruit_CircuitPython_ADS1x15/adafruit_ads1x15 $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import board
>>> import adafruit_ads1x15.ads1115 as ADS
>>> from adafruit_ads1x15.ads1x15 import Mode
>>> from adafruit_ads1x15.analog_in import AnalogIn
>>> ads = ADS.ADS1115(board.I2C())
>>> ads.mode = Mode.CONTINUOUS
>>> ads.data_rate = 860
>>> p1 = AnalogIn(ads, ADS.P1)
>>> p2 = AnalogIn(ads, ADS.P2)
>>> p1.value
18055
>>> p1.value
18057
>>> p2.value
3
>>> p2.value
4
>>> p1.value
18057
>>> p1.value
18056
>>> p2.value
3
>>> 

@evaherrada
Copy link
Collaborator

@caternuson Are you sure that's using your fork of the ads1x15 library? It seems like you're in the ads1x15 library directory, so the import paths you're using wouldn't work (or at least didn't work on my pi)

Python 3.5 was giving me some issues with the SCL and SDA pins, so I'm installing 3.7 to try it on that

@evaherrada
Copy link
Collaborator

evaherrada commented Jun 25, 2020

@caternuson Ok, so I got 3.7 working, which turned out to be an exceptional waste of time since the issue was actually that I forgot to enable I2C.

However, I still got results similar to what I saw before when I was testing the current version of the library on CircuitPython.

@caternuson
Copy link
Contributor Author

However, I still got results similar to what I saw before when I was testing the current version of the library on CircuitPython.

@dherrada Are you testing the current version or the pull request? You need to do some special git-foo to bring in the PR code.

@evaherrada
Copy link
Collaborator

@caternuson You're totally right. I did have your fork, but I was not on the right branch

@evaherrada
Copy link
Collaborator

@caternuson Yep. Works perfectly now

@zacnelson
Copy link

@caternuson This is working for me as well. Here's some benchmark testing.

import board
import busio
import digitalio
from adafruit_mcp230xx.mcp23017 import MCP23017
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from adafruit_ads1x15.ads1x15 import Mode
import time

def print_data_and_time(pin):
    t_start = time.time()
    print('{}: {:>5} {}'.format(pin._pin_setting, pin.value, (time.time() - t_start) * 1000))

i2c = busio.I2C(board.SCL, board.SDA, frequency=1000000)
ads = ADS.ADS1115(i2c, address=0x49)
ads.mode = Mode.CONTINUOUS
ads.data_rate = 860
p0 = AnalogIn(ads, ADS.P0)
p1 = AnalogIn(ads, ADS.P1)
p2 = AnalogIn(ads, ADS.P2)
p3 = AnalogIn(ads, ADS.P3)

print_data_and_time(p0)
print_data_and_time(p0)
print_data_and_time(p0)
print_data_and_time(p1)
print_data_and_time(p2)
print_data_and_time(p3)
print_data_and_time(p3)
print_data_and_time(p3)
print_data_and_time(p1)

The correct values for the following pins should be (roughly):
p0 = 13300
p1 = 13300
p2 = 0
p3 = 590
Previously (without this fix) I get the following output:

0: 13246 1.323699951171875
0: 13242 0.4038810729980469
0: 13242 0.3685951232910156
1: 13263 1.077413558959961 (probably wrong, but same voltage on pin0 and pin1 so can't tell)
2: 13243 1.0492801666259766 (wrong)
3:     3 1.0695457458496094 (wrong)
3:     3 0.3685951232910156 (wrong)
3:     3 0.35881996154785156 (wrong)
1:   585 1.0554790496826172 (wrong)

and now with the fix:

0: 13329 3.7097930908203125
0: 13314 0.40340423583984375
0: 13314 0.37026405334472656
1: 13289 3.551006317138672
2:     4 3.501415252685547
3:   586 3.7114620208740234
3:   587 0.40435791015625
3:   587 0.39124488830566406
1: 13272 3.507852554321289

@@ -170,8 +174,12 @@ def _read(self, pin):
self._write_register(_ADS1X15_POINTER_CONFIG, config)

if self.mode == Mode.SINGLE:
# poll conversion complete status bit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would throw these comments up in the function definition comments to describe how this works in the context of the whole function.

while not self._conversion_complete():
pass
else:
# just sleep (can't poll in continuous)
time.sleep(2 / self.data_rate)
Copy link

@zacnelson zacnelson Jun 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the ADS1115 data sheet (and also the ADS1015) the conversion time is 1 / DR:

9.3.6 Output Data Rate and Conversion Time
The ADS111x offer programmable output data rates. Use the DR[2:0] bits in the Config register to select output
data rates of 8 SPS, 16 SPS, 32 SPS, 64 SPS, 128 SPS, 250 SPS, 475 SPS, or 860 SPS.
Conversions in the ADS111x settle within a single cycle; thus, the conversion time is equal to 1 / DR.

@caternuson Any particular reason you chose 2 / DR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried 1 / DR first and it didn't seem to work. Might have something to do with extra settling time needed due to the mux being changed? Not sure.

@caternuson caternuson requested a review from a team June 28, 2020 15:22
@caternuson caternuson merged commit 1ffc009 into adafruit:master Jun 28, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jun 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants