Understanding saving and using EEPROM

Hi,

I am TOTALLY new to programming and this is my very FIRST project. I have worked out how to do most of the coding and things with the help of you guys by reading similar questions and answers you guys gave other people but I am having one problem now and I can't seem to wrap my head around it, hoping someone here has a solution for me.

I am using:
Original Arduino UNO
Load Cell 1kg
HX711 Module
SG90 Servo Motor

Workings of the project:
There is a weight on the load cell when the weight is more than 50grams the servo motor stays forever in 0 degrees but when the load cell picks up that the weight is less than 50 grams it triggers the servo motor to go to 90 degrees. There is a delay of 5 seconds with the servo motor on 90 degrees and then it will put the servo motor to 0 degress. The loop starts all over. The code will check the weight again and see if the weight is more than 50 grams and if it is more than 50 grams it will keep the servo motor on 0 degrees OR it will see it's less than 50grams again and the servo motor will go to 90 degrees for 5 seconds and after 5 seconds it will put the servo motor to 0 degrees again. I have calibrated the load cell and saved the value to the EEPROM when I first initialized the load cell. I also used the calibration value in my Setup to get the correct value of the weight on the load cell.

The problem I am having:
When there is already a weight on the load cell and we lose power for some or other reason, when the power comes back on some time later, the load cell sees the weight as zero even though there is a weight on it. I need to have it calculate the weight of what is on the scale after starting up but it doesn't. How can I do that??

I read you can use EEPROM but I don't even know if it is needed but as far as I know I read that if you want something to be saved while restarting happens then EEPROM is the answer and so I read a lot about EEPROM.put and EEPROM.get but I still have no idea how to actually put it into my code to utilize it and have it "translate" the weight on the scale after starting up.

As I was trying to solve it, I read up about it, I got to a sketch to "test" or read what is my current value in my EEPROM and I got a result which is correct: 2080.02

So I know the EEPROM has it saved and it is available but I still have no idea how to use it in a way to tell my program it should use it every time it starts up to calculate the current weight on the scale and not reset it to zero every time after it restarts.

I am hoping someone would be able to explain it to me in a way I can understand or show me just how it should look like perhaps??

Schematic:

My Code

#include <HX711_ADC.h>
#include <Servo.h>
#include <EEPROM.h>

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;

int servoPin=9;
int servoPos=0;
Servo myServo;
HX711_ADC LoadCell(4, 5);

void setup() {
  Serial.begin(57600);
  delay(10);
  Serial.println();
  myServo.attach(servoPin);
  LoadCell.begin();
  LoadCell.start(1000);
  LoadCell.setCalFactor(2080.02);
  startMillis = millis();
}

void loop() {
  LoadCell.update();
  float i = LoadCell.getData();
  Serial.println(i);
   if (i < 50)
      myServo.write(90);
      currentMillis = millis();
      if (currentMillis - startMillis >= period)
  {
    myServo.write(servoPos);
    startMillis = currentMillis;
  }
  
}

Thanx for all the help in advance.

This function also tare's the scale. If you don't want to tare the scale

LoadCell.start(1000, false);

1 Like

Thank you so much, that stopped the reseting to zero after losing power with a weight on from happening but I have no idea why but since I put the (1000, false); thing in my calibration is off ..... no weight on the scale showed 0.00 and putting 100grams on showed 100.00 but when I put the false in the code, my "zero" or no weight measurement went to 3821.20 measurement, so I don't know why it basically throws my calibration out .... if I put a 100gram weight on, it picks up the 100 grams and displays 3921.20 but the "false" added basically 3821.20 to the scale reading. Any idea why? or how to fix this problem?

1 Like

Go look at the examples that come with the library. Several show setting and restoring calibration, etc.

1 Like

Instead of the start with tare, you want to restart without tare. See https://github.com/olkal/HX711_ADC/blob/8086d5c50cfd4b969bd58266d9d07ce5c0e1b138/src/HX711_ADC.cpp#L60

   LoadCell.start(1000,FALSE);

