Read/Write Serial EEPROM via I2C
Simple example, how to read and write EEPROM 24C01 via I2C with Arduino Uno.
EEPROM
If you do not know, what EEPROM is, see wikipedia page. For more info about used EEPROM AT24C01A, see attached datasheet.
I2C
Inter-Integrated Circuit is serial synchronous bus. It uses two wires and Arduino Uno has this interface at analog pins A4 and A5. Library for I2C in Arduino is called Wire. More info at wikipedia page.
Code
Code for Arduino is based on code from page Using Arduino with an I2C EEPROM, with some differences:
Code at playground use AT24C256 EEPROM with 256kbit. This EEPROM use 2bits for memory addressing, but AT24C01 use only one bit.
#include <Wire.h>
void eeprom_i2c_write(byte address, byte from_addr, byte data) {
Wire.beginTransmission(address);
Wire.send(from_addr);
Wire.send(data);
Wire.endTransmission();
}
byte eeprom_i2c_read(int address, int from_addr) {
Wire.beginTransmission(address);
Wire.send(from_addr);
Wire.endTransmission();
Wire.requestFrom(address, 1);
if(Wire.available())
return Wire.receive();
else
return 0xFF;
}
void setup() {
Wire.begin();
Serial.begin(9600);
for(int i = 0; i < 10; i++) {
eeprom_i2c_write(B01010000, i, 'a'+i);
delay(100);
}
Serial.println("Writen to memory!");
}
void loop() {
for(int i = 0; i < 10; i++) {
byte r = eeprom_i2c_read(B01010000, i);
Serial.print(i);
Serial.print(" - ");
Serial.print(r);
Serial.print("\n");
delay(1000);
}
}