Writing/Reading to and from Eeprom I2C

Hi,

I'm working on a project (but I don't have the setup here at the time), so I'm wondering if I did this correctly - and hopefully one of the helpful people in this forum can guide me in the right direction if I'm wrong.

What I want to do, is to send data (a byte) to an Eeprom, and then I'd like to read from it again. This bit of code is using the Rom as more of a passthrough than a data storage unit. Meaning that what I send to it, should be read initially afterwards.

Can someone please tell me if Im on the right track here? :slight_smile: The thing I'm mostly questioning is the part where I read the data from the Eeprom.

void Eeprom(void) {
//Writing data to the Eeprom
  Wire.beginTransmission(eeprom);                 //Calling the Eeprom at the Eeprom address
  Wire.write(high);                               //Telling the Eeprom we want to access the High address
  Wire.write(low);                                //Telling the Eeprom we want to access the Low address
  Wire.write(byte(incomingtemp));                 //Writing the incoming temp data to Eeprom 
   //Serial.println(incomingtemp);

  Wire.endTransmission();                         //Stop transmitting to the Eeprom
  delay(500);                                     //Inserting a 500 ms delay

//Reading data from the Eeprom
  Wire.beginTransmission(eeprom);                 //Calling the Eeprom again
  Wire.write(high);                               //Telling the Eeprom we want to access the High address
  Wire.write(low);                                //Telling the Eeprom we want to access the Low address
  Wire.endTransmission();                         //Ending transmission with the Eeprom
  char readdata = Wire.requestFrom(eeprom, 1);    //Requesting one byte from the Eeprom address
  Serial.println(readdata);
  delay(500);                                     //Inserting a 500 ms delay
    shelf++;                                      //Count "shelf" one up
  if (shelf > 255) shelf = 0;                     //When "shelf" 255 is reached, reset "shelf"
                                                  //and start writing to/reading from address 0 again
  if (low == 255) high++;                         //If low address reaches 255, count high address 1 up
  low++;                                          //Count low address one up to prepare for next writing/reading
}//eeprom

requestFrom only tells the device how many bytes you want to read. To read the data, you have to follow this with

readdata = Wire.read();

You would probably find it easier to address the byte by using a 16-bit integer instead of two bytes.

uint16_t i2c_address = 0;
.
.
.
  Wire.write(i2c_address >> 8);
  Wire.write(i2c_address & 0xff);
.
.
.
  // incrementing the address is then trivial
  i2c_address++;
.
.

Pete

Thanks. :slight_smile:

I'll stick with the high and low though, at least for now.. I understand it a bit better that way.

However, in the read part of the code.. is this the correct way otherwise?

//Reading data from the Eeprom
  Wire.beginTransmission(eeprom);                 //Calling the Eeprom again
  Wire.write(high);                               //Telling the Eeprom we want to access the High address
  Wire.write(low);                                //Telling the Eeprom we want to access the Low address
 Wire.endTransmission();                         //Ending transmission with the Eeprom
  Wire.requestFrom(eeprom, 1);    //Requesting one byte from the Eeprom address
char readdata = Wire.read();  
Serial.println(readdata);
  delay(500);                                     //Inserting a 500 ms delay
    shelf++;                                      //Count "shelf" one up

I am particularly wondering about the first 4 lines of code, where I tell the Eeprom I want to access the high and low bytes - and then ending the transmission BEFORE actually requesting and reading my byte of data. Is this the correct way to do this? Cause it seems a bit weird and not very logic. Should the Wire.endTransmission not occur only after Wire.read?

What actually happens in this sequence:

  Wire.beginTransmission(eeprom);
  Wire.write(high);
  Wire.write(low);
 Wire.endTransmission();

is that beginTransmission initiates a write sequence to the device. This is followed by up to 32 Wire.write() (or whatever the buffer size is in the library). These writes do not occur immediately. The data is just stored in the library's buffer. The call to endTransmission then initiates the real write sequence to the device - in this case two bytes are written.
When you want to read from the device, you have to write the address to it first so you use the above sequence. Then you use requestFrom to specify how many bytes you want to read starting at that address. This must then be followed by a Wire.read() for each of the bytes to be read.

Pete