Package: hx711-rpi-py
Raspberry Pi HX711 Python BindingsPython bindings forRaspberry Pi HX711 C++ LibraryUse with Raspberry PiRead from a HX711 using PythonCode tested insidevirtual Raspberry Pi Zero/3/4 environmentson GitHub and builds automatically uploaded to PyPIThis repo automatically rebuilds when the C++ library is updatedSample OutputThe .gif above illustrates the output of asimple Python scripton a Raspberry Pi Zero W where the HX711 chip was operating at 80Hz. In this example, each time the.weightfunction is called the median of three samples was used to calculate the weight in grams.ExamplesfromHX711import*# 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 offsetwithSimpleHX711(2,3,-370,-367471)ashx:# set the scale to output weights in ounceshx.setUnit(Mass.Unit.OZ)# zero the scalehx.zero()# constantly output weights using the median of 35 sampleswhileTrue:print(hx.weight(35))#eg. 1.08 ozAlternative Syntax (w/outwith)fromHX711import*hx=SimpleHX711(2,3,-370,-367471)hx.setUnit(Mass.Unit.OZ)hx.zero()whileTrue:print(hx.weight(35))Keep in mind that calling.weight()will return aMassobject, but you can do the following:# set the scale to output weights in ounceshx.setUnit(Mass.Unit.OZ)# obtain a median reading from 35 samples as a Mass object in ouncesm=hx.weight(35)# number in ouncesnum=float(m)# eg. 1.08# string representation of the Masss=str(m)# eg. 1.08 oz# print the Mass objectprint(m)# eg. 1.08 oz# change the unit to gramsm.setUnit(Mass.Unit.G)grams_as_str=str(m)# eg. 30.62 g# or obtain a new Mass objectm2=m.convertTo(Mass.Unit.KG)kgs_as_str=str(m2)# eg. 0.031 kgThe list of differentMass.Units can be viewedhere.Time-Based SamplingYou can usedatetime.timedeltato obtain as many samples as possible within the time period.fromHX711import*fromdatetimeimporttimedeltawithSimpleHX711(2,3,-370,-367471)ashx:whileTrue:# eg. obtain as many samples as possible within 1 secondprint(hx.weight(timedelta(seconds=1)))Options.weight(),.zero(), and.read()can all take anOptionsparameter. 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 secondhx.zero(Options(timedelta(seconds=1),ReadType.Average))# obtain a raw value from the scale using the median of 100 samplesnum=hx.read(Options(100,ReadType.Median))# obtain a Mass object using the median of three samples# all four statements below are equivalentm=hx.weight()m=hx.weight(3)m=hx.weight(Options())m=hx.weight(Options(3,ReadType.Median))# Options can also be created separatelyopts=Options()opts.timeout=timedelta(seconds=5)opts.stratType=StrategyType.Timem=hx.weight(opts)InstallInstalllibhx711pip3 install --upgrade hx711-rpi-pyCalibrateThere is a Python script in thesrcdirectory 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 installinghx711-rpi-pyis as follows:pi@raspberrypi:~ $wgethttps://raw.githubusercontent.com/endail/hx711-rpi-py/master/src/calibrate.pypi@raspberrypi:~ $python3calibrate.py[datapin][clockpin]Substitute[data pin]and[clock pin]with theGPIO pin numberswhich are connected to the HX711's data pin and clock pin, respectively.DocumentationAs the Python code relies upon theunderlying C++ library, the documentation is identical. However, not all of the code is exposed to Python. You can check precisely which functionality is accessible through Python in thebindings.cpp file.