Python bindings for Raspberry Pi HX711 C++ Library
Project description
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
- This repo automatically rebuilds when the C++ library is updated
Sample Output
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.
Examples
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
with SimpleHX711(2, 3, -370, -367471) as hx:
# set the scale to output weights in ounces
hx.setUnit(Mass.Unit.OZ)
# zero the scale
hx.zero()
# constantly output weights using the median of 35 samples
while True:
print(hx.weight(35)) #eg. 1.08 oz
Alternative Syntax (w/out with
)
from HX711 import *
from datetime import timedelta
hx = SimpleHX711(2, 3, -370, -367471)
hx.setUnit(Mass.Unit.OZ)
hx.zero()
while True:
print(hx.weight(35))
Keep in mind that calling .weight()
will return a Mass
object, but you can do the following:
# set the scale to output weights in ounces
hx.setUnit(Mass.Unit.OZ)
# obtain a median reading from 35 samples as a Mass object in ounces
m = hx.weight(35)
# number in ounces
num = float(m) # eg. 1.08
# string representation of the Mass
s = str(m) # eg. 1.08 oz
# print the Mass object
print(m) # eg. 1.08 oz
# change the unit to grams
m.setUnit(Mass.Unit.G)
grams_as_str = str(m) # eg. 30.62 g
# or obtain a new Mass object
m2 = m.convertTo(Mass.Unit.KG)
kgs_as_str = str(m2) # eg. 0.031 kg
The list of different Mass.Unit
s can be viewed here.
Time-Based Sampling
You can use datetime.timedelta
to obtain as many samples as possible within the time period.
from HX711 import *
from datetime import timedelta
with SimpleHX711(2, 3, -370, -367471) as hx:
while True:
# eg. obtain as many samples as possible within 1 second
print(hx.weight(timedelta(seconds=1)))
Options
.weight()
, .zero()
, and .read()
can all take an Options
parameter. You can use this to fine tune how you want the scale to behave.
# zero the scale by using the average value of all samples obtained within 1 second
hx.zero(Options(
stratType=StrategyType.Time,
readType=ReadType.Average,
timeout=timedelta(seconds=1)))
# obtain a raw value from the scale using the median of 100 samples
num = hx.read(Options(
stratType=StrategyType.Samples,
readType=ReadType.Median,
samples=100))
# obtain a Mass object using the median of three samples
# all four statements below are equivalent
m = hx.weight()
m = hx.weight(3)
m = hx.weight(Options())
m = hx.weight(Options(
stratType=StrategyType.Samples,
readType=ReadType.Median
samples=3))
Install
-
Install libhx711
-
pip3 install --upgrade hx711-rpi-py
Calibrate
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:
pi@raspberrypi:~ $ wget https://github.com/endail/hx711-rpi-py/blob/master/src/calibrate.py
pi@raspberrypi:~ $ python3 calibrate.py
Documentation
As the Python code relies upon the underlying C++ library, the documentation is identical. However, not all of the code is exposed to Python. An example is the Utility class, which is only meant for use within the C++ library. You can check precisely which functionality is accessible through Python in the bindings.cpp file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for hx711_rpi_py-1.35.0-cp37-cp37m-linux_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb663d82ee303ce49284b37ea4d81340c0e4b00267ee8ec039ee0b4386905fe4 |
|
MD5 | a67305dcb4c011de0557ac64d973e91a |
|
BLAKE2b-256 | 8ec0df4d5810ed54378402ff9754c3c61d83b901010eea14e4c73d8a3e3d459e |
Hashes for hx711_rpi_py-1.35.0-cp37-cp37m-linux_armv6l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca418850cfd5e5dfffab2b562b2a0d935789951f0c2e334525f122f491a6835e |
|
MD5 | 65d61b4b88f2af97bbf100b2b9bdb245 |
|
BLAKE2b-256 | 6a70fa45c15b1220051d47df738241e52bf29d1b26d119dd881cfa98906137d8 |