Simple test

Ensure your device works with this simple test.

examples/nau7802_simpletest.py
 1# SPDX-FileCopyrightText: 2023 Cedar Grove Maker Studios
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5nau7802_simpletest.py  2023-01-13 2.0.2  Cedar Grove Maker Studios
 6
 7Instantiates two NAU7802 channels with default gain of 128 and sample
 8average count of 2.
 9"""
10
11import time
12import board
13from cedargrove_nau7802 import NAU7802
14
15# Instantiate 24-bit load sensor ADC; two channels, default gain of 128
16nau7802 = NAU7802(board.I2C(), address=0x2A, active_channels=2)
17
18
19def zero_channel():
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."""
23    print(
24        "channel %1d calibrate.INTERNAL: %5s"
25        % (nau7802.channel, nau7802.calibrate("INTERNAL"))
26    )
27    print(
28        "channel %1d calibrate.OFFSET:   %5s"
29        % (nau7802.channel, nau7802.calibrate("OFFSET"))
30    )
31    print("...channel %1d zeroed" % nau7802.channel)
32
33
34def read_raw_value(samples=2):
35    """Read and average consecutive raw sample values. Return average raw value."""
36    sample_sum = 0
37    sample_count = samples
38    while sample_count > 0:
39        while not nau7802.available():
40            pass
41        sample_sum = sample_sum + nau7802.read()
42        sample_count -= 1
43    return int(sample_sum / samples)
44
45
46# Instantiate and calibrate load cell inputs
47print("*** Instantiate and calibrate load cells")
48# Enable NAU7802 digital and analog power
49enabled = nau7802.enable(True)
50print("Digital and analog power enabled:", enabled)
51
52print("REMOVE WEIGHTS FROM LOAD CELLS")
53time.sleep(3)
54
55nau7802.channel = 1
56zero_channel()  # Calibrate and zero channel
57nau7802.channel = 2
58zero_channel()  # Calibrate and zero channel
59
60print("READY")
61
62### Main loop: Read load cells and display raw values
63while True:
64    print("=====")
65    nau7802.channel = 1
66    value = read_raw_value()
67    print("channel %1.0f raw value: %7.0f" % (nau7802.channel, value))
68
69    nau7802.channel = 2
70    value = read_raw_value()
71    print("channel %1.0f raw value: %7.0f" % (nau7802.channel, value))