Helllo,
I am using the HX711 module and load cell with an arduino uno for force measurements.
I am using this library.
I first use the example "Calibration" code for calibration with a 200 g weight. Then I save the calibration constant to the eeprom (choosing yes on the serial monitor). Then the load cell is able to measure weights accurately.
After that I remove the weight from the load cell and run this code after uncommenting the last line of this part of the above code:
LoadCell.begin();
//LoadCell.setReverseOutput();
float calibrationValue;
calibrationValue = 696.0;
#if defined(ESP8266)|| defined(ESP32)
//EEPROM.begin(512);
#endif
EEPROM.get(calVal_eepromAdress, calibrationValue); // I have uncommented this
The weights are measured accurately.
But when before powering the arduino, if I keep a weight on the load cell that weight is taken to be 0.
For this I have done the following change to the code:
boolean _tare = false;
But I get the following output on the serial monitor (Note: The following output is obtained on startup when there is/there is no weight on the load cell):
Questions: What is the use of the following line:
calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
Is it useless for the code I am using above? Because I am using the calibration value from the eeprom.
Q2) Why is it showing -39000 as an output value? And how to correct it?
Alternate Statement: In other words, I want to use the HX711 module with the arduino and a load cell. I will first calibrate it with a known weight and save the calibration value. Before powering up the arduino I will keep a weight on the load cell. After powering up the weight value should be the absolute value instead of making the Weight Null.
EDIT1: I incorporate the changes suggested in the answers by @Edgar Bonet and also make the problem more clear just in case. According to suggestions I have made the corresponding changes to the calibration code. Then I run that and the values are saved to eeprom. Code changes:
const int calVal_eepromAdress = 0;
const int tare_eepromAdress = sizeof(float);
and
#endif
EEPROM.put(calVal_eepromAdress, newCalibrationValue);
EEPROM.put(tare_eepromAdress, LoadCell.getTareOffset());
#if defined(ESP8266)|| defined(ESP32)
EEPROM.commit();
Now, I wish to run the second script and for this to display the correct weights. This should work in all cases regardless of whether a weight has not been kept on the load cell or has been kept and should be independent of the weight that has been kept. To this script I have made the following changes:
const int calVal_eepromAdress = 0;
const int tare_eepromAdress = sizeof(float);
and
// Inside void setup()
void setup() {
Serial.begin(57600); delay(10);
Serial.println();
Serial.println("Starting...");
LoadCell.begin();
//LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
float calibrationValue; // calibration value (see example file "Calibration.ino")
float tareValue;
calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
tareValue = 0;
#if defined(ESP8266)|| defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
EEPROM.get(tare_eepromAdress, tareValue);
unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
LoadCell.setTareOffset(tareValue);
Serial.println("Startup is complete");
}
}
EDIT2:
#if defined(ESP8266)|| defined(ESP32)
EEPROM.begin(512);
#endif
EEPROM.put(calVal_eepromAdress, newCalibrationValue);
EEPROM.put(tare_eepromAdress, LoadCell.getTareOffset());
Serial.println(newCalibrationValue);
Serial.println(LoadCell.getTareOffset());
Output:
-206.90
8368790
In the second script:
#if defined(ESP8266)|| defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
EEPROM.get(tare_eepromAdress, tareValue);
Serial.println(calibrationValue);
Serial.println(tareValue);
Output:
-206.90
0.00
I am uploading my codes onto Github just in case. They can be found here.
Q) What is happening here?
EDIT3: The following changes make it work like a charm: In the "Calibration" script:
long tareValue = LoadCell.getTareOffset(); // Instead of float tareValue
Similarly in the second script:
long tareValue; // Instead of float tareValue
Persistent_zero_offset.ino
.