Can i use wire library for storing the structure

Hello Everyone,

I am just starting building a project with External EEPROM. can I use the wire library to store the structure
can anyone give advice on this topic?

You need to provide a lot more detail about what you want to do. Structure of what, for example.

You should also provide a link to the datasheet for the external EEPROM.


what is the part number for the EEPROM?

I am just starting building a project with External EEPROM. can I use the wire library to store the structure. Can anyone give advice on this topic?

Yes you can do this.

As Robin2 says, much depends on the struct you want to store. If the struct is large, the wire library has a standard 32 byte buffer, and there are page boundary issues on the external eeprom. Writing to an external eeprom with i2c is not as simple as using the .put() and get() methods with the internal eeprom.

If you forgo some speed and store your data a byte at a time, you will be OK whatever you do. If you want to use more efficient block writes you will need to handle the page boundary and max write size issues.

A library which handles both of these would be helpful. I've used J Christensen's
extEEPROM.h which is available through the library manager. It will work with many of the external eeprom , but not all of them.

As Robin2 says, much depends on the struct you want to store.

I was unwilling to assume that the OP was referring to a C++ struct. Even if he was it is unclear whether he wants to store the definition of the struct, or its contents.


struct MYDATA { // tranmission data
 String ID;
 String PSD;
} ;

That struct only contains information about the String (length, capacity, and pointer to the actual data). It's always 6 bytes long (on an AVR). So, that's all that's written when you do this:

i2c_eeprom_write_page(0x50, eAdd + 2 , (byte*) pdata, sie);

The actual characters are in dynamically allocated memory locations.


The following sketch may be helpful to you. It writes the contents of data.ID = "User_ID" into external EEPROM (I tested on 24C512), reads back and shows on Serial Monitor. (I could not succeed in writing the whole structure in ONE GO onto the EEPROM.)

unsigned int  eAdd = 10; // address
int i = 0;

struct MYDATA
  String ID;
  String PSD;
} ;

MYDATA data;

void setup()

  data.ID = "User_ID";  //intialization
  data.PSD = "123456";
  Wire.beginTransmission(0x50); //roll calling EEPROM
  Wire.write(highByte(eAdd));    //pointing at location address 10
  Wire.write(lowByte(eAdd));     //and then auto increment
    Wire.write(data.ID[i]);       //putting data in Wire buffer
  while (data.ID[i] != '\0');     //keep putting in buffer until null-byte is found
  Wire.endTransmission();         //placing the data bytes into EEPROM
  delay(5);                        //wait for write delay
  eAdd = 10;
  Wire.beginTransmission(0x50);   //repositon the EEPROM beginning address

  byte m = Wire.requestFrom(0x50, i);
  for (int j = 0; j < m; j++)
    Serial.print((char);//read data from EEPROM and show on Monitor

void loop()


As @gfvalvo says, you can not save a String object into the eeprom. The String object is a pointer to some dynamically allocated memory, and not the actual characters of the message.

You would be better off using null terminated character arrays for your ID and PSD.

If you attempt to store a String Object, but do not cycle power, the code may appear to work, as the String Object is still in memory to be pointed at by the retreived pointer from the eeprom.

Always cycle power when testing eeprom code.

You may also want to consider the SparkFun external eeprom library which is available through the library manager. It allows the direct use of .put() and .get(). for simplified handling of the struct. It deals with both page boundaries and wire library buffer size.

Thanks to all, I really appreciate that.
but I have questioned if I store struct byte by byte then I have also take care about page size or not. can I store data to all 64 bytes of a single page in EEPROM?

The page size of your 24LC256 EEPROM is 64-byte. That means that you can fuse these 64-byte data into the EEPROM in ONE GO within 5 ms time (typical write delay) rather than spending 5x64 ms in byte-by-byte process.

But the buffer size of the I2C Bus is 32-byte. Therefore, you have to make two write operations into EEPROM with block size of 32-byte or you have to change the I2C buffer size to 64 (if allowable) by editing the Wire.h Library.

Golam Mostafa thanks I change the EEPROM buffer size in the header file of wire.

Golam Mostafa thanks I change the EEPROM buffer size in the header file of wire.

Is it working? Can you perform write operation in ONE GO with page size 64-byte? Have you verified the written data by reading it back?

yes, I change buffer size and store the data and read it perfectly (for only one user id and password Write and Read).
but when I store another userID and PSD to next address the previous and current data is corrupt I don't why this happen.
is it necessary to empty structure element every time to read or write operation?

Write cycle delay of 5 ms (delay(5)) should come after the Wire.endTransmission(); command.

On that device the page size is 64. The page boundaries are at fixed addresses--64,128,192, etc.

You start at address 60 are trying to block write over a page boundary which is located at address 64. You only store "USER" before the data wrap.

@cattledog @GolamMostafa Thanks to both of you for point of view.
Now I write and read the data properly. such a great community for beginners to help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.