Recent

Author Topic: Reading HX711 with RPI_HAL library  (Read 4406 times)

emptydata

  • Newbie
  • Posts: 1
Reading HX711 with RPI_HAL library
« on: October 25, 2021, 04:50:30 pm »
Hello all,

I'm thinking about trying to work out an alternative reading SPI data from the HX711 chip based on this link (https://github.com/tatobari/hx711py/blob/master/example.py), using Raspberry Pi 3B+ running Raspbian; first, i thought about embedding this python code and using it as a "wrapper" for the lazarus application. However, I was researching and I found the alternative "RPI_HAL" and I thought it would be a better option, as it would be 100% in Pascal. Does anyone have suggestions and could help me with sample code?

The objective would be the mass of objects, using a load cell coupled to the HX711.
« Last Edit: October 25, 2021, 04:52:28 pm by emptydata »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Reading HX711 with RPI_HAL library
« Reply #1 on: October 25, 2021, 05:57:03 pm »
I can't remember where I got to with SPI. However I've certainly used both I2C and GPIO going direct to kernel facilities, so if you have any level of confidence working at that level I'd suggest it might be worthy of research before assuming that too many intermediate layers are necessary.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Reading HX711 with RPI_HAL library
« Reply #2 on: October 26, 2021, 08:21:18 am »
Although the timing specifications for the HX711 clock pulse is relatively relaxed, it may still be problematic to read the data using bit bang IO within a preemptive multitasking OS because the user doesn't have much control to prevent getting interrupted.  It seems as if some users work around this by running processes with high priority, but that doesn't provide any guarantees. Perhaps offloading the data reading part to hardware such as SPI makes sense here.

But then again the typical Python examples seem to just bang the IO clock pin with no regard for timing control.  I assume that Linux + Python is heavy enough to slow a GHz processor down to the meet the 0.2 microsecond minimum clock pulse width.

Maybe I'm over thinking this?  Just try RPI_HAL, bitbang the clock pulse and maybe measure the time to read a value to check the overall timing, maybe using this Python code as template for your pascal code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Reading HX711 with RPI_HAL library
« Reply #3 on: October 26, 2021, 09:26:53 am »
I'm not sure where I got the idea that it was an SPI device, my apologies if it's wildly inaccurate.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Reading HX711 with RPI_HAL library
« Reply #4 on: October 26, 2021, 01:19:26 pm »
I'm not sure where I got the idea that it was an SPI device, my apologies if it's wildly inaccurate.
I was just commenting on the Python code which drives the communication with simple bitbang logic. The actual serial protocol seems like half duplex SPI (without a select pin), but after reading 24 bits data, extra clock pulses are required to indicate gain and channel for the next conversion. Since the SPI peripheral on RPi can be configured for bit length, it seems doable using SPI.

But please note I haven't tested this, everything I say here is merely speculation...

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Reading HX711 with RPI_HAL library
« Reply #5 on: October 26, 2021, 01:40:56 pm »
I was just commenting on the Python code which drives the communication with simple bitbang logic. The actual serial protocol seems like half duplex SPI (without a select pin), but after reading 24 bits data, extra clock pulses are required to indicate gain and channel for the next conversion. Since the SPI peripheral on RPi can be configured for bit length, it seems doable using SPI.

I think you're right, and (having checked again) there's plenty on Google juxtaposing that chip/module and SPI. But I think it's only fair to highlight any areas in which I might possibly be giving out bad advice to a less-experienced user... if for no other reason than that there are plenty of users more experienced than I, who might feel the need to slap me down ungraciously if I do not :-)

If I've used Linux SPI at all, it was strictly from a Raspberry Pi to a piggybacked Arduino-like board rather than to a specific sensor, but I managed to lose a few chunks of sourcecode due to a damaged SDCard so I'm not sure I still have a copy.

I have, however looked quite a lot at GPIO, and would make some observations.

First, avoid anything that uses any kind of memory-mapped I/O for GPIO, since it will be hopelessly platform-specific... and might in practice be tied to a particular RPi model.

Second, the portable /sys interface has limited performance and can be extremely difficult to use since there is no provision for locking a device once it's in use so a different shell session can add/remove GPIO bit etc.

Third, the newer portable /dev interface solves some of these problems but is badly broken in some areas: see https://forum.lazarus.freepascal.org/index.php/topic,56063.msg417205.html#msg417205 and overall thread.

And most importantly, before getting too embroiled with any particular middleware or library find out which of the above it's using.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Reading HX711 with RPI_HAL library
« Reply #6 on: October 27, 2021, 11:01:51 pm »
For reference, reading HX711 with AVR using FPC.

 

TinyPortal © 2005-2018