adafruit / Adafruit_CircuitPython_ADS1x15

CircuitPython drivers for the ADS1x15 series of ADCs.
MIT License
133 stars 58 forks source link

Update read for continuous #57

Closed caternuson closed 4 years ago

caternuson commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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

evaherrada commented 4 years ago

@caternuson Yep. Works perfectly now

zacnelson commented 4 years ago

@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