HX711 readout

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
megb123
Posts: 6
Joined: Mon Dec 13, 2021 11:50 am

HX711 readout

Post by megb123 » Mon Jan 24, 2022 2:14 pm

Hey, we are currently using an HX711 weight sensor using a 2kg load cell with a raspberry pi Pico in Micropython.

Currently, any values obtained from the load cell are all extremely erratic, either the same number 8388607 or 0, regardless of any weight used. Would anyone have any suggestions? This is our current code:

from hx711 import HX711
import utime
from machine import Pin

pin_OUT = Pin(20, Pin.IN, pull=Pin.PULL_DOWN)
pin_SCK = Pin(21, Pin.OUT)

hx711 = HX711(pin_SCK, pin_OUT)

while True:
value = hx711.read()
print(value)
utime.sleep(.5)

We have changed the hardware, but is there a possibility it could be broken? Or is it maybe the library or this code? Is this because we have not yet calibrated it?
Please send any suggestions.
Thanks.

megb123
Posts: 6
Joined: Mon Dec 13, 2021 11:50 am

Re: HX711 readout

Post by megb123 » Mon Jan 24, 2022 2:37 pm

Hey, we have resolved this issue! However we are having trouble calibrating our HX711 weight sensor with a 2kg load cell. Currently, it is reading out values in bit form, and we are unsure how to calibrate it to read out in kg or g. We are using micropython, but we can only find answers in C++/C. (Using with a Raspberry Pi Pico)
Would anyone have any suggestions?
Thank you.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: HX711 readout

Post by Roberthh » Mon Jan 24, 2022 5:21 pm

You have to do the calibration yourself, but that is pretty simple. Just a linear transformation in the form y = a * (x - b)
hx711.read() returns a number. Small for no or small weights, larger for heavier weights.
First take a reading with no weight applied.

offset = hx711.read()

then use a good known reference weight, preferably 1 KG, and take the reading for that weigth.

ref_load = hx711.read()

Then you can calculate the scaling factor as

scaling = ref_load - offset

And get the final kg value as:

weight = (hx711.read() - offset) / scaling

Since the HX711 ADC is pretty noisy, you may have to take an average of a set of readings.

Post Reply