Jump To
How to program a Four Load Cell with HX711
I’m not going to write up a whole detailed article on how to do this as I have a YouTube video you can watch for the details.
Overview
I have a project set up to monitor coffee levels in a carafe in my office. To accomplish this task, I used four load cells which I placed on mounts between two ceramic tiles. The mounts are important because of the way the sensors work. The sensors work by mounting from the outer square and allowing the center “tongue” to be able to move up and down freely, if you mount these sensors directly on a flat surface, the “tongue” will not have room to move as it will be flush against the surface.
Unfortunately I failed in getting a picture of the setup but you can check out the video for that, I also go over why it has to be set up that way so it might make it less confusing to understand the setup as it’s a little odd.
The code can be found at the end of this article.
Watch the Video
The Code
Don’t forget to download the library file found here and adjust the pin values for LOADCELL_DOUT_PIN and LOADCELL_SCK_PIN.
#include "Arduino.h" #include <HX711.h> void calibrate(); // HX711 circuit wiring const int LOADCELL_DOUT_PIN = 3; const int LOADCELL_SCK_PIN = 2; HX711 scale; void setup() { Serial.begin(9600); Serial.println("HX711 Demo"); Serial.println("Initializing the scale"); // Initialize library with data output pin, clock input pin and gain factor. // Channel selection is made by passing the appropriate gain: // - With a gain factor of 64 or 128, channel A is selected // - With a gain factor of 32, channel B is selected // By omitting the gain factor parameter, the library // default "128" (Channel A) is used here. scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); scale.set_gain(128); // Serial.println("Before setting up the scale:"); // Serial.print("read: \t\t"); // Serial.println(scale.read()); // print a raw reading from the ADC // // Serial.print("read average: \t\t"); // Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC // // Serial.print("get value: \t\t"); // Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet) // // Serial.print("get units: \t\t"); // Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided // // by the SCALE parameter (not set yet) // // scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details // scale.tare(); // reset the scale to 0 // // Serial.println("After setting up the scale:"); calibrate(); Serial.print("read: \t\t"); Serial.println(scale.read()); // print a raw reading from the ADC Serial.print("read average: \t\t"); Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC Serial.print("get value: \t\t"); Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare() Serial.print("get units: \t\t"); Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided // by the SCALE parameter set with set_scale Serial.println("Readings:"); } void loop() { Serial.print("one reading:\t"); Serial.print(scale.get_units(), 1); Serial.print("\t| average:\t"); Serial.println(scale.get_units(10), 1); // scale.power_down(); // put the ADC in sleep mode delay(2000); // scale.power_up(); } void calibrate() { // Remove any calibration values and clear the scale scale.set_scale(); scale.tare(); // Prompt the user Serial.println("Add your known weight to the scale, enter the weight and press <Enter>"); int userInput = -123; String inputString = ""; // Loop until we receive an input (which will change the value of userInput while (userInput == -123) { // Read serial input: while (Serial.available() > 0) { int inputChar = Serial.read(); if (isDigit(inputChar)) { // convert the incoming byte to a char and add it to the string: inputString += (char)inputChar; } // if you get a newline, print the string, then the string's value: if (inputChar == '\n') { userInput = inputString.toInt(); } } } // Now get the reading from the scale float calReading = scale.get_units(10); Serial.print("Setting the cal to "); Serial.println(calReading / userInput); scale.set_scale(calReading / userInput); }
my Arduino code shows this compile error “no matching function for call to ‘HX711::HX711()'”. Please help me
Is the email you provided the correct email address? I need to look at your code to know exactly what is going on. If that’s your correct email address I will email you so you can send me your code and I can take a look.
Please explain follow code line;
HX711 scale;
In this line, it’s much like creating a variable but instead we’re creating what is called an “object”. Objects can have specific values and functions that all relate to that object. In this case we created an HX711 object and called it scale because the functions for this object will be representing readings from the weight scale. I hope that helps.
Hi,
nice work! Does it matter if two of the four red wires have a different length? I use four load cells and try to build a 4×4 scale. The problem is that the red wires are not long enough for two of the load cells and I would like to extend them.
Florian, in theory the longer wires will affect the output as there is added resistance with longer wires. However, I wouldn’t worry about that since the added resistance will be so tiny that it likely wouldn’t even be noticed given the error tolerance anyway.
TL;DR Nope, shouldn’t be noticeably different.
Sorry for the delay, my notifications stopped working again.
Thank you for the reply. I have already set it all up in the meanwhile. But its not working yet. I used the basic hx711 Arduino example, but the hx711 just cannot be found by my esp8266. The wiring is correct. Seems like the hx711 is broken. Do you maybe have any ideas?
I tried this:
#include “HX711.h”
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
HX711 scale;
void setup() {
Serial.begin(57600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}
void loop() {
if (scale.is_ready()) {
long reading = scale.read();
Serial.print(“HX711 reading: “);
Serial.println(reading);
} else {
Serial.println(“HX711 not found.”);
}
delay(1000);
}
im doing a project whereby, im using a single load cell to measure weights of sauce condiments. im using multiple load cells but they are individually connected, unlike the way you connected all 4 of your load cells together. iv tried many programs & all of them cant seem to give me a proper calibration factor. Idk what im doing wrong because the circuit connections seem fine. Its just that the values i get using the programme are very fluctuating. sometimes it even goes to negative. Any ideas on why im facing this issue? will be glad to email you my circuit diagram & code that iv tested if you can help.
There definitely is some fluctuation in the values and they will go negative on occasion but it should average around the same “center” value, are you just getting completely random values as in, you can’t even tell when you put weight on it? One thing I ran into (I think I mentioned this in the video but can’t remember) is that you need to make sure the center piece (the piece with the little bump on it) has room to move downward. The scale should be supported by these “bumps” and when weight is applied, that center piece will flex downward causing the reading. So, if you aren’t getting much of any reading, even with heavier objects then I would suggest making sure those have room to move.
hiii i need your help pls im doing this project but i have an error using your code in line 25 scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN, 128);
What is the error you’re receiving?
no matching function for call to ‘HX711::begin(const int&, const int&, int)’
i do get the same error as hit and it says this
Sorry for the late reply, I’m super swamped with other stuff. I didn’t dig into their library too much so I couldn’t find out exactly why it was failing, the “begin” function is still there with three parameters and it hasn’t changed in years.
In any case, I was able to work around it. The last parameter is for the gain setting, which is defaulted to 128 anyway so it’s not needed. I updated the code in the post to also add a second line `set_gain` so if you do want to change the gain you can do it with that command.
Here are the lines that were changed (lines 25 – 26)
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_gain(128);
Hi sir. I receive this error:
no matching function for call to ‘HX711’: : begin(const int&, const int&, int)
Hi Josephe, I replied to another comment above but I’ll paste the same message here. Thanks for checking out my post.
Sorry for the late reply, I’m super swamped with other stuff. I didn’t dig into their library too much so I couldn’t find out exactly why it was failing, the “begin” function is still there with three parameters and it hasn’t changed in years.
In any case, I was able to work around it. The last parameter is for the gain setting, which is defaulted to 128 anyway so it’s not needed. I updated the code in the post to also add a second line `set_gain` so if you do want to change the gain you can do it with that command.
Here are the lines that were changed (lines 25 – 26)
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_gain(128);
I hope you are doing fine, I’m currently using Sparkfun combinator, but there is a lot of fluctuation. The half-bridge load cells I used are 10 kgs. And for my project there is a need for a zero-drift record storing so I can differentiate the zero drift values of different load cells.
but for now, the output value isn’t stable.
it started from 9208.35 g (original weight of 9200g) and after 1.15 hours the value reached 9253.3 g at constant ambient temperature.