Help: Storage info into a i2c EEPROM

Hello friends, I´m starting to try to storage info into a i2c EEPROM (exactly the M24512, a 512kbit EEPROM that is 64kBytes capaty and 128Bytes per page).
Exactly what I want to do is to storage info like an analog read or a text string because I´m working on a mini-meteorological station that gets Lux info, Heat, Time and date but I don´t know how to save the info, I have this code:

#include <Wire.h>
int data;
byte LSB = 0;              //LSB bits
byte MSB = 0;            //MSB bits
int indice = 64;          //How many bytes I want to read
int contador = 0;       //Auxiliar counter
#define Addr 0x57

void setup() {
  Wire.begin();
  Serial.begin(9600);
  delay(100);
  for(int i = 0; i <= indice; i++){            //This loop runs over the LSB and write 1 byte in Hex
    Wire.beginTransmission(Addr);
    Wire.write(MSB);
    Wire.write(LSB);
    Wire.write(0x37); //Here I should put the data which I don´t know what kind must be...
    Wire.endTransmission();
    Serial.print(" ");
    LSB = (LSB++);        //Increase the LSB bit in order to keep writting
    delay(100);
  }
  LSB = 0;                       //Reset LSB for the next loop start from 0
  Serial.println("");
}

void loop() {
  for(; contador <= indice; contador++){      //Reading EEPROM loop from 0 to indice value
    Wire.beginTransmission(Addr);         // Start I2C transmission
  Wire.write(MSB);                      // Select data register [FF,ff]
  Wire.write(LSB);                      //                      [ff,FF]
  Wire.endTransmission();               // End I2C transmission
  Wire.requestFrom(Addr, 1);            // Request 1 byte of data
  if(Wire.available() == 1)             // Read 1 byte of data 
  {    data = Wire.read();  }
  Serial.print(data, HEX);
  Serial.print(" ");
  LSB = (LSB++); 
}
delay(20);

}

I´ve tryed doing Serial.print(analogRead(A0)); to see the data I´m storaging (in theory) and Wire.write(analogRead(A0)); but when I read the data back it doesn´t match... It only works fine when I do ...write(0xFF) ...(0x31) ...('a') or something else but allways only in Hex and ascii... please help, I´ve read and watched videos until my brain went kaboom and still without an idea of what I have to save what I need...
Thank you in advance! and please excuse my bad english.

Look into:

Adafruit I2C Non-Volatile FRAM Breakout - 256Kbit / 32KByte : ID 1895 : $9.95 : Adafruit Industries, Unique & fun DIY electronics and kits

and

Adafruit 24LC32 I2C EEPROM Breakout - 32Kbit / 4 KB - Stemma QT : ID 5146 : $3.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Code examples are provided.

1 Like

Thank you so much, I´m going to study it :slight_smile:

I think the best way is to put all the data you want to save into a struct.
Writing and reading the struct from eeprom can be either on a byte by byte basis or with a block write. Writing individual bytes will avoid page boundary issues.

1 Like

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