10x HX711 code problem

Good day,
as a beginner I need advice on a project.
I have a code with 10 scales with hx711 but the code freezes during the process. On the LCD whenever it has to display the value from the scale.

Thanks for the advice.

code:

#include <LiquidCrystal_I2C.h>     
LiquidCrystal_I2C lcd( 0x27, 20, 4 );


    
#include "HX711.h"

byte c_hacek[8] = {  //definice znaku c s háčkem = č
  B01010,           // jednička svítí
  B00100,           // nula nesvítí
  B01110,
  B10000,
  B10000,
  B10001,
  B01110,
  };

#define calibration_factor_1 23000 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define calibration_factor_2 23500 
#define calibration_factor_3 23500 
#define calibration_factor_4 -22300 
#define calibration_factor_5 23500
#define calibration_factor_6 23500
#define calibration_factor_7 23500
#define calibration_factor_8 23500
#define calibration_factor_9 23500
#define calibration_factor_10 23500

#define DOUT1  28
#define CLK1  29
const int Tare1 = 25;
#define DOUT2  30
#define CLK2  31
const int Tare2 = 22;
#define DOUT3  34
#define CLK3  36
const int Tare3 = 23;
#define DOUT4  38
#define CLK4  40
const int Tare4 = 24;
#define DOUT5  5
#define CLK5  6
const int Tare5 = 26;
#define DOUT6  42
#define CLK6  44
const int Tare6 = 8;  
#define DOUT7  47
#define CLK7  49 
const int Tare7 = 9;   
#define DOUT8  51
#define CLK8  53
const int Tare8 = 12; 
#define DOUT9  2
#define CLK9  3
const int Tare9 = 4;
#define DOUT10  32
#define CLK10  33
const int Tare10 = 7;




HX711 scale1;
HX711 scale2;
HX711 scale3;
HX711 scale4;
HX711 scale5;
HX711 scale6;
HX711 scale7;
HX711 scale8;
HX711 scale9;
HX711 scale10;

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


lcd.init( );
  lcd.backlight( );

lcd.createChar(1, c_hacek);   //odeslání definice 2. znaku


  scale1.begin(DOUT1, CLK1);
  pinMode(Tare1, INPUT);
  digitalWrite(Tare1, LOW);
  scale1.set_scale(calibration_factor_1); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale1.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  
  scale2.begin(DOUT2, CLK2);
  pinMode(Tare2, INPUT);
  digitalWrite(Tare2, LOW);
  scale2.set_scale(calibration_factor_2); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale2.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  
  scale3.begin(DOUT3, CLK3);
  pinMode(Tare3, INPUT);
  digitalWrite(Tare3, LOW);
  scale3.set_scale(calibration_factor_3); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale3.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
  
  scale4.begin(DOUT4, CLK4);
  pinMode(Tare4, INPUT);
  digitalWrite(Tare4, LOW);
  scale4.set_scale(calibration_factor_4); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale4.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  scale5.begin(DOUT5, CLK5);
  pinMode(Tare5, INPUT);
  digitalWrite(Tare5, LOW);
  scale5.set_scale(calibration_factor_5); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale5.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  scale6.begin(DOUT6, CLK6);
  pinMode(Tare6, INPUT);
  digitalWrite(Tare6, LOW);
  scale6.set_scale(calibration_factor_6); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale6.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  scale7.begin(DOUT7, CLK7);
  pinMode(Tare7, INPUT);
  digitalWrite(Tare7, LOW);
  scale7.set_scale(calibration_factor_7); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale7.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  scale8.begin(DOUT8, CLK8);
  pinMode(Tare8, INPUT);
  digitalWrite(Tare8, LOW);
  scale8.set_scale(calibration_factor_8); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale8.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  scale9.begin(DOUT9, CLK9);
  pinMode(Tare9, INPUT);
  digitalWrite(Tare9, LOW);
  scale9.set_scale(calibration_factor_9); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale9.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  scale10.begin(DOUT10, CLK10);
  pinMode(Tare10, INPUT);
  digitalWrite(Tare10, LOW);
  scale10.set_scale(calibration_factor_10); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale10.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  
  Serial.println("Readings:");
}

