Hello
I want to change an adress for a value stored in the EEPROM if 100k write cycles are reached as this is
the guarenteed limit for an EEPROM. Is there a library available which has got such a function. I want to achieve this for 10 values each. I defined the adresses as follows which does not yet consider write cycles:
I don't know of a specific library to recommend to you, but the keywords to search for are: EEPROM wear leveling. There is definitely some example code available if there isn't an Arduino library.
marcel00:
How many write cycles could be reached if using a mega 2560 and no other values are stored?
The naive answer would be:
100000 writes/byte x 4096 bytes / (4 floats x 4 bytes/float + 6 longs x 4 bytes/long) = 10240000
However, it's not so simple. The wear leveling code will use some EEPROM to store its state, so that reduces the above number a bit. However, you will also want to only write to EEPROM when the values have changes (as EEPROM.put() and EEPROM.update() do automatically), so that could also increase the above number in practice.
Why are you writing EEPROM so often ?
A common solution to this type of problem is to save the runtime variables in RAM, while watching the raw power supply voltage.
If the supply starts to go south, then write those RAM values across to EEPROM. (Use a diode and capacitor to hold Vcc up for a few milliseconds.)
If the voltage recovers, continue operation as normal.
If it goes down, you have the values saved*.
Next restart, you can recover those values from EEPROM.
If you ever need a controlled shutdown or restart, simply call the same EEPROM save routine before you go down.
It all works the way it should. I’ve done this for a few years (for millis counters among other things) without a missed drop, and probably 200 total writes in that two year period.
If you need wear levelling, that can be implemented as well, but is there to conserve EEPROM life, not replace good practices.
One of the values is a daily trip driven by electric vehicles, which can be reset to zero. I use a keyswitch which in case of turn off enables the saving of values to EEPROM such that if keyswitch is turned on values can be read from EEPROM again. The vehicles will be used by customers and I am not sure how often they press reset or turn key on and off. So 100k of write cycles could be easily reached. The MCU is permanently powered but can be disconnected from battery by a main switch in case of emergency. After shut off the values should be available again.
So there you are...
Save to RAM unless there’s a reason.
Assuming it gets powered off, or power loss to EEPROM - for any reason once a day, 100K days is probably longer than the vehicle will be around.
Twice a day will start to be a problem in ~136 years.
You seem to have some variables of type long. If they can be unsigned long, the sentinel bit that is mentioned in the article can be part of one of those; depends on how those longs are used.
PS
Based on the fact that you have individual addresses, you might want to store your data in a struct and use eeprom.put() and eeprom.get() to store everything in one go.
RAM is ‘better’ for active runtime storage.
It’s faster and doesn’t ‘wear out’
EEPROM should only be used for ‘persistent’ non-volatile storage when the processor is unable to run.
It’s slower, and has a limited number of write cycles.