Raspberry Pi HX711 Python Bindings
Python bindings for Raspberry Pi HX711 C++ Library
- Use with Raspberry Pi
- Read from a HX711 using Python
- Code tested inside virtual Raspberry Pi Zero/3/4 environments on GitHub and builds automatically uploaded to PyPI
The .gif above illustrates the output of a simple Python script on a Raspberry Pi Zero W where the HX711 chip was operating at 80Hz. In this example, each time the
.weight function is called the median of three samples was used to calculate the weight in grams.
from HX711 import * # create a SimpleHX711 object using GPIO pin 2 as the data pin, # GPIO pin 3 as the clock pin, -370 as the reference unit, and # -367471 as the offset hx = SimpleHX711(2, 3, -370, -367471) # set the scale to output weights in ounces hx.setUnit(Mass.Unit.OZ) # constantly output weights using the median of 35 samples while True: print(hx.weight(35)) #eg. 1.08 oz
from HX711 import * from datetime import timedelta # create an AdvancedHX711 object using GPIO pin 2 as the data pin, # GPIO pin 3 as the clock pin, -370 as the reference unit, -367471 # as the offset, and indicate that the chip is operating at 80Hz hx = AdvancedHX711(2, 3, -370, -367471, Rate.HZ_80) # constantly output weights using the median of all samples # obtained within 1 second while True: print(hx.weight(timedelta(seconds=1))) #eg. 0.03 g
pip3 install hx711-rpi-py
There is a Python script in the
src directory you can use to calibrate your load cell and obtain the reference unit and offset values referred to above. The simplest way to use it after installing
hx711-rpi-py is as follows:
[email protected]:~ $ wget https://github.com/endail/hx711-rpi-py/blob/master/src/calibrate.py [email protected]:~ $ python3 calibrate.py
When you no longer need it, simply
rm calibrate.py to remove the calibration script.