- Use with Raspberry Pi
- Requires lgpio
#include <iostream>
#include <thread>
#include <chrono>
#include <hx711/HX711.h>
int main() {
using namespace std;
using namespace HX711;
const int dataPin = 2;
const int clockPin = 3;
const int referenceUnit = -377;
const int offset = -363712;
SimpleHX711 hx(dataPin, clockPin, referenceUnit, offset);
//make the hx output weights in kilograms
hx.setUnit(Mass::Unit::KG);
while(true) {
cout << hx.weight() << endl; //prints eg. "36.08 kg"
this_thread::sleep_for(chrono::seconds(1));
}
return 0;
}
The .gif above illustrates the output of the test code where I applied pressure to the load cell. The HX711 module was operating at 80Hz. However, note from the code that the value being used is the median of five samples from the sensor.
pi@raspberrypi~ $ git clone https://github.com/endail/hx711
pi@raspberrypi~ $ cd hx711
pi@raspberrypi~/hx711 $ make && sudo make install
make
will create the executable bin/hx711calibration
in the project directory. You can use this to calibrate your load cell and HX711 module. Run it as follows and follow the prompts:
-
data pin: Raspberry Pi pin which connects to the HX711 module's data interface. Use GPIO pin numbering.
-
clock pin: Raspberry Pi pin which connects to the HX711 module's clock interface. Use GPIO pin numbering.
Example using GPIO pin 2 for data and pin 3 for clock.
pi@raspberrypi~/hx711 $ bin/hx711calibration 2 3
make
will create the executable bin/simplehx711test
in the project directory. You can use this to test your load cell and HX711 module. Arguments are as follows:
-
data pin: Raspberry Pi pin which connects to the HX711 module's data interface. Use GPIO pin numbering.
-
clock pin: Raspberry Pi pin which connects to the HX711 modules' clock interface. Use GPIO pin numbering.
-
reference unit: load cell's reference unit. Find this value with the calibration program above, otherwise set it to 1.
-
offset: load cell's offset from zero. Find this value with the calibration program above, otherwise set it to 0.
Example using GPIO pin 2 for data, pin 3 for clock, -377 as the reference unit, and -363712 as the offset:
pi@raspberrypi~/hx711 $ bin/simplehx711test 2 3 -377 -363712
After writing your own code (eg. main.cpp), compile with the HX711 library as follows:
g++ -Wall -o prog main.cpp -lhx711 -llgpio