Questions about my HX711+stepper project

When I compile my code the only output I receive is "Sketch uses 5532 bytes (17%) of program storage space. Maximum is 32256 bytes. Global variables use 477 bytes (23%) of dynamic memory, leaving 1571 bytes for local variables. Maximum is 2048 bytes." I made sure that my board and the port were selected properly in Tools. The code should be displaying weight values in pounds(lbs). My arduino uno is connected to a hx711, and a load cell is connected to the hx711. here is my code:

#include <HX711.h>

//Calibrating load cell
const int LOADCELL_DOUT_PIN = A4;
const int LOADCELL_SCK_PIN = A5;
HX711 scale;

float calibration_factor = 15;
float units;
float lbs;

void setup() {
Serial.begin(9600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); }

void loop() {
Serial.println("Load Cell");
Serial.println("Remove anything above load cell");
Serial.println("After readings appear, place weight on load cell");
Serial.println("Press + to increase calibration factor");
Serial.println("Press - to decrease calibration factor");

scale.set_scale();
scale.tare(); //reset scale to 0

long zero_factor = scale.read_average(); //get baseline reading
Serial.print("Zero factor: ");
Serial.println(zero_factor);

scale.set_scale(calibration_factor);
Serial.print("Reading: ");
if (units < 0) {
units = 0.00;
}

lbs = units * 0.0625;
Serial.print(lbs);
Serial.print( "Pounds");
Serial.print(" calibration_factor: ");
Serial.print(calibration_factor);
Serial.println();

if (lbs > 50.00) {
Serial.println("Test is stopped");
}
}

Compiling is the first step. If successful (which it looks like it was) uloads the code to the- board. You then have to open the Serial Monitor to see what your program is displaying with all those Serial.print() statements. Make sure it it configured the same as your sketch (9600 baud)

I opened the serial monitor and it says "message (enter message to send to arduino uno)" and it also says "New line" 9600 baud

You should also see the output of your sketch, which would be

Load Cell
Remove anything above load cell
After readings appear, place weight on load cell
Press + to increase calibration factor
Press - to decrease calibration factor

...


I ran the code again and checked the serial monitor and it is still blank

You have to upload the code. Which Arduino do you have?

Arduino Uno r3

Have you successfully uploaded any program? Post the output of the upload step.


here is the output from the upload step. the output is just continuously running.

Everything appears to be working as programmed.

Put some weight on the scale or load cell.

I'm applying load to the cell but I still keep seeing 0.00 lbs

As said before, it works as programmed.
You put the calibration in the main loop, so each program cycle starts with calibration of the current load as zero point...

You also never read the scale after calibrating it. Your code prints "Reading" but never actually does.

Things that need to be done once (like calibration/tare/etc) should go inside the setup() function, which runs 1 time.

Things that run repeatedly, like actually measuring things, go inside loop() since that function gets calls repeatedly. Look at some of the examples that come with the HX711 library.

Hi @mrman69,

your code seems to be messed up by being incompletely copied together from possibly well-coded sketches ...

You can change your code to become useful but that requires that you understand the general process of using it. Here are some sources that may assist:

https://steemit.com/utopian-io/@drencolha/making-a-weight-scale-with-the-hx711-module-hx711-arduino-library-and-a-load-cell-tutorial

or even more comprehensive here

https://wolles-elektronikkiste.de/en/hx711-based-balance

Good luck!
ec2021

I fixed some stuff and here is my revised code:

#include <HX711.h>

//define pins
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;

const int LOADCELL_DOUT_PIN = A4;
const int LOADCELL_SCK_PIN = A5;
HX711 scale;

//define calibration factor
float calibration_factor = 15;

float load;

void setup() {
  Serial.begin(9600); //Stats serial communication in 9600 baud rate

//Initiate pins as output
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
  
  Serial.println("Initializing scale calibration."); //Prints user commands
  Serial.println("Please remove all weight from scale.");
  Serial.println("Place known weights on scale");
  Serial.println("Press '+' to increase calibration factor by 10");
  Serial.println("Press '-' to decrease calibration factor by 10");
  Serial.println("Press 'C' for tare");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); //Initializes scaling process
                                                    //Used pins are A4 & A5
  scale.set_scale();
  scale.tare(); //reset scale to 0

  }

