How do I use HX711 and a load cell?

I am currently using HX711 and a load cell. However, the calibration thing in the load cell confuses me, even if I already watched several tutorials, I still don't get it. I have questions in mind such as:

  1. What should I do with the standard (from what I think it is) "-7050" value in the calibration? Do I add or do I subtract?
  2. How do I convert the values into grams?
  3. How can I use an "if/else" code to it? I'm thinking of something like connecting it to a DHT11 temperature sensor and it should be something like "If the weight is 0 grams, don't read the temperature. If the weight is more than 0 grams, read the temperature"

I have the typical schematic diagram from the internet, and no, I haven't tried to connect the whole weight sensor yet, only the load cell once before soldering it to the HX711.
image

I am trying to use this library: GitHub - bogde/HX711: An Arduino library to interface the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter (ADC) for Weight Scales.

For the code, I used SparkFun's Code (note: this is only for the calibration, I haven't coded the connection to the DHT11 yet):

#include "HX711.h"

#define LOADCELL_DOUT_PIN  3
#define LOADCELL_SCK_PIN  2

HX711 scale;

float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");
  
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  
  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" lbs"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
  }
}
 

Along with the library, I found the weight code:

/*!
 * @file readWeight.ino
 * @brief Get the weight of the object
 * @details After the program download is complete,
 * @n The serial port will print the current weight
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @License     The MIT License (MIT)
 * @author      [Wuxiao](xiao.wu@dfrobot.com)
 * @version  V1.0
 * @date  2020-12-26
 * @https://github.com/DFRobot/DFRobot_HX711
 */
 
#include <DFRobot_HX711.h>

/*!
 * @fn DFRobot_HX711
 * @brief Constructor 
 * @param pin_din  Analog data pins
 * @param pin_slk  Analog data pins
 */
DFRobot_HX711 MyScale(A2, A3);

void setup() {
  Serial.begin(9600);
}

void loop() {
  // Get the weight of the object
  Serial.print(MyScale.readWeight(), 1);
  Serial.println(" g");
  delay(200);
}

To understand "what are Calibration Factors", you can follow these steps which do not use any Library:

1. Attach your Load Cell on a bench using screws.

2. Check that the Data-pin and CLK-pin of the HX711 Module are connected with A1-pin and A2-pin of Arduino UNO. Don't forgrt to connect 5V and GND of UNO with the HX711 Module.
hx711LoadCellX
Figure-1:

3. Place 1 kg Weight on the Cantilever End of the Load Cell.

4. Upload the following sketch into UNO and record:

A = (W1, C1) 
A = (1 kg, C1)
where:
C1 is the count shown on Serial Monitor.

Sketch:

/* This program takes 10 samples from LC + HX711B at
   1-sec interval and then computes the average.
*/

unsigned long x = 0, y = 0;
unsigned long dataArray[10];
int j = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(A1, INPUT); //data line  //Yellow cable in my Setup
  pinMode(A0, OUTPUT);  //SCK line  //Orange cable in my Set up
}

void loop()
{

  for (int j = 0; j < 10; j++)
  {
    digitalWrite(A0, LOW);//SCK is made LL
    while (digitalRead(A1) != LOW) //wait until Data Line goes LOW
      ;
    {
      for (int i = 0; i < 24; i++)  //read 24-bit data from HX711
      {
        clk();      //generate CLK pulse to get MSB-it at A1-pin
        bitWrite(x, 0, digitalRead(A1));
        x = x << 1;
      }
      clk();  //25th pulse
      Serial.println(x, HEX);
      y = x;
      x = 0;
      delay(1000);
    }
    dataArray[j] = y;
  }

  Serial.println("===averaging process=========");
  unsigned long C1 = 0;   //C1 = Count-1 against no fuel in the Tank

  for (j = 0; j < 10; j++)
  {
    C1 += dataArray[j];
  }
  Serial.print("Average Count = ");
  C1 = C1 / 10;
  Serial.println(C1, DEC);
}

void clk()
{
  digitalWrite(A0, HIGH);
  delayMicroseconds(50);
  digitalWrite(A0, LOW);
}

5. Place 2 kg Weight on the Load Cell and record:

B = (W2, C2)
B = (2 kg, C2)
where: 
C2 is the count shown on the Serial Monitor

6. Now, solve the following equation to find relation between unknown weight W and the corresponding count, C.

P = (W, C)
where:
W = unknown weigt to be measure
C = count corresponding to W

Equation:

(W2 - W1)/(C2 - C1) = (W2 - W)/(C2 - C)
==> W = mC + k
m = Gain of the Input Device (Load Cell + HX711 Module + Wiring)
k = Offset of the Input Device
<m, k> are called the Calibration Factors.

7. The following diagram (Fig-2) illustrates how the Calibrartion Factors when applied transform a real system (with finite m and k) into an ideal system (with: m = 1 and k = 0).


Figure-2:

8. Place 1.5 kg weight on the Load Cell.

9. Uplaod the following sketch and check that the Serial Monitor shows a reading very close to 1.500 with +/- 10 gm error.

