Skip to content

Commit dfcdcb6

Browse files
increase default ADC conversion rate; update example
Increased the default ADC conversion rate from 10 to 20 samples per second to eliminate a racing error where the conversion completion (Cycle Ready) flag wasn't consistently set. The error made it appear that the ADC data wasn't available. Successfully tested conversion rates up to 320 sps. Note: high sample rates are inherently less accurate but may be useful for some applications. Fixed an error in the simpletest example was causing the example to continuously read the ADC data, ignoring the conversion completion (Cycle Ready) flag. Both changes are expected to be transparent to all existing applications.
1 parent 154d219 commit dfcdcb6

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

cedargrove_nau7802.py

100644100755
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2022 JG for Cedar Grove Maker Studios
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 JG for Cedar Grove Maker Studios
22
#
33
# SPDX-License-Identifier: MIT
44
"""
@@ -85,8 +85,8 @@ class Gain:
8585
class ConversionRate:
8686
"""ADC conversion rate settings."""
8787

88-
RATE_10SPS = 0x0 # 10 samples/sec; _CTRL2[6:4] = 0 (chip default)
89-
RATE_20SPS = 0x1 # 20 samples/sec; _CTRL2[6:4] = 1
88+
RATE_10SPS = 0x0 # 10 samples/sec; _CTRL2[6:4] = 0 (chip default, too slow)
89+
RATE_20SPS = 0x1 # 20 samples/sec; _CTRL2[6:4] = 1 (driver default)
9090
RATE_40SPS = 0x2 # 40 samples/sec; _CTRL2[6:4] = 2
9191
RATE_80SPS = 0x3 # 80 samples/sec; _CTRL2[6:4] = 3
9292
RATE_320SPS = 0x7 # 320 samples/sec; _CTRL2[6:4] = 7
@@ -107,7 +107,7 @@ class NAU7802:
107107
def __init__(self, i2c_bus, address=0x2A, active_channels=1):
108108
"""Instantiate NAU7802; LDO 3v0 volts, gain 128, 10 samples per second
109109
conversion rate, disabled ADC chopper clock, low ESR caps, and PGA output
110-
stabilizer cap if in single channel mode. Returns True if successful."""
110+
stabilizer cap if in single channel mode."""
111111
self.i2c_device = I2CDevice(i2c_bus, address)
112112
if not self.reset():
113113
raise RuntimeError("NAU7802 device could not be reset")
@@ -116,7 +116,7 @@ def __init__(self, i2c_bus, address=0x2A, active_channels=1):
116116
self.ldo_voltage = "3V0" # 3.0-volt internal analog power (AVDD)
117117
self._pu_ldo_source = True # Internal analog power (AVDD)
118118
self.gain = 128 # X128
119-
self._c2_conv_rate = ConversionRate.RATE_10SPS # 10 SPS; default
119+
self._c2_conv_rate = ConversionRate.RATE_20SPS # Custom default
120120
self._adc_chop_clock = 0x3 # 0x3 = Disable ADC chopper clock
121121
self._pga_ldo_mode = 0x0 # 0x0 = Use low ESR capacitors
122122
self._act_channels = active_channels
@@ -281,7 +281,7 @@ def reset(self):
281281
"""Resets all device registers and enables digital system power.
282282
Returns the power ready status bit value: True when system is ready;
283283
False when system not ready for use."""
284-
self._pu_reg_reset = True # Reset all registers)
284+
self._pu_reg_reset = True # Reset all registers
285285
time.sleep(0.100) # Wait 100ms; 10ms minimum
286286
self._pu_reg_reset = False
287287
self._pu_digital = True

examples/nau7802_simpletest.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# SPDX-FileCopyrightText: 2021, 2022 Cedar Grove Maker Studios
1+
# SPDX-FileCopyrightText: 2023 Cedar Grove Maker Studios
22
# SPDX-License-Identifier: MIT
33

44
"""
5-
nau7802_simpletest.py 2022-04-23 1.0.1 Cedar Grove Maker Studios
5+
nau7802_simpletest.py 2023-01-12 2.0.0 Cedar Grove Maker Studios
66
77
Instantiates both NAU7802 channels with default gain of 128 and sample
88
average count of 100.
@@ -17,10 +17,9 @@
1717

1818

1919
def zero_channel():
20-
"""Initiate internal calibration for current channel; return raw zero
21-
offset value. Use when scale is started, a new channel is selected, or to
22-
adjust for measurement drift. Remove weight and tare from load cell before
23-
executing."""
20+
"""Initiate internal calibration for current channel.Use when scale is started,
21+
a new channel is selected, or to adjust for measurement drift. Remove weight
22+
and tare from load cell before executing."""
2423
print(
2524
"channel %1d calibrate.INTERNAL: %5s"
2625
% (nau7802.channel, nau7802.calibrate("INTERNAL"))
@@ -29,9 +28,7 @@ def zero_channel():
2928
"channel %1d calibrate.OFFSET: %5s"
3029
% (nau7802.channel, nau7802.calibrate("OFFSET"))
3130
)
32-
zero_offset = read_raw_value(100) # Read 100 samples to establish zero offset
3331
print("...channel %1d zeroed" % nau7802.channel)
34-
return zero_offset
3532

3633

3734
def read_raw_value(samples=100):
@@ -64,7 +61,12 @@ def read_raw_value(samples=100):
6461
### Main loop: Read load cells and display raw values
6562
while True:
6663
print("=====")
67-
for i in range(1, 3):
68-
nau7802.channel = i
64+
nau7802.channel = 1
65+
if nau7802.available():
66+
value = read_raw_value()
67+
print("channel %1.0f raw value: %7.0f" % (nau7802.channel, value))
68+
69+
nau7802.channel = 2
70+
if nau7802.available():
6971
value = read_raw_value()
7072
print("channel %1.0f raw value: %7.0f" % (nau7802.channel, value))

0 commit comments

Comments
 (0)