I will be using the EEPROM to store distance and time for a bike speedometer, and load them at startup in case of switching off. But switching off may not have a warning. These values will change just about every second, or even more at times. Is it safe/recommended to write to the EEPROM that often? Is there a finite amount of times it can be written to before failing? Should I limit it to writing only when the rider has stopped, so maybe once every few minutes? If it makes any difference I am storing two longs split up into the first 8 bytes of the EEPROM.
A second side question: If I plan to use power down sleep mode a lot, can I trust my variables to stay in tact or should I read from the EEPROM when waking from power down? I suppose if that was a problem, I would have bigger problems to worry about so I would assume that is not necessary.
From the atmega328 datasheet: The EEPROM has an endurance of at least 100,000 write/erase cycles.
So suppose you want to write data every second it would last for about 27 hours. Not the best way to go
EEPROM is more intended for storing data which doesn't change that much, like look-up tables.
You might consider connecting a SD-card (with SD-shield) to your arduino and log your data to the card. This makes it easier to process the data on your computer as well.
Always read the data that is in the eeprom and write only the changed bytes, there are macros that write data, but there is also newer additions that update:
eeprom_write_byte(); //Writes data, regardless of the eeprom value.
eeprom_update_byte(); //Writes only the bytes that are different to the eeprom contents.
Also by dividing your eeprom into two ( or more ) banks, and use them sequentially, you effectively double the lifetime you will get out of the eeprom.
CrossRoads:
You can also use a FRAM chip (mouser.com)
Has non-volatilty of EEPROM, but waaay higher unendurance,10^14 write cycles,
and access speed of SRAM.
I was really wanting to try FRAM for a datalogger project I'm designing, but the cost is way out there. I can get 2Mb EEPROMs for about the same price as a 16KB (128Kb) FRAM. 2Mb FRAMs cost about 5.5 times as much as an EEPROM with equivalent capacity. For some reason I thought it was quite a bit more cost-effective than that. The EEPROM I'm looking at has 4M write-cycle endurance and 200-year data retention. Not even close to that of FRAM, but neither will I ever wear it out. Given the low-bandwidth nature of the thing, I doubt that the slower access speed will be a factor, either.
Anyhoo just disappointed in the pricing. If you come across any deals though, be sure and give a holler!
It's not difficult to design the hardware to provide power to the mcu for a fraction of a second after the power supply is removed. Then you can have the mcu detect the loss of power and write to flash at that time.
To provide the reserve power, use a diode between the battery and the 5V (or whatever) regulator input, along with a large capacitor to ground. To detect loss of power, use a voltage divider feeding an analog input.