Using PIO to read HX711 Load Cell Amplifier: Need some help tidying up

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
lpaelke
Posts: 3
Joined: Sat Mar 13, 2021 9:26 am

Using PIO to read HX711 Load Cell Amplifier: Need some help tidying up

Post by lpaelke » Sat Mar 13, 2021 9:56 am

As a first project using the Pico, I want to build a centre of gravity (cog) scale for RC-planes.

This works by placing one load cell ahead of the cog (say at the leading edge of the wing) and one behind (at the trailing edge). From the distribution of weight between the 2 load cells, one can calculate the position of the cog. There are several examples of such devices using an Arduino to be found on the web, but I would like to use a Pico.

As a first step, I programmed the PIO to communicate with the HX711 load cell amplifier.
My biggest problem was the lack of documentation to program and use the PIO with Python.
So if anyone can point me to some more complete documentation than available from raspberrypi.org, that would be much appreciated.

At the moment I have one specific question: How can I find out in Python, whether data is available in the FIFO?
Currently, I just wait for some time and hope that data is available.

Anyway, here is the code I have written so far:

Code: Select all

#PIO program to read HX711 load cell amplifier channel A with gain 128
'''
 *
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2021 Lutz Paelke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
'''


import time
import rp2
from machine import Pin

@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, set_init=rp2.PIO.IN_LOW)
def hx711():
    set(x, 24)           #generate 25 Pulses 1us on 1us off to initialise hx711 for channel A with gain 128
    label("loop1")
    nop()    .side(1)    [7]
    nop()    [1]
    nop()    .side(0)    [6]
    nop()    [1]
    jmp(x_dec, "loop1")
    
    wrap_target()
    wait(0,pin,0)        #wait for data ready, indicated by low level
    
    set(x, 23)           #generate 24 Pulses...
    label("loop2")
    nop()    .side(1)    [7]
    nop()    [1]
    nop()    .side(0)    [5]   
    in_(pins, 1)         #... to read read 24 bits 
    nop()    [1]
    jmp(x_dec, "loop2")
    nop()    .side(1)    [7]    #generate 1 more pulse to get to 25 pulses
    nop()    [1]
    nop()    .side(0)    [7]
    nop()    [1]
    push()               #write data to FIFO
    
    wrap()
    
    
#Test Program

sm = rp2.StateMachine(0, hx711, freq=10_000_000, sideset_base=Pin(16), in_base=Pin(17))

sm.active(1)

while True:
    time.sleep(0.1)            #THIS IS THE PART I WOULD LIKE TO IMPROVE! (by checking, whether data is available)
    Data=sm.get()              #read Data from FIFO
    print((Data-139700)/257)   #write calibrated Data to console

Lutz

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

Re: Using PIO to read HX711 Load Cell Amplifier: Need some help tidying up

Post by Roberthh » Sat Mar 13, 2021 11:01 am

About the number of values in the FiFo: I made as small set of helper functions, and two of them tell the number of values in the RX and TX fifo. You'll find them here: https://github.com/robert-hh/RP2040-Exa ... r/rp2_util

I made also a PIO based driver for the HX711. You may have a look at it for hints (or simply use it): https://github.com/robert-hh/hx711

lpaelke
Posts: 3
Joined: Sat Mar 13, 2021 9:26 am

Re: Using PIO to read HX711 Load Cell Amplifier: Need some help tidying up

Post by lpaelke » Sat Mar 13, 2021 2:20 pm

Those helper functions are really helpfull and exactly what I was looking for.

And I will certainly take a look at, and probably some inspiration from your HX711 driver.

Thank you very much,

Lutz

cebseb
Posts: 1
Joined: Wed Mar 31, 2021 10:40 pm

Re: Using PIO to read HX711 Load Cell Amplifier: Need some help tidying up

Post by cebseb » Wed Mar 31, 2021 10:46 pm

Any luck in figuring things out for your project? I’m trying to do research into a project using a load cell with the pico and there has yet to be any fleshed out documentation out there for micropython.

Anyways, thanks for getting things rolling!

lpaelke
Posts: 3
Joined: Sat Mar 13, 2021 9:26 am

