[SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
pythoncoder
Posts: 4572
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by pythoncoder » Fri Dec 29, 2017 8:13 am

The problem is with the myus variable. You are issuing += against this when it has not been assigned an initial value. The following line 8 should fix it.

Code: Select all

    def read(self):
        self.powerUp()
        while not self.isready():
                pass
        print("<waiting finished> dataPin: {}, sckPin: {}".format(self.dataPin.value(), self.pdsckPin.value()))
        my = 0
        now = utime.ticks_us()
        myus = ''  # ***** add this line *****
        for i in range(24):
            now = utime.ticks_us()
            self.pdsckPin.value(1)
            self.pdsckPin.value(0)
            data = self.dataPin.value()
            myus += ", " + str(utime.ticks_diff(utime.ticks_us(), now))
            mydata += str(data)
            my = ( my << 1) | data
            print("bitbanged: ", my)
            print("us: ", myus)
            print("data: ", mydata)
Peter Hinch

delarge
Posts: 3
Joined: Mon Dec 18, 2017 4:04 pm

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by delarge » Sun Dec 31, 2017 8:13 pm

thank you for the reply mate!

I've added the line:

def read(self):
self.powerUp()
while not self.isready():
pass
print("<waiting finished> dataPin: {}, sckPin: {}".format(self.dataPin.value(), self.pdsckPin.value()))
my = 0
now = utime.ticks_us()
myus = ''
for i in range(24):
now = utime.ticks_us()
self.pdsckPin.value(1)
self.pdsckPin.value(0)
data = self.dataPin.value()
myus += ", " + str(utime.ticks_diff(utime.ticks_us(), now))
mydata += str(data)
my = ( my << 1) | data
print("bitbanged: ", my)
print("us: ", myus)
print("data: ", mydata)

for i in range(3):
self.pdsckPin.value(1)
utime.sleep_us(2)
self.pdsckPin.value(0)
self.powerDown()

but sadly the error remains:

>>> HX711()
<waiting finished> dataPin: 0, sckPin: 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 14, in __init__
File "<stdin>", line 34, in read
NameError: local variable referenced before assignment

I would appreciate any other hint!

Thanks

User avatar
pythoncoder
Posts: 4572
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by pythoncoder » Tue Jan 02, 2018 1:50 pm

I think you have made the same mistake with mydata.
Peter Hinch

delarge
Posts: 3
Joined: Mon Dec 18, 2017 4:04 pm

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by delarge » Tue Jan 09, 2018 12:52 pm

Indeed I have. Thank you mate.

Best Regards.

oliverr
Posts: 5
Joined: Mon May 08, 2017 5:16 am
Location: Melbourne, Australia
Contact:

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by oliverr » Wed Aug 29, 2018 10:05 pm

Hey guys, just wanted to say thank you for the information in this thread, helped me get my HX711 load sensor up and running with less hassle! I've put a bit of a class together that should hopefully help any future HX711 users, can find it on my Github: https://github.com/HowManyOliversAreThe ... mpy-driver

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

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by Roberthh » Thu Aug 30, 2018 5:55 am

That one looks fine. Adding gain is just a minor additional step. For interest, I also made a driver using SPI. In that case, MOSI is used as the clock signal. The SPI clock signal is not used. The core of the class is below. It was made for a LoPy, so it does not immediately run. The interesting parts are the data structures for clock and the lookup table for fast transformation. The reason for using SPI was, that the ESP32 just like the ESP8266 is not good at fast toggling and reading, eventually not matching the timing requirements of the HX711. Using SPI ensures fast timing.

Code: Select all

class HX711:
    def __init__(self, dout, pd_sck, spi_clk, gain=128):

        self.pSCK = Pin(pd_sck , mode=Pin.OUT)
        self.pOUT = Pin(dout, mode=Pin.IN, pull=Pin.PULL_DOWN)
        self.spi = SPI(0, mode=SPI.MASTER, baudrate=1000000, polarity=0, phase=0, pins=(spi_clk, pd_sck, dout))
        self.pSCK(0)

        self.clock_25 = b'\xaa\xaa\xaa\xaa\xaa\xaa\x80'
        self.clock_26 = b'\xaa\xaa\xaa\xaa\xaa\xaa\xa0'
        self.clock_27 = b'\xaa\xaa\xaa\xaa\xaa\xaa\xa8'
        self.clock = self.clock_25
        self.lookup = (b'\x00\x01\x00\x00\x02\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
                       b'\x04\x05\x00\x00\x06\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
                       b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
                       b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
                       b'\x08\x09\x00\x00\x0a\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
                       b'\x0c\x0d\x00\x00\x0e\x0f')
        self.in_data = bytearray(7)

        self.set_gain(gain);

    def set_gain(self, gain):
        if gain is 128:
            self.clock = self.clock_25
        elif gain is 64:
            self.clock = self.clock_27
        elif gain is 32:
            self.clock = self.clock_26
        self.read()

    def read(self):
        # wait for the device to get ready
        while self.pOUT() != 0:
            idle()

        # get the data and set channel and gain
        self.spi.write_readinto(self.clock, self.in_data)

        # pack the data into a single value
        result = 0
        for _ in range (6):
            result = (result << 4) + self.lookup[self.in_data[_] & 0x55]

        # return sign corrected result
        return result - ((result & 0x800000) << 1)

loadcellengineer
Posts: 1
Joined: Sat Jan 04, 2020 2:29 am

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by loadcellengineer » Sat Jan 04, 2020 2:42 am

You might want to read this info on instructables for the Hx711. It has complete specs but you might need an account.
I use the load cell manufactured in the US by transducer Techniques. They are really helpfull too.

monkeyjacob17
Posts: 1
Joined: Sun Mar 15, 2020 5:09 pm

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by monkeyjacob17 » Sun Mar 15, 2020 5:15 pm

@Roberthh : How did you calibrate the HX711 with a lopy? How do you define the calibrate parameter?
Thanks,

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

Re: [SOLVED/WORKEDAROUND] Having problems with HX711 load cell sensor

Post by Roberthh » Sun Mar 15, 2020 7:15 pm

I did not calibrate yet. It depends on the actual load cell you are using and your mechanical set-up. Then you would take the zero-load value and the value for a known load and use that for calibration with a simple linear equation approach ((actual_value - zero_load_value) * scaling) , assuming that the load cell works reasonable linear. The methods tare(), set_offset() and set_scale() are provided for that purpose.
You might have to use different value for different temperatures.

Post Reply