For my ongoing clock project, I wanted to persistently store some data after turning off the Arduino. This should be easy since Arduino board has an on-board EEPROM. Unfortunately the Arduino standard EEPROM library only implements functionality for reading and writing a single bytes, which makes the EEPROM quite cumbersome to use.

This is why I wrote the EEPROMex library, an extension of the standard Arduino EEPROM library. It extends the functionality of the original Arduino EEPROM library with:

#include <EEPROMex.h>// Always get the EEPROM in the same order// This will ensure that each variable gets// the same adress after every restartint addressInt = EEPROM.getAddress(sizeof(int));int addressFloat = EEPROM.getAddress(sizeof(float));

double IntVarInput = 1234;double IntVarOutput = 0;

double FloatVarInput = 101.101;double FloatVarOutput = 0;

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

// Write an int to EEPROM, and then read it backEEPROM.writeInt(addressInt, IntVarInput);IntVarOutput = EEPROM.readInt(addressInt);Serial.print(IntVarOutput);Serial.println();

// Write a float to EEPROM, and then read it backEEPROM.writeFloat(addressFloat, FloatVarInput);FloatVarOutput = EEPROM.readDouble(addressFloat);Serial.print(FloatVarOutput);Serial.println();}void loop() {}

Let me know what you find!

[UPDATE: The previous version had some bugfixes. Please download the update version. The new version also comes with support for newer Arduino boards and Teensy boards!]

Before I start spending more time in this library than needed, some questions:

- Are many of you arduino aficionado's out there are using the EEPROM for storage? If you aren't using it, is that because you don't need it, or because of some lacking functionality?- Are there people using external SPI connected EEPROM (or even RAM)? Would it be useful to generalize the backend of this library so that it would also work for SPI connected storage?- Are there people using the very nice flash library by Mikal Hart (http://arduiniana.org/libraries/flash/)? Would it be useful to, in a similar manner, implement object oriented variables that can recall /store themselves from EEPROM?

Please let me know if you find bugs or have suggestions for future improvements.

Indeed! In fact, there are a lot of snippets and small libraries around that implement some form of EEPROM reading/writing to extend the standard library. The one you mention is probably one of the most general (and useful?), but there are also

And so on. There is no rocket science in this library, it is meant mostly for convenience. What I figured was lacking was a library that I could use as a drop-in replacement for the EEPROM library, that implemented both the basic types like, int, float, double with a syntax very similar to the standard lib, as well as more complex types like structs and arrays.

Still, this library introduces some functionality that I could not really find elsewhere:

- Updating only changed bytes, instead of blindly rewriting all of them. In practice I am often writing more or less the same data to EEPROM. Updating instead of writing then saves a lot EEPROM wear.

- A maximum on the number of bytes that you can write. In theory you could burn out a memory cell in a few minutes: a write/erase cycle takes approximately 4 ms, so writing 100.000 times to a single cell takes 6 1/2 min. It happened to me that I put an EEPROM write statement within a loop rather than outside, an only after a minute or so running noticed that my Arduino didn't seem to be doing anything useful. So far, it seems that my trusted Arduino survived .

- Basic Memory allocation functionality. It is basically just a counter of the last allocated byte. This is also implemented in newer AVR libraries, but not in the one that ships with the Arduino.

- Targeting single bits. I haven't needed it myself yet, but I found it as a suggestion somewhere, and figured it should be easy enough to add. The idea is that writing single bits can help you use the 1Kb that you have with maximum efficiency.

if this tests out, it would be a great improvement / addition to the standard lib.

I agree. I was fiddling with the builtin EEPROM library, making a way to store events and timestamps, and ran into the problem that timestamps wouldn't store into 1 byte. I eventually wound up packing them in a really odd way with a 12.3 second resolution or some such so I could fit them in 2 bytes--as I recall, the first byte was the week and the second byte was split into the day & time in some strange way. It didn't really work too well. A library like this would have been really helpful.

if this tests out, it would be a great improvement / addition to the standard lib.

Well, that would of course be nice :-) In fact, I would prefer that the Arduino came with a lot more user-created libraries by default (or would provide an easy way of getting to them, perhaps using a package manager), so people would start building on work of others rather that reinventing the wheel every time.

But for now, I would appreciate it very much if people would testdrive the library and see what is missing and if bugs pop up. I put the library on the Playground here, and am in the process of writing an library on top of EEPROMex that implements variables that can recall /store themselves from EEPROM.

@mdn15: Not without some real work. I have thought about this: the class could call different EEPROM writers/readers, which would make the underlying storage transparent to the user. However, I have not had many request for this (not many people seem to use external EEPROMs) so it is not high on my priority list.

What was higher on the list was to fix a small mistake in capitalization (a capital mistake?), which would give build errors on case-sensitive environments. This has now been fixed.

Great - I just stumbled across the limitations of the standard arduino EEPROM library, and this will be real helpful

Thanks for sharing!

tip: make a note about memory used if you include this library, this is always an issue on the 168/328p based boardstip: most RTC ds1302 modules sold on ebay include a i2c EEPROM - supporting external EEPROMs and in particular this module with a simple example would be a great idea. Most vendors don't even seem to know it is on their module (let alone the optional ds18b20!)

This is a great idea to fix the Arduino EEPROM library but why not use the excellent support already available in AVR?

The compiler supports marking a variable as stored in EEPROM with the attribute EEMEM. This allows symbolic handling of EEPROM addresses instead of the manual method forced by the Arduino EEPROM library. Also there are already all the needed functions for different data size access in AVR EEPROM, http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html