Weigh scale with buzzer

I am trying to create a scale for a box that is being filled by a conveyor, using a HX711 and a 20kg load cell. Ideally the scale would have a liquid crystal display showing current weight of box, as well as a piezo buzzer that would buzz constantly once a certain weight is achieved, until the box is replaced with a new empty box. I have watched several videos on the subject (trying to make a scale with a HX711) and tried multiple variations of code, none seem to give me desired bench test results. I am getting communication with the arduino Uno, and it seems to be receiving data from the HX711, but regardless of the code used, the serial monitor shows no change to the data, regardless of how much weight added to the load cell. I have the ‘fixed’ side screwed to a large steel block (approximately 10kg) with several washers below load cell, and a small piece of polycarbonate sheet, also spaced with washers, attached on top of the ‘movable’ side of the load cell. I’ve gotten this code to work the best, as it at least shows ‘add calibrated weight’ on the serial monitor, until something is placed on the weigh platform, but then, no matter how long I wait, I do not get the ‘calibration complete’ message, and I can add as much or as little weight and get no change in the data. Attached is my code, which I got from makersportal.com

hx711test.ino (1.38 KB)

If you never get “Calibration complete,” then is is likely that hx711.read() is always less than x0+10000, so there is no exit from the while(true) loop. You should figure out why that is so. One way to get started on that is to use the simplest possible HX711 code to just print out the raw values only, first with nothing on the scale, and then with full load (or your calibration load) on the scale. With those in hand, go back to the code you posted and see if you can understand what it is doing and why it gets stuck. It may be helpful to add some Serial.prints to that code, too.

Excerpt of YOUR code (I just added a comment):

Serial.println("Add Calibrated Mass");
  // calibration procedure (mass should be added equal to y1)
  int ii = 1;
  while(true){
    if (hx711.read()<x0+10000){ //<---stuck here?
    } else {
      ii++;
      delay(1000);
      for (int jj=0;jj<int(avg_size);jj++){
        x1+=hx711.read();
      }
      x1/=long(avg_size);
      break;
    }
  }
  Serial.println("Calibration Complete");

If I was doing this, I’d also use my DMM to measure the load cell output voltage under no load/with load conditions, and see if I can calculate what the raw output of the HX711 should be.

BTW, this is not related to your problem, but it’s odd to see a float used to hold the number of samples to take for an average. That suggests (to me, at least) that the code may be…unreliable in other ways…

float avg_size = 10.0; // amount of averages for each mass measurement

Ok so I tried your suggestion and get an error trying to upload, here is my version with your code included, I’ve tried including every HX711 library variation I can find, and each time the compile fails at the line “if (hx711.read()<x0+10000){ //<—stuck here?” saying hx711 was not declared in this scope

#include <HX711.h>


void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println("Add Calibrated Mass");
  // calibration procedure (mass should be added equal to y1)
  int ii = 1;
  while(true){
    if (hx711.read()<x0+10000){ //<---stuck here?
    } else {
      ii++;
      delay(1000);
      for (int jj=0;jj<int(avg_size);jj++){
        x1+=hx711.read();
      }
      x1/=long(avg_size);
      break;
    }
  }
  Serial.println("Calibration Complete");
}

I also tried reading the load cell with my DMM and I get so much variation in voltage that I don’t have a clue what I’m looking for. (Using a Fluke 87 III)

I've also tried this code, sourced from brainy-bits.com, and it fails to compile at the line "HX711 scale(DOUT, CLK); // Init of library" saying no matching function for call to HX711 scale(int,int)

/* Calibration sketch for HX711 */
 
#include "HX711.h"  // Library needed to communicate with HX711 https://github.com/bogde/HX711
 
#define DOUT  6  // Arduino pin 6 connect to HX711 DOUT
#define CLK  5  //  Arduino pin 5 connect to HX711 CLK
 
HX711 scale(DOUT, CLK);  // Init of library

void setup() {
  Serial.begin(9600);
  scale.set_scale();  // Start scale
  scale.tare();       // Reset scale to zero
}

void loop() {
  float current_weight=scale.get_units(20);  // get average of 20 scale readings
  float scale_factor=(current_weight/0.145);  // divide the result by a known weight
  Serial.println(scale_factor);  // Print the scale factor to use
}

Any ideas what I can do to remedy this issue?

daveyg1987:
...fails to compile...any ideas what I can do to remedy this issue?

Maybe start with code that you know has compiled...such as, apparently, the code in your first post?

daveyg1987:
Ok so I tried your suggestion and get an error trying to upload, here is my version with your code included...

That's not my code. That's an excerpt of your code, with a comment that I added where I thought a problem could be located.

I suggest editing the code in your first post: delete the tare and calibration parts in setup(), delete the mass and averaging part in loop(), and change loop() to just print out a raw reading every few seconds. That should get you to my first suggestion:

