EEPROM not reliable?

Hi Arduinoheads,

I've been using the EEPROM lately to store data that needs to be persistent. However, sometimes this data needs to be changed.
For some reason, whenever I try to read newly stored data from the EEPROM, the board returns data that I never inputted! This data looks corrupted, e.g. whenever i want to store a string like this: "abcdef", it returns "R: ''"

Any idea what might be the problem?


I use this routine to store the string

// Declared Variable at the top of the sketch
char thisString[7] = "abc123";

// Somewhere down in the sketch
void storeString(){

eeprom_write_block( (void *)thisString, (void *)20, 7);


Is it perhaps a wrong declaration of the char array size? :-/

What library are you using?

I use a standard EEPROM library, as suggested somewhere else on the forum

It's this line

#include <avr/eeprom.h>

So, the write looks OK. What does your call to read the data look like?

eeprom_read_block( (void *)thisString, (void *)20, 7);

Do you perhaps know more about strings (char arrays) ?

E.g.: thisString[7] = "abc123"

What will thisString[0] return?
And what about thisString[3]?

A string is a NULL terminated array of characters. So, in your example, thisString[0] is an 'a' and thisString[3] is a '1'.

For the write/read of EEPROM issue, I'd use a variable to hold the location value, instead of a constant.

If thisString[0] is an 'a' and thisString[3] is a '1', then I'd assume I would have 8 positions in the char array (so: 7 chars + '\0')
However, and this is why I ask, when I initialize thisString like this:

thisString[6] = "abc123"

The compiler says that there is not enough space to store "abc123" in it. That contradicts with the postulate of having position 0 as a valid place for a char, doesn't it? :-?

The value you provide for the array size is the SIZE of the array, not the maximum position.
If you have "char thisString[6]", it contains six positions indexed from thisString[0] to thisString[5]

The following sketch seems to work as I'd expect it to.

void loop()
  eeprom_write_block(mystring, (void *)20, sizeof(mystring));
  Serial.print("Wrote: ");
  eeprom_read_block(mystring, (void *)20, sizeof(mystring));
  Serial.print("Read: ");
  eeprom_write_block("erase", (void *)20, sizeof(mystring));
  Serial.print("Wrote: ");
  eeprom_read_block(mystring, (void *)20, sizeof(mystring));
  Serial.print("Read: ");

Perhaps something else is wrong with your sketch; using too much RAM, perhaps?