HX711 - which lib should I use?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: HX711 - which lib should I use?

Post by Roberthh » Mon Aug 08, 2022 9:51 am

To use the SPI variant, you test code may start with:

Code: Select all

from machine import SPI, Pin
from hx711_spi import *

pin_OUT = Pin(12, Pin.IN, pull=Pin.PULL_DOWN)
pin_SCK = Pin(11, Pin.OUT)
spi_SCK = Pin(10)

spi = SPI(1, baudrate=1000000, polarity=0,
          phase=0, sck=spi_SCK, mosi=pin_SCK, miso=pin_OUT)

hx = HX711(pin_SCK, pin_OUT, spi)

print("Raw HX711 value", hx.read())
Note that the Pin assignments are now GPIO12 for HX711 Data and GPIO11 for HX711 Clock. The SPI clock pin is NOT USED, but has to be defined.

likith1268
Posts: 15
Joined: Thu Aug 04, 2022 10:46 am

Re: HX711 - which lib should I use?

Post by likith1268 » Mon Aug 08, 2022 10:49 am

okay,
can you please elaborate what I should keep inside the while loop in order to print the weights?
and to print the new values.
Attachments
example_rp2_test.png
example_rp2_test.png (41.67 KiB) Viewed 4987 times

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

Re: HX711 - which lib should I use?

Post by Roberthh » Mon Aug 08, 2022 2:18 pm

Nothing that cannot be found in even the most basic Python textbook.

Code: Select all

import time
while True:
    if hx711.is_ready():
        print(hx711.get_value())

likith1268
Posts: 15
Joined: Thu Aug 04, 2022 10:46 am

Re: HX711 - which lib should I use?

Post by likith1268 » Mon Aug 08, 2022 2:33 pm

I tried adding the code in the while loop which you gave, but getting the same zeros in the output.

I need the weight of the product as the output but it's showing all zeros.
Attachments
example.png
example.png (55.17 KiB) Viewed 4963 times

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

Re: HX711 - which lib should I use?

Post by Roberthh » Mon Aug 08, 2022 3:01 pm

So I hooked up a HX711 to a Pico again. Test script:

Code: Select all

from machine import Pin
from hx711_gpio import HX711
import time

pin_OUT = Pin(18, Pin.IN, pull=Pin.PULL_DOWN)
pin_SCK = Pin(19, Pin.OUT)

hx = HX711(pin_SCK, pin_OUT)

hx.set_gain(128)
time.sleep_ms(50)
hx.tare()

while True:
    if hx.is_ready():
        print(hx.get_value())
    time.sleep(0.1)
First few lines of output:
10.5625
11.6875
2.03125
2.03125
3.03125
10.53125
13.65625
18.25
-3.546875
-32.14063
-27.34375
-21.75
-22.79688
-26.57813
-32.67188
-36.73438
-31.03125
-20.25
-9.421875
8.203125
Do you have a load cell connected to the HX711?

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

Re: HX711 - which lib should I use?

Post by Roberthh » Mon Aug 08, 2022 3:03 pm

Note: Vcc to the HX711 must be 3.3V, to match the Pico Pin levels.

likith1268
Posts: 15
Joined: Thu Aug 04, 2022 10:46 am

Re: HX711 - which lib should I use?

Post by likith1268 » Mon Aug 08, 2022 3:47 pm

yes, it worked..

Thank you so much for that 3.3v, we fixed it and getting the values

can we know how to calibrate the values, I have attached scale.py offset values which is -404751.

can you please help with that thing too, it would be so great if you could help with this.
Attachments
Screenshot (112).png
Screenshot (112).png (190.78 KiB) Viewed 4947 times
Screenshot (111).png
Screenshot (111).png (188.72 KiB) Viewed 4947 times

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

Re: HX711 - which lib should I use?

Post by Roberthh » Mon Aug 08, 2022 4:09 pm

You must use a known weight (or force), take a reading without that weight, a reading with the weight, and the difference divided by the weight result in an offset and scaling. So lets say:

w = 1000 # Weight 1000g
offset = hx711.read_average(100)
# apply the weight
r_load = hx711.read_average(100)

scaling = (r_load - offset) / w
# The unit of the scaling is readings/g
# If you then read a value, you calculate:
# ti get a weight in g
weight = (hx711.read_average() - offset) / scaling

likith1268
Posts: 15
Joined: Thu Aug 04, 2022 10:46 am

Re: HX711 - which lib should I use?

Post by likith1268 » Mon Aug 08, 2022 7:02 pm

sorry to bother you again, but I need some clarity on calibration thing.

so, when I am running example_rp2_test.py file it's printing the value continuously with different numbers and simultaneously, i am getting one value when running scale.py file (this is without load)

lly, when applied load the readings are increasing acccording to load.

so, may i know how to take the offset value??

is the output which i got by running scale.py is the offset value???
Attachments
Screenshot (112).png
Screenshot (112).png (190.78 KiB) Viewed 4919 times
Screenshot (111).png
Screenshot (111).png (188.72 KiB) Viewed 4919 times

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

Re: HX711 - which lib should I use?

Post by Roberthh » Mon Aug 08, 2022 7:20 pm

hx.tare() take the sensor reading at the time of the call and subtracts it from all following readings. This value is internally called offset. What I am curious about is, that even after call hx.tare() the following readings are ~3000. They should be around 0, at least when the series starts.
Which kind of load cell is attached to the HX711.

Post Reply