HX711 LoadCell tare with known weight

Hello everyone ,
I used HX711 ADC module with Arduino IDE in that i used HX711 Arduino Library
→ In that i used this code for measure weight.
→I measured calibration factor already.
→I want to store current values on reset..

/**
 * Complete project details at https://RandomNerdTutorials.com/arduino-load-cell-hx711/
 *
 * HX711 library for Arduino - example file
 * https://github.com/bogde/HX711
 *
 * MIT License
 * (c) 2018 Bogdan Necula
 *
**/

#include <Arduino.h>
#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 13;
const int LOADCELL_SCK_PIN = 14;

HX711 scale;

void setup() {
  Serial.begin(115200);
  Serial.println("HX711 Demo");
  Serial.println("Initializing the scale");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  Serial.println("Before setting up the scale:");
  Serial.print("read: \t\t");
  Serial.println(scale.read());      // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));   // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
            // by the SCALE parameter (not set yet)
            
  scale.set_scale(-23);
  //scale.set_scale(-471.497);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
            // by the SCALE parameter set with set_scale

  Serial.println("Readings:");
}

void loop() {
  Serial.print("one reading:\t");
  Serial.println(scale.get_units() / 1000);

` // delay(5000);
}

in this code when i reset device with known weight the output is showed 0 kg. My known weight is 1 KG i put on scale and reset the device so my weight is tare.

I want solution for when i reset device my load weight is same as privous readings in short i do not won`t to tare while resetting.So when i get my device power off and on my calibrartion and parameters are set accordind to privious readings.??????........

Scale calibration is doing with two known weight and produce two calibration parameters: i) offset - value from the scale without weight and ii) scale bias - scale units to weight units ratio.
You need a tare scale first with zero weight, then with known weight (say 1 kg), calculate the both coefficients and store it in EEPROM. After that you will can use it later for weighting your load.

Can you elaborate more ???

What more?
See the HX_calibration.ino library example

HX_calibration.ino in library there is given for calibration measurement when i calibrate with no load there is guru meditation error occurs and my weight is shows infinity. And i unable to measure weight.
→ I saved calibration value in EEPROM i used that value in below section


  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
            // by the SCALE parameter (not set yet)
            
  scale.set_scale(-23);
  //scale.set_scale(-471.497);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

But when i reset my device my load is tared and load cell give 0 output.
when i move load from the cell the output should be in negative.

Your mistake is using the tare() at every program run.

You should use tare() only once, then store the parameter in EEPROM and remove line with tare() command from the code.

Which parameter should i store in EEPROM?According to this code.....

/**
 * Complete project details at https://RandomNerdTutorials.com/arduino-load-cell-hx711/
 *
 * HX711 library for Arduino - example file
 * https://github.com/bogde/HX711
 *
 * MIT License
 * (c) 2018 Bogdan Necula
 *
**/

#include <Arduino.h>
#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 13;
const int LOADCELL_SCK_PIN = 14;

HX711 scale;

void setup() {
  Serial.begin(115200);
  Serial.println("HX711 Demo");
  Serial.println("Initializing the scale");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  Serial.println("Before setting up the scale:");
  Serial.print("read: \t\t");
  Serial.println(scale.read());      // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));   // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight (not set yet)

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
            // by the SCALE parameter (not set yet)
            
  scale.set_scale(-23);
  //scale.set_scale(-471.497);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
            // by the SCALE parameter set with set_scale

  Serial.println("Readings:");
}

void loop() {
  Serial.print("one reading:\t");
  Serial.println(scale.get_units() / 1000);

// delay(5000);
}

For getting required output

Also I don't see where you read the calibration parameters from the scale and sore it in the EEPROM. Without it you can hardly expect your weight to be measured correctly

This code is incomplete and uses a different outdated HX711 library, as I see.
Look at this

As I mentioned above, see the HX_calibration.ino library example.
According to the example code you need to store OFFSET and SCALE parameters.

1 Like

I use these functions for save value in EEPROM

void calibration(){
  if (scale.is_ready()) {
    scale.set_scale();    
    Serial.println("Tare... remove any weights from the scale.");
    delay(5000);
    scale.tare();
    Serial.println("Tare done...");
    Serial.print("Place a known weight on the scale...");
    delay(5000);
    long reading = scale.get_units(10);
    Serial.print("Result: ");
    Serial.println(reading);
    seprom();
}

void seprom() {
  byte bytes[sizeof(float)];
  memcpy(bytes, &reading, sizeof(float));
  for (int addr1 = 0; addr1 < sizeof(float); addr1++) {
    EEPROM.write(addr + addr1, bytes[addr1]);
  }
  EEPROM.commit();
}
void readprom() {
  byte bytes[sizeof(float)];
  int val1;
  for (val1 = 0; val1 < sizeof(float); val1++) {
    bytes[val1] = EEPROM.read(addr + val1);
  }
  memcpy(&reading, bytes, sizeof(float));
  Serial.print("EEPROM Value:");
  Serial.println(reading);

}

You didn't read calibration parameters from scale, didn't save it in any variables. As about writing to EEPROM - you write the nothing,

Correction: you try to read and store scale parameter, but not the OFFSET.
And your way to use local variable reading in the another function is very likely path to errors.

The correct way to use reading in seprom() function is pass it as function parameter:

so in calibration() it will be:

and why did you use sizeof(float) if your reading has long type?

From this solution i solved my issues and my scale run perfectly..
Thankyou......:grin:

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