First off, there are already a whole bunch of tutorials on how to use this particular I2C EEPROM with an Arduino. If you want to understand how such an EEPROM works, this post is not for you.

I just wanted to share this bit of code with anyone who doesn’t feel like writing their own. It basically allows you to select an EEPROM address and specify a payload to be written to that particular address. I only needed access to the first 100 or so bytes, so I didn’t bother implementing the full 16 bit address word functionality into the section of code that handles serial communication. You should be able to add that in without too much trouble though, since both readEEPROM() and writeEEPROM() fully support 16 bit addresses. (change the int address to unsigned int address if you are using larger EEPROMs and need addresses larger than 32767).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <Wire.h>
#define EEPROM_ADDR 0x57
#define WAIT_FOR_SERIAL(x) while(Serial.available()<x)
 
void writeEEPROM(uint8_t _data, int address) {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write(address >> 8);
  Wire.write(address & 0xff);
  Wire.write(_data);
  delay(10); //chuck in a tWR
  Wire.endTransmission();
  return;
}
 
uint8_t readEEPROM(int address) {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write(address >> 8);
  Wire.write(address & 0xff);
  Wire.endTransmission();
 
  Wire.requestFrom(EEPROM_ADDR, 1);
  return (Wire.read());
}
 
 
 
void setup() {
  Serial.begin(9600);
 
  Wire.begin();
}
 
void loop() {
   
  while(Serial.available() > 0){
    int addr = Serial.read();
    Serial.println("Insert Data");
    WAIT_FOR_SERIAL(1); //wait for payload
    uint8_t payload = Serial.read();
    Serial.println(payload);
    writeEEPROM(payload, addr);
    delay(100);
    uint8_t readback = readEEPROM(addr);
    Serial.println(readback);
    //wait for input
  }
   
 
}