You'll also need to add code to manage the tare value,

Per https://github.com/olkal/HX711_ADC/blob/8086d5c50cfd4b969bd58266d9d07ce5c0e1b138/src/HX711_ADC.cpp#L414 and https://github.com/olkal/HX711_ADC/blob/8086d5c50cfd4b969bd58266d9d07ce5c0e1b138/src/HX711_ADC.cpp#L420 there is a getTareOffset and setTareOffset

When storing a calibration in EEPROM, you should also store the tareOffset. On reboot, you should read the calibration and the tareOffset and restore them. I think I'd add a storeParameters button and function, and a restoreParameters button and function.

ETA: https://github.com/olkal/HX711_ADC/blob/master/examples/Persistent_zero_offet/Persistent_zero_offset.ino has some demo code for the saving ad resetting the tare with EEPROM.

Thank you for the help so far, it doesn't tare any more. It works now if I restart and keep the weight on but there is a new problem lol

with no load on the load cell the following code gives me 0.00 which is correct

  LoadCell.start(1000);
  LoadCell.setCalFactor(2080.02);

but as soon as I write the following code and upload it to the ardunio

  LoadCell.start(1000, false);
  LoadCell.setCalFactor(2080.02);

my Serial Monitor says now 3821.20

so by adding false to that it added 3821.20 grams to my reading

Still trying to see how to fix that

I suspect that you will need a button for the user to be able to force tare. The scale can't tell whether a weight was left on the scale during a power outage.

You could use EEPROM to record that a weight was there last you knew, but who knows whether it still is?

2 Likes

.setCalFactor() is not the same as .setTareOffset(). You need to manage both.

1 Like

I assume I have "managed" the .setCalFactor() by using:

LoadCell.setCalFactor(2080.02);

I don't have a glue how to "manage" the .setTareOffset()

So since I am getting 3821.02 MORE weight added I thought I could offset it this way:

LoadCell.setTareOffset(3821.02);

but that didn't work because it gave me an error saying:

'Loadcell' was not declared in this scope

I tried to search and read how to implement this .setTareOffset() but I can't find any source to explain what I need to do to offset this 3821.02 amount that is counted extra.

I see an edit happened ... hahaha .... let me work through this because I didn't read the updated stuff .... thank you so much

Nope, I am an idiot with coding. I don't get it to see the tareoffset story. I am wondering if it won't just be easier to forget the tareoffset lol

I want to measure 50grams .... at the moment it is measuring 3821.02 ... so perhaps I should program the code to "see" 3821.02 as zero and add 50 to it lol

so then it would like this .....

if (i < 3871.02)
      myServo.write(90);
      currentMillis = millis();
      if (currentMillis - startMillis >= period)
  {
    myServo.write(servoPos);
    startMillis = currentMillis;
  }

I think that would be the quickest fix since I can't figure this setTareOffset thing out :slight_smile:

I actually used the similar thing but got it to show ZERO and if I put a weight on it shows the correct weight :slight_smile:

#include <HX711_ADC.h>
#include <Servo.h>
#include <EEPROM.h>

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000;
 
int servoPin=9;
Servo myServo;
HX711_ADC LoadCell(4, 5);

void setup() {
  Serial.begin(57600);
  Serial.println();
  myServo.attach(servoPin);
  LoadCell.begin();
  LoadCell.start(3000,false);
  LoadCell.setCalFactor(2246.83);
  startMillis = millis();
}

void loop() {
  LoadCell.update();
  float i = LoadCell.getData()-3820.52;
  Serial.println(i);
  if (i < 50)
      myServo.write(0);
      currentMillis = millis();
      if (currentMillis - startMillis >= period)
  {
    myServo.write(90);
    startMillis = currentMillis;
  }
  
}

I just want to thank everyone for helping and letting you know your knowledge and time was truly appreciated :slight_smile:

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