Core2 as a scale - measured value jumps - measurement accurate to the gram?



  • Hi,
    I have now bought this M5Stack Core2. It's a nifty little thing with great application possibilities. So now my first project with it...

    I want to determine the weight of food in a food bowl. I have a "scale kit" with 4 weight sensors (HX711) for the M5Stack Core2. I actually wanted to program via UIFlow 2, which looks very appealing and simple. But as a newbie, it's a bit overwhelming. So I switched to Python after all...

    So, everything is plugged in (absolutely simple) and I already have measured values on the display. But they still fluctuate too much. Countless measured values come in every second. So the idea is to collect the measured values for half a second and then only show the average value on the display. Unfortunately, I've been out of Python programming for so long that I can't do it without your help.

    Code:
    import os, sys, io
    import M5
    from M5 import *
    from hardware import *
    from unit import WEIGHTUnit

    label0 = None
    title0 = None
    label1 = None
    label2 = None
    weight_0 = None

    import math

    gewicht = None
    gewicht1 = None

    def btnA_wasClicked_event(state):
    global label0, title0, label1, label2, weight_0, gewicht, gewicht1
    weight_0.set_tare()

    def setup():
    global label0, title0, label1, label2, weight_0, gewicht, gewicht1

    weight_0 = WEIGHTUnit((33,32))
    M5.begin()
    Widgets.fillScreen(0x222222)
    label0 = Widgets.Label("label0", 45, 87, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu72)
    title0 = Widgets.Title("Futter/Feed", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu40)
    label1 = Widgets.Label("Kg", 47, 163, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu24)
    label2 = Widgets.Label(">0<", 49, 224, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu12)

    BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btnA_wasClicked_event)

    def loop():
    global label0, title0, label1, label2, weight_0, gewicht, gewicht1
    M5.update()
    gewicht = (weight_0.get_scale_weight) / 52000
    gewicht1 = round(gewicht, 1)
    if gewicht1 <= 0.00:
    gewicht1 = 0.00
    label0.setText(str(gewicht1))
    sleep(1)

    if name == 'main':
    try:
    setup()
    while True:
    loop()
    except (Exception, KeyboardInterrupt) as e:
    try:
    from utility import print_error_msg
    print_error_msg(e)
    except ImportError:
    print("please update to latest firmware")


    Please help :-)
    Thank you



  • @dreamer This is 'roughly' how I average temperatures from sensor

    float room_temp_read[5] = { 25, 25, 25, 25, 25 };   //array of five reads
    int x = 1;  //(declared in Setup)
    //Read and Average subroutine
    if (x > 4) {
        x = 0;
      }
    
    room_temp_read[x] = tempC;
    room_tempAVG = (room_temp_read[0]
                  + room_temp_read[1]
                  + room_temp_read[2]
                  + room_temp_read[3]
                  + room_temp_read[4]
                  + room_tempAVG * 8)
                     / 13;   //five reads + weighted average equals 13
       x++;  //increment to next array pointer
    

    There are five reads 0-4 and a heavy weighted average (times 8) of the last average results
    This is Arduino C, but just change the grammar for Python.
    -Terry