If you never get "Calibration complete," then is is likely that hx711.read() is always less than x0+10000, so there is no exit from the while(true) loop. You should figure out why that is so. **One way to get started on that is to use the simplest possible HX711 code to just print out the raw values only, first with nothing on the scale, and then with full load (or your calibration load...which should be "20" somethings, same as y1, right?) on the scale.**With those in hand, go back to the code you posted and see if you can understand what it is doing and why it gets stuck.

See if you can make sense of those values (raw reads with no load and then with calibration load). If they are not reasonably stable, then there could be other problems (which is where your DMM may come into play, or a wiring diagram, or photos of your setup).

If they are reasonable and stable, then go back to the code in your OP, and go through each line in setup() to see if you can figure out why hx711.read() is apparently less than x0+10000 and whether that condition statement's result (going into an infinite loop if the read is less than x0+10000) makes sense.

And, regarding my second suggestion...

It may be helpful to add some Serial.prints to that code, too.

...I suggest adding a print of x0 after the tare procedure, before the calibration procedure. If I were you, I'd also be interested in x1, to help you understand whether the calibration procedure makes sense.

Ok I think I understand what you’re saying now. Here is my new current code, and it fails compilation at the marked location, says x0 does not name a type, I must have deleted a line I should have kept that defines x0

#include <Q2HX711.h>

const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 4;

float y1 = 20.0; // calibrated mass to be added
long x1 = 0L;
long x0 = 0L;
float avg_size = 10.0; // amount of averages for each mass measurement

Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() {
  Serial.begin(9600); // prepare serial port
  delay(1000); // allow load cell and hx711 to settle
     x0+=hx711.read();
  }
  x0/=long(avg_size); <==============fails compilation here
    
  Serial.println("Calibration Complete");
}

void loop() {
  // averaging reading
  long reading = 0;
  for (int jj=0;jj<int(avg_size);jj++){
    reading+=hx711.read();
  }
  reading/=long(avg_size);
  // calculating mass based on calibration and linear fit
  Serial.print("Raw: ");
  Serial.print(reading);
  Serial.print(", ");
  Serial.println(mass);
}

Ok so I fixed the code and it compiles and uploads fine, seems to be fine on the arduino end of things, but I wonder if I have a faulty HX711 or load cell now.
My readings (serial print) get some variation, but even with the weight they seem to revert back to the original reading. Should the reading not stay the same once the weight is placed on top? Marked is where I added a 20lb bar of steel to the scale. It slowly begins to hit the higher number more frequently… but even if I left the weight on the scale it still will periodically display the lower number. Perhaps I will try changing the delay to 5000 instead of 2000, see if it is just a ‘bouncing’ issue.
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8389426, inf <==================
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf
Raw: 8389426, inf
Raw: 8390245, inf

#include <Q2HX711.h>

const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 4;

float y1 = 20.0; // calibrated mass to be added
long x1 = 0L;
long x0 = 0L;
float avg_size = 10.0; // amount of averages for each mass measurement

Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() {
  Serial.begin(9600); // prepare serial port
  delay(1000); // allow load cell and hx711 to settle
 
  
  Serial.println("Calibration Complete");
}

void loop() {
  // averaging reading
  long reading = 0;
  for (int jj=0;jj<int(avg_size);jj++){
    reading+=hx711.read();
  }
  reading/=long(avg_size);
  // calculating mass based on calibration and linear fit
  float ratio_1 = (float) (reading-x0);
  float ratio_2 = (float) (x1-x0);
  float ratio = ratio_1/ratio_2;
  float mass = y1*ratio;
  Serial.print("Raw: ");
  Serial.print(reading);
  Serial.print(", ");
  Serial.println(mass);
  delay(2000);
}

It is odd that you see so little change in the readings when you go from nothing to nearly half of the capacity of the load cell. Can you test with a variety of known weights?

That is what has me thinking I have a faulty cell. I have gathered some pieces of material (I work in a machine shop) that weigh 4lbs, 6lbs, and 14lbs each.
Well after trying with those 3 piece I am more convinced that I have a hardware issue, either a faulty load cell, or the wiring is somehow screwed up. I get more variation in the raw readings from having nothing on it than I get from having any combination of the weights on it. Here is a picture of the setup. I currently have the red and black going to 5v+ and gnd respectively, and the white goes to A- of the HX711, green goes to A+. Curious if switching to B- and B+ will change the results in any way…

I'm thinking at this point of getting a second load cell and HX711, just so I can compare the results if I duplicate everything the same way (or maybe I will connect the second one up to a separate arduino and wire it differently so I can switch between the two quickly...)

