Runtime determination of EEPROM size

This is probably a stupid question, but I'd like to be able to determine the size of EEPROM at runtime. I know it's 512, 1024, or 4096 depending upon the board, but is there some way to find this out at runtime?

econjack:
This is probably a stupid question, but I'd like to be able to determine the size of EEPROM at runtime. I know it's 512, 1024, or 4096 depending upon the board, but is there some way to find this out at runtime?

I don't believe there is a simple way to determine that at run-time, however there should be a way to check that at compile time, as when you select the board type before verifying or uploading the processor type is known and can be tested to come up with the proper size. What you could do at run time is to write a known value into address 0 of the eeprom and then write a different value into address 1024 and then read adress 0 changed, if not then write the different value into address 4096 and check again if address 0 changed or not. Writing or reading to a eeprom address larger then it's actual size will just cause the address to 'wrap around' as if the higher address bits did not exist.

Hope that helps;

Lefty

I tried that, but a different way (knowing EEPROM kinda works like a ring buffer). I tried it again, with a slightly different twist, and tested it with the two boards I have and the following code seems to work. I'd like to hear from others with different boards if it works for them.

/*****
Purpose: Find out how much EEPROM this board has. If you wish
to see print statement, set

#define DEBUG 1

at top of program. To remove them, place comment marks
in front of #define

Parameter list:
void

Return value:
int the EEPROM size

Free to use: econjack
*****/
int FindEepromTop()
{
byte b;
int i;
const int MAGICNUMBER = 55;
const int BASENUMBER = 33;

EEPROM.write(0, BASENUMBER);
EEPROM.write(512, MAGICNUMBER);
b = EEPROM.read(0);
if (b != MAGICNUMBER) {
#ifdef DEBUG
Serial.println("EEPROM > 512");
#endif
} else {
return 512;
}
EEPROM.write(0, BASENUMBER);
EEPROM.write(1024, MAGICNUMBER);
b = EEPROM.read(0);
if (b != MAGICNUMBER) {

#ifdef DEBUG
Serial.println("EEPROM > 1024");
#endif
} else {
return 1024;
}
#ifdef DEBUG
Serial.println("EEPROM = 4096");
#endif
return 4096;
}

Sure there are several methods that could be used, but again a few simple preprocessor if/else tests in your source code will resolve what processor type is being used and therefore eliminate unneeded write/read cycles which take time and count towards the finite read/write cycles the eeprom has available. I say it's better to learn the size at compile time not run-time.

Lefty

The symbol E2END is the last EEPROM address.

For a 328 Arduino this sketch

void setup() {
  Serial.begin(9600);
  Serial.println(E2END);
}
void loop() {}

Prints this

1023

See this <avr/io.h>: AVR device-specific IO definitions

1 Like

You can use :

EEPROM.length()