I2C EEPROM read/write confirmation

i am using an I2C EEPROM to store data in a struct on a Nano 33 BLE.
Managed to make it work with the functions below.
Could you please point me in the right direction for creating a check function to see if the read/write process was successful like there is in the internal EEPROM library?


void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
  Wire.write((int)(eeaddresspage >> 8)); // MSB
  Wire.write((int)(eeaddresspage & 0xFF)); // LSB
  byte c;
  for ( c = 0; c < length; c++)

// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  Wire.write((int)(eeaddress >> 8)); // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.requestFrom(deviceaddress, length);
  int c = 0;
  for ( c = 0; c < length; c++ )
    if (Wire.available()) buffer[c] = Wire.read();

The obvious way would be to read the data back and compare it with what was written or do you want to do something else ?

thank you for the reply.

What troubles me is that if the writing process fails (for example due to battery low) then the data written may be corrupted and maybe the read process could also fail.

I would like to know if the write process has failed so to be able to maybe use a backp-dataset or something..

The only way that you can be sure that the data has been written correctly to the EEPROM is to read it back. Wire.write() returns the number of bytes written to the device but there is no guarantee that the data is actually stored on the device and that the values are correct

What would be the ideal solution if it existed ?

Something like having a confirmation that wire.endTransmission() actually happened meaning the data have been written

wire.endTransmission() returns a byte indicating the status of the transmission

  • 0:success
  • 1:data too long to fit in transmit buffer
  • 2:received NACK on transmit of address
  • 3:received NACK on transmit of data
  • 4:other error

But you will still not know that the data has actually been stored

This sounds like a solution!

The potential problem i would like to avoid is that the data are written on the EEPROM at very and of the sketch and will bel used at the start of the successive reset/power on

So if the writing process hasn't ended well the next instance of the sketch could fail.

If the data have not been written at all then the sketch will use the old one and it is not a big deal.

I wanted to avoid the sketch beeing stuck on trying to read corrupted data.

Note: Before using an I2C EEPROM i tried with a flash EEPROM emulator and around one time in 200/300 runs it would have problems getting stuck. So i wanted to make really sure the problem would not be able to manifest itself.