PARTS LIBRARY :: I2C EEPROM

PARTS NEEDED

   1x Arduino

   1x I2C EEPROM

   4x Jumper Cable

 

LIBRARIES USED

   Wire

Method
This page shows you how to use an I2C EEPROM. EEPROMs are non-volatile memory, so data stays there after the power has gone off.

This covers a simple read and write routine using code from the Arduino.cc Playground page on I2C EEPROMs.

Circuit
Connect Pin 1 to +5v, pin 2 to GND, pin 3 to arduino analog pin 5 and pin 5 to arduino analog pin 4 (the usual I2C pins).
My camera ran out of battery so take a look here for a clear circuit layout. You can see manuelfloresv has connected the other four pins to ground but I found it worked without.

Code
The following is a very basic modification of the code found on the Arduino.cc Playground page on I2C EEPROMs.
It performs two writes to the EEPROM in Setup. The second time we use i2c_eeprom_write_page we change the start address to byte 23 so that the write starts at the next bit from the previous write which is 23 characters long. Addressing starts at zero, so address 23 refers to the 23rd byte.
When you have loaded the code to your arduino open the serial monitor to view the output.

  #include  //I2C library

  void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
    int rdata = data;
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.send(rdata);
    Wire.endTransmission();
  }

  // WARNING: address is a page address, 6-bit end will wrap around
  // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddresspage >> 8)); // MSB
    Wire.send((int)(eeaddresspage & 0xFF)); // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.send(data[c]);
    Wire.endTransmission();
  }

  byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.receive();
    return rdata;
  }

  // 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.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,length);
    int c = 0;
    for ( c = 0; c < length; c++ )
      if (Wire.available()) buffer[c] = Wire.receive();
  }

  void setup() 
  {
    char somedata[] = "EEPROM read/write test!"; // data to write
    char moredata[] = " Second EEPROM write test.";
    Wire.begin(); // initialise the connection
    Serial.begin(9600);
    i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM 
    delay(10); //add a small delay
    i2c_eeprom_write_page(0x50, 23, (byte *)moredata, sizeof(moredata)); // write to EEPROM 
    delay(10); //add a small delay

    Serial.println("Memory written");
  }

  void loop() 
  {
    int addr=0; //first address
    byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
    while (b!=0) 
    {
      Serial.print((char)b); //print content to serial port
      addr++; //increase address
      b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
    }
        
    Serial.println(" ");
    delay(2000);

  }


© Sam Rusling 2011