[ad_1]
The main CircuitPython code module, code.py, prepares and operates the Clue Coffee Scale. Before using the scale, you’ll need to update code.py with a calibration ratio that’s unique to the load cell you attached to the NAU7802 breakout. First let’s take a walk through the code and look at how each section works.
8a. Import and set defaults
This section imports all the needed modules and libraries, including the NAU7802 sensor driver. After importing, the first visible task of this section is to turn the Clue board’s NeoPixel LED yellow to indicate that the Coffee Scale is initializing. During operation, the indicator LED will glow green when operating normally or red when zeroing the scale.
Next, the scale defaults are specified. These are constants with names that are capitalized to help identify them as constants. You can change these values to alter the operation of the scale:
MAX_GR is the full-scale value of the scale in grams. The tick mark values adjacent to the graduated scale graphic are automatically derived from MAX_GR which can be any positive integer value. Since the graduated scale graphic is divided into tenths of full-scale, with five numbered tick marks, it’s best to choose an integer MAX_GR value that will display nicely such as 1000, 500, 250, 100, 50, 25, 10, or 5.
DEFAULT_GAIN is the gain setting of the NAU7802 internal ADC’s incoming signal pre-amplifier. Normally, this is set to the highest value of 128.
SAMPLE_AVG specifies the number of measurements that are averaged when the load cell is measured. The measurement value will be more stable with a high SAMPLE_AVG value. The display will update more slowly with a higher value; it’s a trade-off between stability and speed.
8b. Load cell calibration ratio
CALIB_RATIO is the factor that is used to convert the NAU7802’s raw measurement value into grams. This ratio will need to be updated after running the calibrator method for your particular load cell in Step 9 below. You should only have to measure and record this calibration ratio once.
8c. Instantiate the sensor and display
The NAU7802 board is connected to the Clue’s I2C bus and lives at address 42 (hexadecimal 2A). The NAU7802 24-bit ADC chip is capable of supporting two load cell channels, but only the first channel is available on the Adafruit Stemma breakout board; active_channels is therefore set to 1.
Next, the Clue’s integrated display is instantiated along with the primary displayio graphics group layer, scale_group, which will contain the background bitmap image and other display layers.
Font objects for displaying measurement values and the tick mark labels are defined next.
8d. Display the background bitmap image
Figure U
The display background file that includes the graduated scale (Figure U) is read from the Clue root directory and is appended as the first item in the primary displayio group layer. All other graphics objects will be put on layers over this background.
8e. Define and display text and graphics
This section defines the graphic elements that will be placed in front of the background graphic. First, the zeroing button graphic is appended to the primary displayio group.
The subsection that starts with for i in range… is the code that steps through the graduated scale’s tick marks, creating a value label that’s calculated using the MAX_GR constant and appending each label to the displayio group. Measurement values and units labels are added next.
Lastly, the indicator_group with its floating indicator bubble is created and added to the primary displayio group. The indicator_group layer is now the front-most graphics layer of the display (Figure V).
The indicator bubble is a yellow circle that travels up and down the graduated scale, pointing to the measured value. The center of the circle will normally be transparent, but will appear yellow or red depending on the scale’s current status; yellow when initializing, red when zeroing.
8f. Helpers
Two helper functions work with the NAU7802 driver class to zero the scale and to read the load cell’s current raw value:
The zero_channel( ) helper sets up and zeros the NAU7802’s internal amplifiers and ADC (analog to digital converter) to prepare it to receive signals. This function is used when the scale is first powered up, and when it’s manually zeroed by pressing the Clue board’s A button.
The load cell’s raw measurement value is obtained by the read( ) helper. This helper accepts an integer parameter that specifies the number of samples to be averaged each time the helper is called, defaulting to one sample (no averaging). The helper returns the averaged raw measurement value. Averaging the raw value can help reduce the jitter of the displayed value.
8g. Activate sensor and prepare to loop
This is where the NAU7802 ADC is enabled and calibrated for use. Before calibrating and zeroing, the internal sensor amplifier’s gain is set to the default value. Once completed, the Clue will chirp some welcoming notes.
8h. The primary code loop
The primary loop is the main operational process of the scale. The loop indicates the scale’s status on the Clue board’s NeoPixel; green when the scale is operating, red when it’s busy zeroing.
After setting the NeoPixel color, the load cell raw value is measured and converted to grams and ounces. The converted values are formatted and placed into the corresponding on-screen display labels and are also printed in the REPL.
The grams measurement value is used to position the on-screen indicator bubble along the graduated scale graphic using the map_range( ) function. Also, if the grams measurement value falls outside of the minimum or maximum range, the bubble is “parked” at the extreme position and its interior color is changed from transparent to red.
Finally, the Clue board’s A button is watched to see if it was pressed. When pressed, the Clue plays a sound and will begin zeroing the scale. During the zeroing process, the NeoPixel, the zeroing button graphic, and the center of the bubble are all set to a red color. After zeroing has completed, the code waits until the button is released before playing a completion sound, setting the bubble and zeroing button graphic interiors to transparent, and returning the Clue Coffee Scale to normal operation.
# The Primary Code Loop
# Read sensor, move bubble, display values
while True:
clue.pixel[0] = clue.GREEN # Set status
indicator to green (ready)
# Read the raw scale value and scale for
grams and ounces
value = read(SAMPLE_AVG)
mass_grams = round(value * CALIB_RATIO, 1)
mass_ounces = round(mass_grams * 0.03527, 2)
grams_value.text = f”{mass_grams:5.1f}”
ounces_value.text = f”{mass_ounces:5.2f}”
print(f” {grams_value.text} grams
{ounces_value.text} ounces”)
# Reposition the indicator bubble based
on grams value
min_gr = (MAX_GR // 5) * –1 # Minimum
display value
bubble.y = int(map_range(mass_grams,
min_gr, MAX_GR, 240, 0)) – 10
if mass_grams > MAX_GR or mass_grams <
min_gr:
bubble.fill = clue.RED
else:
bubble.fill = None
# Check to see if zeroing button is pressed
if clue.button_a:
# Zero the sensor
clue.pixel[0] = clue.RED # Set
status indicator to red (stopped)
bubble.fill = clue.RED # Set bubble
center to red (stopped)
zero_button_circle.fill = clue.RED #
Set to red (stopped)
clue.play_tone(1660, 0.3) # Play
“button pressed” tone
zero_channel()
while clue.button_a:
# Wait until button is released
time.sleep(0.1)
clue.play_tone(1440, 0.5) # Play
“reset completed” tone
zero_button_circle.fill = None # Set
to transparent (ready)
bubble.fill = None # Set bubble
center to transparent (ready)
[ad_2]