Re: Using PIO to read HX711 Load Cell Amplifier: Need some help tidying up

Post by lpaelke » Fri Apr 02, 2021 9:28 am

OK, I've got my project finished.
Well sort of. At least I've got it working and doing what I need it to do. And good enough is good enough for me.

To explain a bit further: my main hobby is flying radio controlled models. General making, tinkering and microcontrollers are just supporting hobbies. So with the winter coming to an end and the weather improving in these parts, I needed the cog scale to work in a hurry, in order to balance the model I've built over the winter to get it ready for its maiden flight.

So with my PIO code basically running, I stuck with it.
If you would like to start your own project, I would recommend you use Roberthh’s. It’s much more sophisticated.

I also asked the same question on the Pico forum at raspberrypi.org. The ensuing discussion over there led me to make some measurements of the timing of my program. From those I realised that the .get() method in the Micropython implementation waits for data. (In the C implementation there are apparently 2 different methods, one which waits for data, and on which doesn’t.)
So by taking 80 measurements, not only do I get a good average, but the resulting update rate of about 1s is quite pleasing for the eye.

So without further ado, here is my quick and dirty code:

Code: Select all

#Center of gravity scale program for the Raspberry Pi Pico
#using the PIO to read HX711 load cell amplifier channel A with gain 128
'''
 *
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2021 Lutz Paelke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
'''


import time
import rp2
from machine import Pin, I2C
from sh1106 import SH1106_I2C



@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, set_init=rp2.PIO.IN_LOW)
def hx711():
    set(x, 24)           #generate 25 Pulses 1us on 1us off to initialise hx711 for channel A with gain 128
    label("loop1")
    nop()    .side(1)    [7]
    nop()    [1]
    nop()    .side(0)    [6]
    nop()    [1]
    jmp(x_dec, "loop1")
    
    wrap_target()
    wait(0,pin,0)        #wait for data ready, indicated by low level
    
    set(x, 23)           #generate 24 Pulses...
    label("loop2")
    nop()    .side(1)    [7]
    nop()    [1]
    nop()    .side(0)    [5]   
    in_(pins, 1)         #... to read read 24 bits 
    nop()    [1]
    jmp(x_dec, "loop2")
    nop()    .side(1)    [7]    #generate 1 more pulse to get to 25 pulses
    nop()    [1]
    nop()    .side(0)    [7]
    nop()    [1]
    push()               #write data to FIFO
    
    wrap()
    


Fakt0 = 387   #Calibration factor for load cell0
Fakt1 = 397

sm0 = rp2.StateMachine(0, hx711, freq=10_000_000, sideset_base=Pin(18), in_base=Pin(19))
sm1 = rp2.StateMachine(1, hx711, freq=10_000_000, sideset_base=Pin(16), in_base=Pin(17))

WIDTH  = 128
HEIGHT = 64

i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=200000)
oled = SH1106_I2C(WIDTH, HEIGHT, i2c, None, 0x3c, False)


#tare

oled.fill(0)
oled.text("Initialisierung",0,0)
oled.show()

sm0.active(1)
sm1.active(1)


Kal0 = 0
Kal1 = 0

Kal_i = 80    # number of measurements to average for tare

for i in range(Kal_i):
    Kal0 = Kal0 + sm0.get()
    Kal1 = Kal1 + sm1.get()
    
Kal0 = Kal0 / Kal_i
Kal1 = Kal1 / Kal_i


#main loop

n = 80    #number of measurements to average

while True:
    Data0 = 0
    Data1 = 0
    for i in range(n):
        Data0 = Data0 + sm0.get()              #read Data from FIFO, .get() waits for data
        Data1 = Data1 + sm1.get()
    Data0 = Data0 / n
    Data1 = Data1 / n
    
    m0 = (Data0-Kal0)/Fakt0
    m1 = (Data1-Kal1)/Fakt1
    
    m = m0 + m1
    x = 290 * m1 / m + 5    #front and back supports are 290 mm appart, the stop bar is 5 mm from the front support
    
    oled.fill(0)
    oled.text("cog: "+str(round(x))+" mm",0,0)
    oled.text(" m = "+str(round(m))+" g",0,10)
    oled.show()
   

Post Reply