0

How can I save my output continuously to EEPROM? Or is this even possible? Please help me out.

void setup() {
  Serial.begin(9600);
}

void loop() {
  static unsigned long count = 0;
  static float sum = 0;
  float ave = 0;
  int volt = analogRead(A1);
  float voltage = map(volt, 0, 1023, 0, 2500) / 100.0;
  Serial.print("Voltage: ");
  Serial.println(voltage, 2);
  sum = sum + voltage;
  count++;
  ave = sum / count;
  Serial.print("Average: ");
  Serial.println(ave, 2);
  Serial.println("-----");
  delay(3000);
}
1
  • Add an FRAM to your system. They can support 10 trillion writes. 34 billion years worth of writes. Plus they can be accessed at SRAM speeds, while the onboard EEPROM takes 3.3mS to write to it, and can be corrrupted if power is lost during the write. With FRAM, you can toggle between 2 addresses, if one gets lost you can back up to the good one and only be a few seconds behind. (you can do that with EEPROM too)
    – CrossRoads
    Jan 14, 2020 at 17:01

2 Answers 2

1

It's possible, but it's certainly not advised.

EEPROM has a limited number of writes before it's dead. By writing continuously to the EEPROM you will kill it in no time at all.

2
  • How limited is it? Maybe I can adjust the seconds on when I will save data per day? Dec 15, 2019 at 13:17
  • 1
    100,000 IIRC but I'd have to check the datasheet. Better to use something like a 47L16 EERAM.
    – Majenko
    Dec 15, 2019 at 13:19
1

The answer is: Read the Arduino Reference and find EEPROM.put

But your actual problem is that

1) if you write to eeprom every 3 seconds, the Arduino eventually won't survive you

2) you eventually rather want to write the sum and the count, in your case ?

You should also think about how to initialize from EEPROM after Restart.

Perhaps it's sufficient to save restart values only once after 1 minute / 1 hour / daily if it still changes?

BTW the "Update only if it changed" is already built into the put() method.

4
  • What if i will just store my latest data? Every data it will received will be replaced by new one every 3sec. As long as I can save or store my last data, that will be fine. Dec 15, 2019 at 15:29
  • 1
    You might like to consider whether there's a chance to detect the power-down before the Arduino actually stops working. If detected your values are saved, some milliseconds suffice. This could be done with some hardware but depends on your environment. Dec 15, 2019 at 19:30
  • @Rica Jacutina: That was my basic idea as well. If you always store the latest data every 3 sec, you will store them 28800 times a day. atmega EEPROM has a lifetime of about 100000 writes, so your system survives 4 days. Not very smart for an "average since ever". You can write them to different locations and use all EEPROM memory and thus extend lifetime to about a year, but the busybee's idea is much better. Dec 16, 2019 at 23:06
  • I'll try your suggestions. Thanks a lot! Dec 17, 2019 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.