adafruit / Adafruit_CircuitPython_ADS1x15

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

Wait for conversions in continuous example #62

Closed WizardTim closed 3 years ago

WizardTim commented 3 years ago

Previously polling rate (apparent sample rate) was limited purely by I2C speed, script will now wait a full conversion cycle until it next reads the conversion result.

Requires #61 to be merged first.

With RATE=250 and i2c_baudrate=1000000 (1 MHz)

Old

Took 0.139 s to acquire 1000 samples.

Configured:
    Requested       =   250    sps
    Reported        =   250    sps

Actual:
    Polling Rate    =  7171.07 sps
                       2868.43%
    Repeats         =   966
    Conversion Rate =   243.82 sps   (estimated)

New

Took 4.009 s to acquire 1000 samples.

Configured:
    Requested       =   250    sps
    Reported        =   250    sps

Actual:
    Polling Rate    =   249.41 sps
                         99.76%
    Repeats         =    25
    Conversion Rate =   243.18 sps   (estimated)

Also constrains polling rate at any other valid sample rate, eg. RATE=3300 and i2c_baudrate=1000000 (1 MHz) New

Took 0.309 s to acquire 1000 samples.

Configured:
    Requested       =  3300    sps
    Reported        =  3300    sps

Actual:
    Polling Rate    =  3234.39 sps
                         98.01%
    Repeats         =    25
    Conversion Rate =  3153.53 sps   (estimated)
WizardTim commented 3 years ago

This pull request resolves #27, #35, #43.

New

Took 10.003 s to acquire 33000 samples.

Configured:
    Requested       =  3300    sps
    Reported        =  3300    sps

Actual:
    Polling Rate    =  3298.86 sps
                         99.97%
    Skipped         =     2
    Repeats         =   444
    Conversion Rate =  3254.48 sps   (estimated)

Plot of time.monotonic() error in comparison to time_next_sample at ADC read (3,300 sps jitter target is 303 μs) ads1015 i2c jitter

Bugs: Will often skip first 1 or 2 samples, I suspect this is due to the following line however I don't think it's worth the trouble to change it. https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15/blob/c513e9ec6681e7f458a08d439f268b7c27585b6a/adafruit_ads1x15/ads1x15.py#L180

WizardTim commented 3 years ago

New Fix it skipping 1-2 samples on the first acquisition due to setup time, still skips 1 sample every so often with >1000 samples

Took 0.303 s to acquire 1000 samples.

Configured:
    Requested       =  3300    sps
    Reported        =  3300    sps

Actual:
    Polling Rate    =  3298.52 sps
                         99.96%
    Skipped         =     0
    Repeats         =    27
    Conversion Rate =  3209.46 sps   (estimated)
caternuson commented 3 years ago

Sorry. Just now seeing this.

So this essentially looks like you've implemented something like suggested here? https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15/issues/35#issuecomment-501747400

WizardTim commented 3 years ago

Sorry. Just now seeing this.

So this essentially looks like you've implemented something like suggested here? #35 (comment)

Yes pretty much although this implementation is a bit more advanced to make sure the sample rate doesn't drift or over-sample to catch up. It also uses a blocking while loop instead of time.sleep(SOME_DELAY) in order to achieve better timing at the cost of power consumption.

caternuson commented 3 years ago

Yep. Thanks. Looks like you also tested it, so I'm fine with merging. If this was useful for you, hopefully will be for others trying to use fast read.