Page 6 of 8

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 2:28 pm
by Roberthh
What is in line 7?

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 2:34 pm
by likith1268

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 3:13 pm
by Roberthh
That line is ok. Reload the library to the device.

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 3:20 pm
by likith1268
still not working, is there any problem in pico??

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 3:32 pm
by Roberthh
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.

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 3:40 pm
by Roberthh
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.

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 4:21 pm
by likith1268
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)

Re: HX711 - which lib should I use?

Posted: Thu Aug 04, 2022 4:39 pm
by Roberthh
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)

Re: HX711 - which lib should I use?

Posted: Mon Aug 08, 2022 9:05 am
by likith1268
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.

Re: HX711 - which lib should I use?

Posted: Mon Aug 08, 2022 9:40 am
by Roberthh
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.