void loop() {
  lcd.clear() ;
  lcd.setCursor(0,0);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 1:");
  lcd.setCursor(12,0);
  lcd.print(scale1.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  
  /*Serial.print("Hmotnost: ");
  Serial.print(scale2.get_units(),1); //scale.get_units() returns a float
  Serial.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  */
  lcd.setCursor(0,1);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 2:");
  lcd.setCursor(12,1);
  lcd.print(scale2.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  
  /*Serial.print("Hmotnost: ");
  Serial.print(scale3.get_units(),1); //scale.get_units() returns a float
  Serial.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  */
  lcd.setCursor(0,2);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 3:");
  lcd.setCursor(12,2);
  lcd.print(scale3.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  
  /*Serial.print("Hmotnost: ");
  Serial.print(scale4.get_units(),1); //scale.get_units() returns a float
  Serial.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  */
  lcd.setCursor(0,3);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 4:");
  lcd.setCursor(12,3);
  lcd.print(scale4.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  delay (2000);
  lcd.clear() ;
  lcd.setCursor(0,0);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 5:");
  delay (2000);
  lcd.setCursor(12,0);
  lcd.print(scale5.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  
  lcd.setCursor(0,1);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 6:");
  lcd.setCursor(12,1);
  lcd.print(scale6.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  
  lcd.setCursor(0,2);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 7:");
  lcd.setCursor(12,2);
  lcd.print(scale7.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor

  delay(2000);
  lcd.clear() ;
  lcd.setCursor(0,0);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 8:");
  lcd.setCursor(12,0);
  lcd.print(scale8.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
 
  lcd.setCursor(0,1);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 9:");
  lcd.setCursor(12,1);
  lcd.print(scale9.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor

  lcd.setCursor(0,2);
  lcd.print("V");
  lcd.write(byte(1));    //zobrazí č (znak 1)
  lcd.print("elstvo 10:");
  lcd.setCursor(12,2);
  lcd.print(scale10.get_units(),1); //scale.get_units() returns a float
  lcd.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
  
  delay(2000);
}

What messages appears on the lcd before the execution freezes?

Read upon arrays and looping methods.

They will shrink your dude by at least 50% and make it much easier to read and debug.

1 Like

For example, this is last picture from lcd, but it is always before the value from the scale displayed.

Can I ask for some guidance on how to modify my code to a working state.

Not on how to modify your code, as it would be more of a complete do over.

If you were to go back, I assume, to where you had one sensor only, reporting successfully on the LCD, you'd have a sketch with everything you need to do N = 10 of them.

You may have not used copy, paste and edit, but that is what it looks like, and for a person at a certain point in the learning curve a very rapid way to develop a program.

But. It turns into a bit of a nightmare, when what you are looking for is typos from the editing, or when a mistake discovered has to be (carefully) applied to N blocks of code. Then the search for more typos.

So… the guidance I offer is to learn how to program.

Srsly, I know there's a fire, but you are using the equivalent of a squirt gun to extinguish it.

When you can, learn the next set of features usually presented, and available, in any programming language. A short list of search terms, or to help you find information in whatever way you do, beyond asking for help here:

  • array variables

  • for loops

  • functions

Googling any of those three, plus "arduino", or "C++" or even "programming" will lead you to a firehose (!) of information.

To wet your appetite.

Array variables give one name to as many items of one type (16 bit integers, for example) as you need. Tell the code which one you are meaning just now with an index, just a number, Numbers can come from anywhere - a constant, a variable or an expression that evaluates to a number.

for loops, at the simplest, let you make a variable take on every value between limits that you program. That number could indicate which array variable you talking about. Which of N sensors.

functions let you encapsulate an algorithm. Other code can call your function, saying something that makes the function work on the problem at hand (which of N sensors, e.g.); the function does so and then returns the result for your use at what we term a higher level. Or does something like print a value to an LCD screen.

So all your scale constants and pins numbers and even your sensors could each be just an array.

The N sensors could be "looped over" with a for statement and whatever needs be done for each represented by just one block of code.

Lastly, that block of code could be the basis for a function, so all the for loop actually needs to do is (drum roll, please, I'm almost finished) call that function.

These three concepts are part of every programming language, and in many are expressed almost identically, with but minor tweaks in syntax.

When you know a language like C, it is super easy to start reading and programming in numerous other languages. Just because these basic (or one small step for a man above basic) ideas are central to this kind of coding.

OK, I'm done. I do hope that helps and further hope you find the time to learn more, as every single thing you learn now will pay off handsomely for the rest of your activities around programming.

Post a sketch that works for one sensor.

a7

How did you connect these 10 HX711 to arduino? As far as I know, they are not adressable..

Look at the code, then look at the source code of the library being used.

It seem that each sensor gets its own clock and data pin, set by the begin() method.

HTH

a7

As alto 777 writes, each scale has its own clock and sata pin

The HX711-multi library uses a common clock pin, so 11 pins for 10 sensors.
Leo..

I would suggest that you first get the HX711 multi example from the library to work. HX711 there use the common clock:

#define CLK A0      // clock pin to the load cell amp
#define DOUT1 A1    // data pin to the first lca
#define DOUT2 A2    // data pin to the second lca
#define DOUT3 A3    // data pin to the third lca

While you use clocks all over.

#define DOUT1  28
#define CLK1  29
const int Tare1 = 25;
#define DOUT2  30
#define CLK2  31
const int Tare2 = 22;
#define DOUT3  34
#define CLK3  36
const int Tare3 = 23;
#define DOUT4  38
#define CLK4  40
const int Tare4 = 24;
#define DOUT5  5
#define CLK5  6

Forget about the LCD for now, get the HX711 units working, with code and hardware setup. Then focus on the LCD.

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