void loop() { 


digitalWrite(dirPin, HIGH); //Determining the number of steps the motor will take.
for(int x = 0; x < 1400; x++) {  //1400 steps for small, 1200 for medium, 800 for large
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(500);
  if (x%274 == 0) //Incrementing load readings every 274 steps.
  {               //Increments of 274 steps for Small, Inc. of 213 steps for Medium
                  //Inc. of 160 steps for Large
    scale.set_scale(calibration_factor); //Adjusts the calibration factor
    Serial.print("Reading: "); //Prints weight readings in 2 decimal places, in lbs.
    Serial.print(scale.get_units(),2);
    Serial.println("lbs");
    Serial.print("Calibration factor: "); //Prints calibration factor
    Serial.println(calibration_factor);
  }
  load = scale.get_units(); //Storing load value collected from force reading
  if (load >= 50) { //testing if load is >= 50lbs. If true, the loop will be broken
    break;          //And pulses will stop being sent to the motor stopping it
    }
}

  if(Serial.available()) //calibration process starts if there is a serial connection present
  {
    char temp = Serial.read();  //Reads users keyboard inputs

if(temp == '+')  //Increases calibration factor by 10 if '+' key is pressed
  calibration_factor += 10;
  else if(temp == '-') //Decreases calibration factor by 10 if '-' key is pressed
  calibration_factor -= 10;
else if(temp == 'c' || temp == 'C')
scale.tare();   //Reset scale to zero if 'C' key is pressed

}

scale.power_down();  //Puts scale to sleep mode for 5 seconds.
delay(5000);
scale.power_up();   
}

I'm noticing that the reading will be 0.00lbs, then when I apply a load it reads 0.001 lbs so I'm not sure if the load actually being read or not.

Here is my spec sheet for the load cell, wiring schematic and my connections:



Hi,
Why are you using such LARGE GAUGE wire, I can see many many shorts.
What are you soldering with?


Tom... :smiley: :+1: :coffee: :coffee: :coffee: :australia:

Ok, your sketch now includes also a stepper motor.

Would you mind telling us what the intended use of the complete equipment is?

In general it is advisable to make a single setup of the different components, get them to work as expected and then integrate them step by step. It is much easier to identify bugs ...

I second @TomGeorge's concerns about the wires and soldering. If you use smaller wires they'll still work but they are much easier to handle and to avoid unwanted shortcuts.

Greetings
ec2021

Hi,
Did you have to solder the header pins to the UNO or were they already done when you bought it?

A wider angle picture showing all your hardware would be good too.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I am still getting a value of 0 when reading the load with the HX711. I wanted to ask if there is a test code I can run to see if there is a hardware problem. I am getting a multimeter tomorrow to help narrow down the issue. Here is my code, connections, schematic, and spec sheet for the load cell.

#include <HX711.h>

//define pins
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;

const int LOADCELL_DOUT_PIN = A4;
const int LOADCELL_SCK_PIN = A5;
HX711 scale;

//define calibration factor
float calibration_factor = 15;

float load;

void setup() {
  Serial.begin(9600); //Stats serial communication in 9600 baud rate

//Initiate pins as output
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
  
  Serial.println("Initializing scale calibration."); //Prints user commands
  Serial.println("Please remove all weight from scale.");
  Serial.println("Place known weights on scale");
  Serial.println("Press '+' to increase calibration factor by 10");
  Serial.println("Press '-' to decrease calibration factor by 10");
  Serial.println("Press 'C' for tare");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); //Initializes scaling process
                                                    //Used pins are A4 & A5
  scale.set_scale();
  scale.tare(); //reset scale to 0

  }

void loop() { 


digitalWrite(dirPin, HIGH); //Determining the number of steps the motor will take.
for(int x = 0; x < 1400; x++) {  //1400 steps for small, 1200 for medium, 800 for large
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(500);
  if (x%274 == 0) //Incrementing load readings every 274 steps.
  {               //Increments of 274 steps for Small, Inc. of 213 steps for Medium
                  //Inc. of 160 steps for Large
    scale.set_scale(calibration_factor); //Adjusts the calibration factor
    Serial.print("Reading: "); //Prints weight readings in 2 decimal places, in lbs.
    Serial.print(scale.get_units(),2);
    Serial.println("lbs");
    Serial.print("Calibration factor: "); //Prints calibration factor
    Serial.println(calibration_factor);
  }
  load = scale.get_units(); //Storing load value collected from force reading
  if (load >= 50) { //testing if load is >= 50lbs. If true, the loop will be broken
    break;          //And pulses will stop being sent to the motor stopping it
    }
}

  if(Serial.available()) //calibration process starts if there is a serial connection present
  {
    char temp = Serial.read();  //Reads users keyboard inputs

if(temp == '+')  //Increases calibration factor by 10 if '+' key is pressed
  calibration_factor += 10;
  else if(temp == '-') //Decreases calibration factor by 10 if '-' key is pressed
  calibration_factor -= 10;
else if(temp == 'c' || temp == 'C')
scale.tare();   //Reset scale to zero if 'C' key is pressed

}

scale.power_down();  //Puts scale to sleep mode for 5 seconds.
delay(5000);
scale.power_up();   
}