adafruit/CircuitPython_NAU7802

poll rate example

Opened this issue · 5 comments

Would someone please write an example that changes the poll rate to a higher speed? I'm having trouble making it work. The only thing that has worked for me is to change the default in my local copy of cedargrove_nau7802.py.
Thanks!

Here's an example calibrator that measures raw output:

# clue_scale_calibrator.py

import time
import board
from cedargrove_nau7802 import NAU7802

SAMPLE_AVG = 3  # Number of sample values to average
DEFAULT_GAIN = 128  # Default gain for internal PGA
POLL_RATE = 320  # 10, 20, 40, 80, or 320

# Instantiate 24-bit load sensor ADC
i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
nau7802 = NAU7802(i2c, address=0x2A, active_channels=1)


def zero_channel():
    """Initiate internal calibration and zero the current channel. Use after
    power-up, a new channel is selected, or to adjust for measurement drift.
    Can be used to zero the scale with a tare weight."""
    nau7802.calibrate("INTERNAL")
    nau7802.calibrate("OFFSET")


def read(samples=100):
    # Read and average consecutive raw sample values; return average raw value
    sample_sum = 0
    sample_count = samples
    while sample_count > 0:
        if nau7802.available():
            sample_sum = sample_sum + nau7802.read()
            sample_count -= 1
    return int(sample_sum / samples)


# Activate the NAU780 internal analog circuitry, set gain, and calibrate/zero
nau7802.enable(True)
nau7802.gain = DEFAULT_GAIN  # Use default gain
nau7802.poll_rate = POLL_RATE
zero_channel()  # Calibrate and zero

print("-----------------------------------")
print(" NAU7802 SINGLE CHANNEL CALIBRATOR")
print("-----------------------------------")
print("Place a calibration weight on the")
print("load cell.")
print("-----------------------------------")
print("")


# Main loop: Read sample and display value
while True:

    # Read the raw value; print raw value, gain setting, % of full-scale, and measured poll_rate
    t0 = time.monotonic()
    value = read(SAMPLE_AVG)
    rate = (1/(time.monotonic() - t0))
    print(f"CHAN_{nau7802.channel:1.0f} RAW VALUE: {value:7.0f}")
    print(f"GAIN: x{DEFAULT_GAIN}  full-scale: {(value / ((2**23) - 1)) * 100:3.2f}%")
    print(f"POLL_RATE: {rate:4.1f}")
    print("===================================")

    time.sleep(0.1)

Is this what you were looking for?

BTW, the measured sample rate is somewhat less than the value set in the code and doesn't exceed about 100SPS.

That appears to be exactly what I'm looking for. Thanks!

However, I'm only getting measured poll rates of 12-17SPS.

Why does the measured sample rate max out at 100SPS? And what can I do to get to that rate?

Can you share your code?

I ran your example above verbatim. The only thing is I ran it directly from my MacBook Pro using an Adafruit MCP2221 breakout board

Here is a bit of the output:

CHAN_1 RAW VALUE: 184
GAIN: x128 full-scale: 0.00%
POLL_RATE: 16.7

CHAN_1 RAW VALUE: 114
GAIN: x128 full-scale: 0.00%
POLL_RATE: 12.6

CHAN_1 RAW VALUE: 372
GAIN: x128 full-scale: 0.00%
POLL_RATE: 11.9

CHAN_1 RAW VALUE: -618
GAIN: x128 full-scale: -0.01%
POLL_RATE: 11.9

CHAN_1 RAW VALUE: 56
GAIN: x128 full-scale: 0.00%
POLL_RATE: 14.5

CHAN_1 RAW VALUE: 54
GAIN: x128 full-scale: 0.00%
POLL_RATE: 17.5

CHAN_1 RAW VALUE: 616
GAIN: x128 full-scale: 0.01%
POLL_RATE: 12.0

I've not used the MCP2221 General Purpose USB to GPIO ADC I2C - Stemma QT breakout, so I'm not certain of where to look for the speed bottleneck. The numbers I reported were from a standalone test with the NAU7802 breakout connected via stemmaQT to a Clue board -- no host connection. You may want to post the question on the Forums or the Adafruit Discord to cast a broader net.