EEPROM Programming


I'm new to Arduino and programming in general, however I'm muddling my way through by following the examples, watching tutorials etc.

I'm building a test rig that is intended to complete 250,000 cycles. I'm going to be logging data at regular intervals onto an SD card, perhaps creating new files per set interval or time scale i.e per day, week, etc. I'm trying to come up with a solution for storing the total completed cycles perhaps to the nearest 10 cycles. Should the power be lost to the rig, on power up I'd like to read the total completed cycles to call into my program loop.

Would it be feasible to use the EEPROM on my UNO for this function? If so, how would I go about it?


I would do this in two parts:

  1. Use one Arduino to gather the data.

  2. Use a separate Arduino, serial data logger to store the data.


You can use and a FRAM memory to store the data. Can access it as fast as you would an SRAM, yet it has EEPROMs nonvolatility without EEPROMs slow (3.3mS) write speed.
Say you used an 8K FRAM. Every 10 cycles you set a bit in one address, working your way across.
8192 bytes x 8 bits/byte x 10 cycles per bit = 655,360 cycles.
Or use a smaller part, and every 10 cycles update an unsigned long number stored in a location.
With 1 Trillion to 100 Trillion write cycles endurance, you could log every cycle if you wanted.

How exact do you need the count to be? If you can miss a handful of cycles, you could just log the count to a file on the SD card every once in a while.

Using the internal eeprom should work for your application. They are conservatively spec'd for 100000 R/W cycles to an address, so for 250000/10 you are comfortably within spec even if you do all your writes to the same two addresses required for the integer.

But you could use a wear leveling scheme and safely write every cycle. Because your data is monotonically increasing, then the method is very simple, because the data becomes its own index to its location.

Type your cycle data as unsigned long and you write it to 4 bytes in the 1024K eeprom. With each write, you will move this storage chunk progressively around in the eeprom to 256 possible locations.

There will be a data value in eeprom bytes 0:3, then the next at bytes 4:7, then the next in 8:11 etc. When you get to the end of the eeprom you start around again. When the power is on and the program is running the management of the next write location is straight forward.

When power is cycled, the program needs to find where the last written chunk was located, maybe use that value for something, and to start writing again from that location.

The way to do this is to start reading the eeprom from address 0 in 4 byte chunks and test each value against the next. When the value at an address is not bigger than the value in the previous address (remember to be incrementing addresses by 4), that is where to write the next value. You will need to start off with the eeprom at all 0's so that the comparison of each stored value to the next will work on the first pass. If you find that all 256 locations contain increasing data values, then your next write will be at address 0.

You will basically share the 250,000 writes across 256 locations with a little less than 1000 writes in each byte.

Your first task is to write your data 256 times, at sequential locations 4 bytes apart. Then practice reading what you have stored.

When you are comfortable writing the data to sequential locations and sequentially reading it out, you can move on to comparing each pair of sequential values. Test if a value is larger than the last one, and if it isn't you have found where the next write should be. The testing routine should be part of startup() in your final program.

I was just writing about an eeprom library I made last night. Maybe it is useful to you:

Reading and writing the eeprom is pretty straight forward. The library is mostly useful because it handles the edge states (initialization, corruption) by storing a checksum with the data.

The same site talks about MegunoLink, software we made and use quite a bit for developing and running test rigs. The interface panel in particular.

Thanks Paul. .

Hi Guys,

Many thanks for the replies, I didn't receive a notification that there were responses...

I have found what I was looking for really I think, the example is here:

I'm still getting to grips with programming and so learning about bit shifting and masking has been a daunting task, however I think I understand it a little better and will have a go at testing some variable longs using the example above as a guide.

Cattledog, I do like the idea of creating a wear levelling scheme, and it could help improve my accuracy of cycles by logging each count without excessively wearing the first 4 addresses of my EEPROM. I'll look to see if there are any examples of this in use.

Many thanks!

The standard EEPROM.h library included with the IDE now has the functions EEPROM.put and EEPROM.get which handle multi-byte variables. You may learn more using and understanding the function you referenced, but the standard library will give you extra tools for other situations.