How to save loadcell HX711 weight value after restarting Arduino?

Hello,

I am using loadcell HX711.

I have a small container on top of the load cell, and using scale.tare command, so it will calibrate to 0 gr by subtract the container’s weight.

I read some tutorial using the EEPROM.

But I don’t know why it still doesn’t work, when I put something on the container, let’s say item with 3gr weight. When I restart, the weight become 0 gr (eventough there is an item in the container). What I want is when it restarting, the value still 3gr.

How to save weight value after restarting Arduino?

Thank you,
Regards,

Here is my code

#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <DS3231.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <EEPROM.h>
#include <Dhcp.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
LiquidCrystal_I2C lcd(0x3F, 16, 2);


HX711 scale(A1, A0); 
float items_weight = EEPROM.read(5);



void setup() {
  delay(1000);
  scale.power_down();
  delay(100);
  scale.power_up();
  items_weight = scale.get_units();
  delay(1000);
  Serial.begin(115200);

  lcd.init();
  lcd.backlight();
 
  lcd.setCursor(0, 0);
  lcd.print("Weight:");

  scale.set_scale(2280.f); //// this value is obtained by calibrating the scale with known weights
  scale.tare();

}

uint32_t x = 0;

void loop() {

  unsigned long currentMillis = millis();

  scale.power_down();
  delay(100);
  scale.power_up();
  items_weight = scale.get_units();
  EEPROM.write(5, items_weight);
  if (items_weight <= 0.10 ) {
    items_weight = 0;
  }

  lcd.setCursor(8, 0);
  lcd.print(items_weight, 2);
  lcd.setCursor(14, 0);
  lcd.print("gr");

  Serial.print(items_weight, 2);
  Serial.println("gr");

}

You initialise the items_weight to the value read from EEPROM, but then in the setup() method you overwrite it with the value read from the scale.

Thank you, I remove the items_weight code on the setup() method but it still showing 0 gr after restarting (there is item on the container). Is there still any problems with the code? Where should I put the EEPROM code?

Thank you

Or maybe because of scale.tare() on setup() method, so it will calibrate itself to 0 eventough there is an item in the container?

Perhaps always start/restart with empty cup would be smarter since you want to tare the cup.

Only thing to change is how you use it.

Hello @rizkydp. Have you find solutions of your problem with load cell? how to save weight value after restarting arduino? I am searching the solutions. But still not find it. Sorry for my bad english. Thankyou. Waiting your reply

Hi, You need to remove the scale.tare() function from your startup() - tare 'calibrates' the scale to zero to account for any container that you have sitting on it BEFORE you place the items to be weighed. This allows you to use a variety of containers, of differenet weights, but still only weigh the item in the container.

However, if you are using the same container EVERY TIME then you do not need to run scale.tare() at startup(), because the known weight on startup() is always the same.

This is what you do if you use the same container every time.

  1. Calibrate your scale as normal with no load and no container.
  2. Place your container on the scale and record the value (say 10g for example), create a constant called something like 'containerWeight'.
  3. Now in your sketch include a line that subtracts this containerWeight value from the value of the items you are weighing.

EG: itemWeight = measuredWeight - containerWeight;

Now when you restart your Arduino it will simply measure what is on the scale and subtract the container weight. (As I mentioned earlier, this only works if you use the same container every time.)

cul billd