Weight based Servo Trigger

I have a 20KG Load cell with an HX711 and I am looking to trigger servo movement if the load cell detects a predetermined weight reading.

Is anyone able to please help with the coding for this or point me in the direction of some handy resources, as coding isn't my strongest point?

The way the logic should work is:

  • Servo starts at 0 degrees
  • If the reading is equal to 3.4 - turn the servo to 180 degrees
  • If the reading over 3.4 - servo remains at 0 degrees
  • In the reading is less than 3.4 - servo remains at 0 degrees

Someone in another forum also did mention it may be worth looking into ranges and hysteresis, but I'm honestly not sure how I would go about implementing those.

I've been able to, however, 'jerry-rig' the following code together

#include "HX711.h"
#include <Servo.h> // Include the Servo library

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define LOADCELL_DOUT_PIN  3
#define LOADCELL_SCK_PIN  2
int servoPin = 5; // Declare the Servo pin

HX711 scale;

Servo Servo1; // Create a servo object
float units;

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  Serial.println("Readings:");
 
  Servo1.attach(servoPin);  // We need to attach the servo to the used pin number
}

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();

 Serial.print("Reading: ");

 units = scale.get_units(10);
     if (units > 3.35 && units < 3.45)
     {
       Servo1.write(180); 
       delay(1000); 
       Serial.print("Success. Door is now OPEN!");
     }

       else
     {
       Servo1.write(0); 
       delay(1000); 
       Serial.print("Success. Door is now CLOSED!");
     }

}

However, I've run into the following 'kinks' with this:

  • When it hits the target weight, the servo keeps moving back and forth between 0 and 180 and doesn't hold the 180 positions as intended.
  • It also still does this when it exceeds the target weight. However,
    It is supposed to reset the servo to position 0 in this case.

Is anyone able to please help me look into this?

Nicely posted code!

I don't have the scale library here. You call get weight with the value 10 and with the "no value", nill.

  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
.
.
.

 units = scale.get_units(10);

Is it some averaging in that 10?

Placing a heavier weight might make the sensor like "count" upwards and deliver a value in the interval.
Why not trigger on only > 3.35? Running a second measurement checking for over weight....

Railroader:
Nicely posted code!

I don't have the scale library here. You call get weight with the value 10 and with the "no value", nill.

  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float

.
.
.

units = scale.get_units(10);





Is it some averaging in that 10?

Placing a heavier weight might make the sensor like "count" upwards and deliver a value in the interval.
Why not trigger on only > 3.35? Running a second measurement checking for over weight....

Thanks haha!
However, I've just realised my original [ost was missing a few details. I've now updated it, which should help proved better context to my issue

New games, new bets.....

The first thing I'd do is put a Serial.print(units) in immediately after you set units. That will tell you what values you're working with.

Do you actually get all the expect Door Opened, Door Closed, Reading etc prints in the Serial monitor? A listing of those would probably help.

Steve

slipstick:
The first thing I'd do is put a Serial.print(units) in immediately after you set units. That will tell you what values you're working with.

Do you actually get all the expect Door Opened, Door Closed, Reading etc prints in the Serial monitor? A listing of those would probably help.

Steve

Yes, they all print actually. Here's a link to a screenshot of the serial monitor and a video of the actual setup https://imgur.com/a/iuwRxEE

What doe all the hieroglyphs tell? Viking characters....

Try this:

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();

  Serial.print("Reading: ");

  units = scale.get_units(10);

  Serial.println(units);

  if (units > 3.35 && units < 3.45)
  {

Railroader:
What doe all the hieroglyphs tell? Viking characters....

Try this:

void loop() {

Serial.print("Reading: ");
 Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
 Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
 Serial.println();

Serial.print("Reading: ");

units = scale.get_units(10);

Serial.println(units);

if (units > 3.35 && units < 3.45)
 {

Thanks for this. However, just tried it out and its still doing the same thing

No, if you have correctly added the new Serial.println(units) the serial monitor output will have changed. I would also suggest changing the prints that just say Reading: to add an identifier like Reading1, Reading2 etc. so the output is easier to read. You can see what is happening and when but we can't. Please do this then post the latest code and the serial monitor output.

Steve

The garbage characters are disturbing. I would like that to be corrected in order to achieve reliable debugging characters.

iflow1:
However, just tried it out and its still doing the same thing

[/quote]

Of course the same bad action will take place. My suggestion intended to tell what values are coming from the reading of the input.

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