24LC256 EEPROM on Arduino

November 8, 2011

About: I picked up a few 24LC256 eeproms to get some more external memory for future projects. These eeproms hold around 32Kbytes which is more than enough for a basic data-logger or for storing specific values. You can hook up to 4 of these chips together to get a whopping total of 128Kbytes of external memory. These chips are great not only for the memory but also because they are I2C. Don’t let this scare you, there are many libraries for I2C eeprom chips. I2C is also great because it only uses 2 analog pins of your arduino. The I2C pins on the arduino are analog pin 4 and analog pin 5. For this tutorial I followed Hkhijhe example and his functions.

Objective: To connect a I2C eeprom to the Arduino Uno.

Instructions: For the breadboard schematic below for how to connect the 24LC256 to the Arduino.

24LC256 Connections
Pin Connection
1 GND
2 GND
3 GND
4 GND
5 Analog Pin 4
6 Analog Pin 5
7 GND
8 5V

Arduino Code: The following code will write a string to the eeprom and keep repeating it every 2 seconds to the serial port. Hold your mouse over the source code to copy it.

#include <Wire.h> //Include the Arduino I2C Library</pre>
<p style="text-align: left;">
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();
}

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;
}

void setup() {
char somedata[] = "dayzlab.wordpress.com"; //Data
Wire.begin(); //Start I2C connections
Serial.begin(9600);
i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM

delay(10); //add a small delay

Serial.println("Memory written");
}

void loop() {
int addr=0; //EEPROM Address 0
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);
}</p>
<p style="text-align: left;">

Leave a comment