Using a library and its variables is not allowing the code to work

Hello everyone,,

I'm using the HX711 multi-library in my project.

However, this library is doing something so weird to my sketch and I narrow it down to this: Whenever I define the library and its variables (even without using it within the code) my whole sketch won't work including serial print function.

Here is a simple example I just did (i'm using Arduino mega):

Using this code; the LCD won't work, and not the serial monitor (Nothing happens)

#include <LiquidCrystal_I2C.h>
#include "HX711-multi.h"
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display


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

#define TARE_TIMEOUT_SECONDS 4 //4 seconds while tare 
byte DOUTS[3] = {DOUT1, DOUT2, DOUT3}; //identify the HX711 modules
#define CHANNEL_COUNT sizeof(DOUTS)/sizeof(byte) //how many hx711 are connected 
long int results[CHANNEL_COUNT]; //number of modules is saved to results
HX711MULTI scales(CHANNEL_COUNT, DOUTS, CLK); //telling the library about number of scales and how are they connected and where is clock connected


const int red[] = {42, 44, 46, 48, 39, 41, 43}; //rgb lights (red pins)


void setup() {
    lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("No problem");
  for (int i = 0; i < 7 ; i++) {
   pinMode(red[i], OUTPUT);
    digitalWrite(red[i], HIGH);
  }
  Serial.begin(9600);
  Serial.print("HI");

}

void loop(){
  
}

And by simply removing the HX711 library part of the above sketch, everything works
the code will become:

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display




const int red[] = {42, 44, 46, 48, 39, 41, 43}; //rgb lights (green pins)


void setup() {
    lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("No problem");
  for (int i = 0; i < 7 ; i++) {
   pinMode(red[i], OUTPUT);
    digitalWrite(red[i], HIGH);
  }
  Serial.begin(9600);
  Serial.print("HI");

}

void loop(){
  
}

Please note that I followed the example given by the library to make the code.

Did I do something wrong? Why would that happen?

Have you tried the original library?
Your link states:

This is a modification of bogde's (GitHub - bogde/HX711: An Arduino library to interface the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter (ADC) for Weight Scales.) HX711 library for the Avia Semiconductor HX711 24-Bit Analog-to-Digital Converter for scales (load cells)

Hi @khamis98
There really is some problem with this library running on Mega arduino,
I ran on Mega and it didn't print anything.
I changed the Mega Pins to be compatible with the Mini Pins and ran it again in Mega, and nothing printed.
I ran it on the Arduino Mini, and it printed Hi on the serial monitor.

I used this lib:

#include <LiquidCrystal_I2C.h>
#include "HX711-multi.h"
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display


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

#define TARE_TIMEOUT_SECONDS 4 //4 seconds while tare 
byte DOUTS[3] = {DOUT1, DOUT2, DOUT3}; //identify the HX711 modules
#define CHANNEL_COUNT sizeof(DOUTS)/sizeof(byte) //how many hx711 are connected 
long int results[CHANNEL_COUNT]; //number of modules is saved to results
HX711MULTI scales(CHANNEL_COUNT, DOUTS, CLK); //telling the library about number of scales and how are they connected and where is clock connected


const int red[] = {5, 5, 7, 8, 9, 10, 11}; //rgb lights (red pins)


void setup() {
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("No problem");
  for (int i = 0; i < 7 ; i++) {
    pinMode(red[i], OUTPUT);
    digitalWrite(red[i], HIGH);
  }
  Serial.begin(9600);
  Serial.print("HI");

}

void loop() {

}

Yes, I have, it didn't have any problems.
However, I need to use this library because I will be using multiple load cells (even more than3)
The thing is i'm really not sure if this is a problem from the library or i did something wrong because the problem is so weird

Hello @ruilviana

Yes this is the same one

I see, then it's definitely the library itself, i was questioning everything hahahaha

Any thoughts on how to fix that?

Hello

Unlike the original library, this one doesn't have a begin method, so it does things like pinMode, digitalRead and digitalWrite in the constructor, which is called before the arduino is fully initialized. It may cause problems.

Try replace this line HX711MULTI scales(CHANNEL_COUNT, DOUTS, CLK);

By this

HX711MULTI * scales;
...
void setup()
{
  scales = new HX711MULTI(CHANNEL_COUNT, DOUTS, CLK);
  ...

A better idea would be to modify this library so the constructor is only used to set up variables, and add a begin method, like in the original library

Did you attempt the example code? No LCD, just console printing.

#include "HX711-multi.h"

// Pins to the load cell amp
#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

#define BOOT_MESSAGE "MIT_ML_SCALE V0.8"

#define TARE_TIMEOUT_SECONDS 4

byte DOUTS[3] = {DOUT1, DOUT2, DOUT3};

#define CHANNEL_COUNT sizeof(DOUTS)/sizeof(byte)

long int results[CHANNEL_COUNT];

HX711MULTI scales(CHANNEL_COUNT, DOUTS, CLK);

void setup() {
  Serial.begin(115200);
  Serial.println(BOOT_MESSAGE);
  Serial.flush();
  pinMode(11,OUTPUT);
  
  tare();
}


void tare() {
  bool tareSuccessful = false;

  unsigned long tareStartTime = millis();
  while (!tareSuccessful && millis()<(tareStartTime+TARE_TIMEOUT_SECONDS*1000)) {
    tareSuccessful = scales.tare(20,10000);  //reject 'tare' if still ringing
  }
}

void sendRawData() {
  scales.read(results);
  for (int i=0; i<scales.get_count(); ++i) {;
    Serial.print( -results[i]);  
    Serial.print( (i!=scales.get_count()-1)?"\t":"\n");
  }  
  delay(10);
}

void loop() {
  
  sendRawData(); //this is for sending raw data, for where everything else is done in processing

  //on serial data (any data) re-tare
  if (Serial.available()>0) {
    while (Serial.available()) {
      Serial.read();
    }
    tare();
  }
 
}

I tried this now, didn't work

How can I do that? i never modified a library before, can you share a hint ?

+1

Ray

yup I did, still nothing happens

Hi
I will search trying to understand the library

Thanks, i will do so too

The "hint" is in the original library and the begin method.

Arduino libraries are two-edged swords: when they work flawlessly, everyone sings about Arduino ease of use. When a library fails, it is like global warming and everyone screams and a few get hurt.

Without the sensors you are using, it is not reasonably appropriate to start out and library-hack in the forum; back and forth, trial and error.

Previously

khamis98
mrburnette
18m
.
Yes, I have, it didn't have any problems.

So the original library works... that is the point to start. Study the multicell library when compared to the known good one. Hopefully you'll track down the issue. But consider that the author said the library worked, so maybe it was ArduinoIDE that changed?

Hi
What I've discovered so far:
The code does not print anything because when creating the instance on this line:
HX711MULTI scales(CHANNEL_COUNT, DOUTS, CLK);
The library uses line 136 of "HX711-multi.cpp"

135: // wait for all the chips to become ready
136: while (!is_ready());

and the processing is stuck inside this whil waiting for some measurement from the HX711 module.

Theoretically, if the HX711 is mounted on the arduino, it should pass this line and print HI on the serial

But why it works with the mini arduino I don't know.

I will start with that now.... and will update the post when i'm done

OR the author didn't try it on mega

Please note that @ruilviana tried it on the Arduino mini and it worked

Alright thanks for pointing that out