(You must replace the following code by the one that has been derived for your system in Step-6.)

 float W = (float)0.90*(sum-901002)/946560 + 0.75;
/* This program takes 10 samples from LC + HX711B at
   1-sec interval and then computes the average.
*/

unsigned long x = 0, y=0;
unsigned long dataArray[10];
int j = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(A1, INPUT); //data line  //Yellow cable
  pinMode(A0, OUTPUT);  //SCK line  //Orange cable
}

void loop()
{

  for (int j = 0; j < 10; j++)
  {
    digitalWrite(A0, LOW);//SCK is made LL
    while (digitalRead(A1) != LOW) //wait until Data Line goes LOW
      ;
    {
      for (int i = 0; i < 24; i++)  //read 24-bit data from HX711
      {
        clk();      //generate CLK pulse to get MSB-it at A1-pin
        bitWrite(x, 0, digitalRead(A1));
        x = x << 1;
      }
      clk();  //25th pulse
      Serial.println(x, HEX);
      y = x;
      x = 0;
      delay(1000);
    }
    dataArray[j] = y;
  }

  Serial.println("===averaging process=========");
  unsigned long sum = 0;

  for (j = 0; j < 10; j++)
  {
    sum += dataArray[j];
  }
  Serial.print("Average Count = ");
  sum = sum / 10;
  Serial.println(sum, HEX);
  float W = (float)0.90*(sum-901002)/946560 + 0.75;//0.005331 * sum - 1146.176;
  //W = (float)W / 1000.00; 
  Serial.println(W, 3);
}

void clk()
{
  digitalWrite(A0, HIGH);
  digitalWrite(A0, LOW);
}

2 Likes

I have tried steps 3 and 4, however, the data shown are only "0" when I put weight on it. What seems to be the problem here? Here's the photo of my setup.


And here's the count shown on the serial monitor.
image

An absolutely horrible soldering job, for starters (including a possible short).

image

They should be reworked (if that is even possible) to look like the ones marked "OK":


Courtesy of Adafruit.

That, and some (maybe most or all) of the green boards are have a (fixable) defect: E- is not connected to ground. It should/must be. Lots of posts here about that.

3 Likes

It was my first time soldering and I thought making the wires stick to it was enough. Does this mean I need to cover the holes in soldering?

I added some images of what they should look like in my post. Also, check out the Adafruit link below the image for some good guidance. And hopefully you are using rosin-core solder.

2 Likes

Maybe my issue was with my soldering iron. I found it hard to solder using the tip since the solder doesn't melt, which is why I used this part:
image

Thanks for the tips!

Was the -7050 a number that you found by following the directions in the first sketch with your known weight on the scale? If so it isn't an add or subtract, it is a factor that the code divides the measurement by to transform the raw counts into the units of your known weight. The adding and subtracting part of the calibration is done by the scale.tare() line in the sketch.

Without knowing the value of your known weight and its units, along with the weight rating and sensitivity of the load cell, it is hard to say exactly what the -7050 is. But by following those directions you should be able to put units on the -7050. Perhaps the scale factor is something like -7050 HX711_ADC_counts per ounce?

Edit: Ah, I missed the pics. The zeros and the solder joint does indicate that something might be shorted out.

Yes, I just found the -7050. What I meant with the "add or subtract" is due to this:

if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;

And when I tried to type "a", the -7050 turned into -7040. Which is why I thought it was an add or subtract.

In using my first sketch, do I add a value to the scale.tare();? Like, scale.tare(100);? (I'm a literal beginner in Arduino)

Ah -- these add and subtracts are like a knob that adjusts the initial -7050 scale factor to something that produces the right numerical value of weight that matches your known value.

How much weight are you putting on the scale and what does it read when it is doing this printing?

1 Like

Make your correct soldering first as described in post #5.

I have fifteen years of professional soldeing experince (having had higher training on soldering from USA) with Schlumberger Wire Line Services for their downhole electronics that were subjected to vibration and high temperature.

1. For your home soldering works, use 60W soldering Iron (preferable AC isolated and magnetically coupled) with Fan/AC stopped in your room.

2. Insert bare end of the wire into the hole of the PCB.

3. Attcah the tip of the soldering iron against the junction point of the wire and hole at 450 (Fig-1).
solderPrac
Figure-1:

4. Wait for 15 to 20 seconds.

5. Place the 60/30 solder lead near the tip (NOT on the tip) of the soldering iron (Fig-1).

6. Check that the solder is melting and making a nice bridge around the wire (OK picture of post #5).

7. And you are done.

8. Now upload the sketch of post #2 and check the the counts are non-zero (shold be at leat greater than 6/7 digits with 1 kg weight). If not. the next step is to be checking the functionality of the HX711 Module by the following setup (Fig-2):


Figure-2:

2 Likes

Then I would say that your tip is, possibly, damaged (burned, oxidized).
Once it's good and hot, use a damp sponge or cloth to remove as much debris and stuff as you can. If you have some steel wool handy you may be able to stroke it clean/er with that, but don't go overboard with that.

I'm sure you can find some tutorials and other demos on the "care and feeding" of soldering irons etc.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.