Detect non working HX711 amplifiers

Dear all,

I have developped a large software managing 12 load cells and radiotransmission by Lora radio network, and it works fine.

But I am still looking on following issue to make it more robust :
I want deactivate loadcells that not work, for example with broken wires , to avoid to block the entier system. So in case of issue, Arduino can restart by Wathdog, and deactivates non working loadcells in the setup phase.

To test, I start up Arduino, having disconnected one loadcell amplifier (HX711).
Some times, but not all times, when starting up Arduino in this case, it keeps blocked at the HX711.begin instruction definitefely, until I reconnect the amplifier.

I know the instruction : wait_ready_timeout(500) that solves the issue when programm is already started, but when doing setup, that means the begin() instruction, I do not know how to handle the blocked programm that occurs some times.

How can I test if the amplifier works in the setup() , without blocking the programm ?

thanks for any suggestion

Armin

following code is very simple code that demonstrates the problem :

when a loadcell is not connected, some times it keeps blocked at begin() instruction

#include "HX711.h"

HX711 scale1;
HX711 scale2;
HX711 scale3;

void setup() {
  Serial.begin(57600);
  Serial.println("begin ");
  delay(2000);
  scale1.begin(3, 5);
  Serial.println("begin ok 1");
  scale2.begin(7, 9);
  Serial.println("begin ok 2");
  scale3.begin(11, 13);
  Serial.println("begin ok 3");
}

void loop() {

  if (scale1.wait_ready_retry(10)) {
    long reading = scale1.read();
    Serial.print("HX711 reading: ");
    Serial.println(reading);
  } else {
    Serial.println("HX711 not found.");
  }

  delay(1500);

}

Check out: Documentation for the non-blocking mode.

Paul

Thanks for your answer,

As mentioned in my topic,
the non blocking mode avoids to block the programm if you request a measurement value and the amplifier does not respond ( by defining a timeout) , but same function for begin does not exist ?

Today, if loadcell does not respond any more, the non blocking mode avoids to have problem, but if Arduino restarts for any reasons, programm is blocked ( some times, not any time) on the begin instruction !

thats what I would like to solve ?

You don't need a library to have a timeout for any type of input, just a few lines of code.

For example, to wait until a digital input goes HIGH (with arbitrary timeout), you could use something like this:

unsigned long start_ms = millis();
byte timeout = 0;
while ( (digitalRead(pin) == LOW) && !timeout) {
  if (millis()-start_ms > TIMEOUT) {
     timeout=1; 
    }
  }
if (timeout) restart_arduino();

Thanks for your reply,

Interesting, will be used for other things.

But here, I am blocked at a line, not having unexpected value.

if scale9.begin() blocks, I have to unable scale9 sensor.

But how to create a timeout, when I can be blocked? I can not check delay, I am blocked.

With timer interupts or something like that? not used to this type of functions

Armin

Use a non-blocking version of scale9.begin().

Yes,

I would like to do, but does not exist.

How can I do something like :

setup() {
  define Timeout(2000, Callback);
  scale.begin(5, 7)
}


loop(){
  
}


callback(){
  deactivate sensor; // called if program blocks on begin function
}

Take a look at the code for .begin(). Maybe you can rewrite it.

Hello,

perhaps too high level for me.

I found a very simple solution :

I never payed attention to the fact, that in using a standard Wathdog function using
#include <avr/wdt.h>
you can execute programme code before Wathdog restarts Arduino.

So each time I call the begin() function I memorize in variable the number of HX711 I try to start, and if Wathdog detects blocking, I deactivate this HX711 (activation of amplifiers is memorized in EEPROM), and at restart it is not activated, but only the remaining ones.

Tested long time, works very fine.

Thanks for everbody for your help

armin

I don't understand how begin() can block anything. Here's the code from the library (HX711.cpp):

void HX711::begin(byte dout, byte pd_sck, byte gain) {
	PD_SCK = pd_sck;
	DOUT = dout;

	pinMode(PD_SCK, OUTPUT);
	pinMode(DOUT, INPUT_PULLUP);

	set_gain(gain);
}


void HX711::set_gain(byte gain) {
	switch (gain) {
		case 128:		// channel A, gain factor 128
			GAIN = 1;
			break;
		case 64:		// channel A, gain factor 64
			GAIN = 3;
			break;
		case 32:		// channel B, gain factor 32
			GAIN = 2;
			break;
	}

}

As you can see, begin() just sets up some pins and saves a value for GAIN. Puzzled, of Reading. (Unless it's a different library - this is from HX711_Arduino_Library. (I don't use it - found it too cumbersome, so wrote my own stuff.)).

I agree , it is very strange

but reality is here, even with my extremenly simplified programm, I had the issue !

when I start up this programm, without HX711 connected, in the serial monitor I see : begin, then nothing happens, when later i connect HX711, programms continues ???

#include "HX711.h"

HX711 scale1;
HX711 scale2;
HX711 scale3;


void setup() {

  Serial.begin(57600);
  Serial.println("begin ");
  scale1.begin(3, 5);
  Serial.println("begin ok 1");
  scale2.begin(7, 9);
  Serial.println("begin ok 2");
  scale3.begin(11, 13);
  Serial.println("begin ok 3");

  
}

void loop() {
   delay(1500);

}

emailarmin:
I agree , it is very strange

Which library are you using? There are several for the HX711.

emailarmin:

That's the library I clipped the code-segment from. Nothing in there to make begin() hang. Something odd going on!!