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 » Tue Aug 02, 2022 2:18 pm

Which variant of (Micro-)Python do you use? And which HX711 lib?
I'm a little bit puzzled by the constructor

hx711 = HX711(5,6)

Because HX711 expects pin objects top be supplied. But maybe you use a different lib.

kiranmai1268
Posts: 11
Joined: Tue Jul 19, 2022 11:26 am

Re: HX711 - which lib should I use?

Post by kiranmai1268 » Tue Aug 02, 2022 3:35 pm

we are trying to run code using RPi 4b
and i'm running in things in thonny.

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 » Tue Aug 02, 2022 3:47 pm

Which HX711 lib do you use?

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: HX711 - which lib should I use?

Post by scruss » Tue Aug 02, 2022 4:06 pm

Roberthh wrote:
Tue Aug 02, 2022 3:47 pm
Which HX711 lib do you use?
They're not using MicroPython, Robert: they're using the HX711 library for Python on a Raspberry Pi 4.

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 » Tue Aug 02, 2022 4:20 pm

So nothing to do for me.

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 11:09 am

Hi there,

we are working on 1kg load cells and HX711 amplifier. I have tried multiple source codes to run on raspberry pi Pico. however unfortunately, the code is working but the thing is printing only zero's as the output.

I tried to change the code but it doesn't work.
I'm attaching the link of the video with our connections and code

https://1drv.ms/v/s!Ah1A9_q5Vo-jhUxWyfL ... B?e=Ue6dmO

here, the code which we used.

https://github.com/robert-hh/hx711

it would be so great if you could help with that.

Thank You.

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 12:12 pm

The video is almost useless. It is better if you write down, which connection you made. The pins have names & numers. And you can as well paste your code here. No need to show a video. At least, it seems that you connected the HX711 to GPIO 2 and 3 (Pin(2) and Pin(3)). So these numbers should be in our script.

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 1:46 pm

The pins we used are pin 12 and pin 13
I'm attaching the image of the connections we made.
https://1drv.ms/u/s!Ah1A9_q5Vo-jhU0JsnG ... B?e=ZPWmKL

and here is the code we used.

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()

THESE ARE THE CODES....

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)

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 1:57 pm

According to the picture, you connect to SCK to GPIO19 and DOUT to GPIO18. So the setting 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 » Thu Aug 04, 2022 2:19 pm

why this error occuring?/

earlier the pico, everything is working fine but suddenly it is showing the option for saving the files in pico as well.

https://1drv.ms/u/s!Ah1A9_q5Vo-jhU7gQP0 ... O?e=Uvgsui

Post Reply