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 » Thu Aug 04, 2022 2:28 pm

What is in line 7?


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 » Thu Aug 04, 2022 3:13 pm

That line is ok. Reload the library to the device.

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

Re: HX711 - which lib should I use?

Post by likith1268 » Thu Aug 04, 2022 3:20 pm

still not working, is there any problem in pico??

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 » Thu Aug 04, 2022 3:32 pm

On you previous picture with the connections of the hx711 board, Vcc is connected to GND. And the last code snippet still does not match the connections picture.

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 » Thu Aug 04, 2022 3:40 pm

still not working, is there any problem in pico??
What is not working. Do you get the error message or no results?

It's working here with my pico.

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

Re: HX711 - which lib should I use?

Post by likith1268 » Thu Aug 04, 2022 4:21 pm

hello there,

This is the schematic diagram for the load cell with pico.
this could give you a clear understanding of the connections.

I'm keeping the code as well for your reference.

Code: Select all

from hx711_gpio import HX711
from machine import Pin

pin_OUT = Pin(12, Pin.IN, pull=Pin.PULL_DOWN)
pin_SCK = Pin(13, Pin.OUT)

hx711 = HX711(pin_SCK, pin_OUT)

hx711.tare()
value = hx711.read()
value = hx711.get_value()

#this is the hx711_gpio file.

from machine import enable_irq, disable_irq, idle
import time
        
class HX711:
    def __init__(self, pd_sck, dout, gain=128):
        self.pSCK = pd_sck
        self.pOUT = dout
        self.pSCK.value(False)

        self.GAIN = 0
        self.OFFSET = 0
        self.SCALE = 1

        self.time_constant = 0.25
        self.filtered = 0

        self.set_gain(gain);

    def set_gain(self, gain):
        if gain is 128:
            self.GAIN = 1
        elif gain is 64:
            self.GAIN = 3
        elif gain is 32:
            self.GAIN = 2

        self.read()
        self.filtered = self.read()

    def is_ready(self):
        return self.pOUT() == 0

    def read(self):
        # wait for the device being ready
        for _ in range(500):
            if self.pOUT() == 0:
                break
            time.sleep_ms(1)
        else:
            raise OSError("Sensor does not respond")

        # shift in data, and gain & channel info
        result = 0
        for j in range(24 + self.GAIN):
            state = disable_irq()
            self.pSCK(True)
            self.pSCK(False)
            enable_irq(state)
            result = (result << 1) | self.pOUT()

        # shift back the extra bits
        result >>= self.GAIN

        # check sign
        if result > 0x7fffff:
            result -= 0x1000000

        return result

    def read_average(self, times=3):
        sum = 0
        for i in range(times):
            sum += self.read()
        return sum / times

    def read_lowpass(self):
        self.filtered += self.time_constant * (self.read() - self.filtered)
        return self.filtered

    def get_value(self):
        return self.read_lowpass() - self.OFFSET

    def get_units(self):
        return self.get_value() / self.SCALE

    def tare(self, times=15):
        self.set_offset(self.read_average(times))

    def set_scale(self, scale):
        self.SCALE = scale

    def set_offset(self, offset):
        self.OFFSET = offset

    def set_time_constant(self, time_constant = None):
        if time_constant is None:
            return self.time_constant
        elif 0 < time_constant < 1.0:
            self.time_constant = time_constant

    def power_down(self):
        self.pSCK.value(False)
        self.pSCK.value(True)

    def power_up(self):
        self.pSCK.value(False)
        self.filtered = self.read()

    def is_ready(self):
        return self.pOUT() == 0

    def read(self):
        # wait for the device being ready
        for _ in range(500):
            if self.pOUT() == 0:
                break
            time.sleep_ms(1)
        else:
            raise OSError("Sensor does not respond")

        # shift in data, and gain & channel info
        result = 0
        for j in range(24 + self.GAIN):
            state = disable_irq()
            self.pSCK(True)
            self.pSCK(False)
            enable_irq(state)
            result = (result << 1) | self.pOUT()

        # shift back the extra bits
        result >>= self.GAIN

        # check sign
        if result > 0x7fffff:
            result -= 0x1000000

        return result

    def read_average(self, times=3):
        sum = 0
        for i in range(times):
            sum += self.read()
        return sum / times

    def read_lowpass(self):
        self.filtered += self.time_constant * (self.read() - self.filtered)
        return self.filtered

    def get_value(self):
        return self.read_lowpass() - self.OFFSET

    def get_units(self):
        return self.get_value() / self.SCALE

    def tare(self, times=15):
        self.set_offset(self.read_average(times))

    def set_scale(self, scale):
        self.SCALE = scale

    def set_offset(self, offset):
        self.OFFSET = offset

    def set_time_constant(self, time_constant = None):
        if time_constant is None:
            return self.time_constant
        elif 0 < time_constant < 1.0:
            self.time_constant = time_constant

    def power_down(self):
        self.pSCK.value(False)
        self.pSCK.value(True)

    def power_up(self):
        self.pSCK.value(False)
Attachments
Screenshot 2022-08-04 171652.png
Screenshot 2022-08-04 171652.png (93.36 KiB) Viewed 4668 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 » Thu Aug 04, 2022 4:39 pm

Just to repeat myself: the schematic diagram shows, that you connected the hx711 to GPIO18 and 19. Therefore, the Pin definitions must be:

Code: Select all

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

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 9:05 am

Hello,

yes, thanks for your reply.
I have changed the pin connection in the code and when i am trying to calibrate it's not working

when i am running the example_rp2_test.py file , it giving me all zero's
and when i am running scale.py file, it gives me a error saying type object 'SPI' has no attribute 'MASTER'

i am attaching the files below, please look at it.
Attachments
example_rp2_test.png
example_rp2_test.png (41.67 KiB) Viewed 4581 times
scale.png
scale.png (57.58 KiB) Viewed 4581 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 9:40 am

You should read new values in the 'while True' loop, for instance with:

value = hx711.get_value()

Otherwise it will print always the same number. And you cannot use the SPI variant of the driver without changing it. It was made for the Pycom variant of a ESP32 based MicroPython.

Post Reply