Here are the readings I am getting now, with no weights placed on the scale. Each reading is 20s apart now. Doesn't seem to be leveling out at all. If I add a weight to it (doesn't seem to matter which one) The readings continue to fluctuate, with some of the previous to adding weight readings recurring.
Calibration Complete
Raw: 8388041, inf
Raw: 8396286, inf
Raw: 8390985, inf
Raw: 8390964, inf
Raw: 8390966, inf
Raw: 8390992, inf
Raw: 8390981, inf
Raw: 8390985, inf
Raw: 8391000, inf
Raw: 8390977, inf
Raw: 8390998, inf
Raw: 8391005, inf

I think you're on the right track with the idea to get more hardware.

daveyg1987:
Here are the readings I am getting now, with no weights placed on the scale. Each reading is 20s apart now. Doesn't seem to be leveling out at all.

:confused: If you throw out the high and low in those numbers, you've got a variation of about one part in 500,000 relative to the average, which is great.

Regarding the no change in output when weight is added: a few clear close-up photos of your setup may reveal something.

(Oops - just noticed your photo. Please take a side view, too.)

Also, if I were you, I'd be curious about resistance between each leg of the load cell (when not under load and not attached to the HX711) and the voltages at E+, E-, A+, and A- relative to ground, under no load and under full load (after reattaching the cell to the HX711). Also, I'd be curious about the load cell's sensitivity (usually expressed in mV/V) to see if its output under full load matches the spec.

(Oops - just noticed your post about connecting red to +5...it should go to E+, not +5v, and black should go to E-, not ground. Also, this may be one of those boards where you need to connect E- to ground... see this topic. You will know that is the case if E- has a voltage greater than zero.)

And yes, of course changing to B+ and B- will change things. Read the datasheet.

So we can see it:
scale.png
That is some serious base! :grinning:

Good picture; yes, I too would like to see a side view!

Here is a side view. E- and E+ on the HX711 is what I meant by gnd and +5v. Sorry for the confusion. I will check with the DMM and see what voltage I’m getting on E- and +, is that where I should check to see if the gnd is good on the HX711? I’ll read that other linked post next, if its stated in there, disregard. I ordered another HX711 and another load cell on Friday, maybe between the two I can do one correctly. As for the ‘beefy’ base, I wanted to make sure my test bed was stable haha. A 5kg chunk of stainless steel may be a bit overkill in the actual application where it is going to end up… but for now, it is what it is.
How do you link the image directly? I used to use photobucket but now they are all screwy, other image hosting sites seem to be very hit and miss. For now I’ll just attach.

Ok so I am getting 4.24vdc from E+ to usb socket... So by that measure I should have a good gnd at the HX711. The load cell I am using is likely just some Chinese one made by some dude in his basement, the new one I ordered on Friday is a name brand name version (Cyclewet 50kg each, 2pcs) probably still made in China but at least made in an actual factory haha. Until it arrives I may try swapping A+/- to B+/- and see what results I get then. The issue with the no name cell is trying to find the datasheet has been a real goose chase...

scale2.jpg
OK, may not be linear response but looks plausible. Must be a wiring problem! :cold_sweat:

Well since I had the DMM out of the cabinet I decided to check and from the E+ to the gnd pin on the HX711 I'm getting 4.21v (also 4.21v from E+ to usb socket on arduino, but from E+ to usb socket on usb extension I'm getting 4.24v, so I'm thinking my short 1ft usb to arduino cable is dropping voltage by .03v) but I'm getting the exact same voltage from gnd on HX711 to both A+ and A-. Maybe that is normal, I don't know, this is my first time messing with a load cell, maybe the weight to voltage change is less than .00? I'll throw my biggest weight on top and check voltage again. Ok, so unloaded I get 4.21v at both A+ and A- and with 24lbs on it I am still getting 4.21v and 4.21v so maybe the cell outputs less variation than my DMM is set to read (hundredths) I'll try doing the same with the CLK and DT pins and see what readings I get with the DMM. On HX711, connected to arduino and load cell, unloaded, I'm getting vcc-gnd of 4.92v, dt-gnd reads .005v, and clk-gnd gets .001v
With load of 24lbs (appx 11kg) I am getting the exact same on the DMM, .005v at DT pin, .001v at CLK pin.
If I unhook the CLK and DT wire from the arduino, the readings change to .002/3v at DT and .255/.260v fluctuating on CLK WITH weight, and .002/3v at DT and .242/.248v at GND
That mean anything to anyone? If I could find the datasheet for the load cell those numbers might mean something haha. BUT there IS a variation in the range of movement with weight and without weight, coming only from the CLK pin, when not connected to arduino.

Paul, what image hosting site do you like to use? I used to use photobucket (have like 400 pics on there still) but since they went $$ I have had issues.

Don't use a hosting site. Reply, and use the Attachements and other options feature to attach your .jpg file, which you may have to resize to be a bit smaller if using a camera that takes 8 Megapixel pictures or other equally large photos.

Once attached & posted, copy the Link that results and use More:Modify to copy the ink back into your post with img & /img tags, or use the Insert an Image box in the menu: