How to Read Write External EEPROM with arduino

Different Arduino board have different size of internal EEPROM memory where you can save data for permanent storage. Arduino UNO has 1KB of internal EEPROM but if you want to store more data then you can use external EEPROM device. Some popular EEPROM device are the microchip 24LC series of serial I2C EEPROM devices. In this tutorial we will show how to interface 24LC256 EEPROM with Arduino UNO and how to write and read string into the serial EEPROM device.

If you don't know-  EEPROM, is abbreviation of Electrically Erasable Programmable Read-Only Memory.

The next tutorial Read Write to External EEPROM with Arduino demonstrates the actual implementation of this tutorial on breadboard.

Interfacing Arduino with 24LC256 EEPROM

The following shows the 24LC256 external EEPROM connected to the Arduino Uno and the two push buttons.

24LC256 interfacing with Arduino on breadboard

The following shows how to interface Arduino with the 24LC256 I2C EEPROM. 

Interfacing Arduino with 24LC256 EEPROM

The 24LC256 EEPROM is interfaced to the Arduino using the I2C connection. I2C(Inter-Integrated Circuit) is technology that enables microprocessor or microcontroller like Arduino to communicate with peripheral devices and sensors integrated circuit chip(IC chips). Another communication technology to communicate with peripheral devices is SPI(Serial Peripheral Inteface) technology. SPI is faster than I2C but requires three signal wires while I2C protocol requires only two signal wires. 

The 24LC256 EEPROM has 256KB memory. It has the following pins:

  • A0-A2 (pins 1-3) – These pins determine the I2C address of the chip.
  • WP (pin 7) – This is Write Protect. If this pin is connected to Vcc we cannot perform write operation to the EEPROM and if grounded then we can perform both read and write operation.
  • SDA (pin 5) – The Serial Data for the I2C connection.
  • SCL (pin 6) – The Serial Clock for the I2C connection.

The A0-A2 which is used for addressing are all grounded in the example above. The combination of  A0,A1 and A2 determines its address as follows.

EEPROM address A0,A1,A2
Since in our case, the address lines A0 to A2 are all grounded the EEPROM address is 0x50 which will be required later in the program code.

The main signal lines in I2C communication is the SDA and SCL which stands for serial data and serial clock respectively. These are connected to Arduino SDA analog pin A4 and SCL analog pin A5 respectively and should be pulled high with 10KOhm resistors as shown in the above EEPROM-Arduino interfacing circuit diagram. The WP(Write Protect) pin is for write protection which when grounded allows us to read and write to the EEPROM and if it is pulled high to Vcc then we cannot write the EEPROM device. Here we have grounded this WP pin.

Also in the circuit diagram above we have connected two push button at pin 9 and 8. These push button are connected to use them to control read and write operation. When the push button connected to pin 9 is pressed(gets low) then write operation to the 24LC256 EEPROM is performed and when the push button connected to pin 8 is press then read operation is performed. So these push button are merely to control and illustrate the read and write operation.

Read Write Code for External EEPROM

In this external EEPROM read write tutorial with Arduino we will write a string or text into the EEPROM when the push button connected to pin 9 is pressed and read back the string when the push button connected to pin 8 is pressed. The code for doing this is below.


#include <Wire.h>

const int writeBTN = 9;
const int readBTN = 8;

const byte EEPROM_ADDR = 0x50; // I2C address for 24LC128 EEPROM

char myMessage[50] = {"This text was saved in EEPROM memory."};
char MEM[50];


void setup() {
	Serial.begin(9600);
	Wire.begin();

	pinMode(writeBTN, INPUT_PULLUP);
	pinMode(readBTN, INPUT_PULLUP);
}

void loop() {

	if(digitalRead(writeBTN) == 0){
		delay(100);		//simple debounce delay
		Serial.println();
		Serial.println("Writing to Ext.EEPROM...");

		for(unsigned int i=0; i < sizeof(myMessage); i++){
			writeEEPROM(i, myMessage[i]);
		}
		Serial.println("Write Complete");
	}

	if(digitalRead(readBTN) == 0){
        delay(100);		//simple debounce delay
		Serial.println();
		Serial.println("Reading Ext.EEPROM...");
		for (unsigned int j = 0; j < sizeof(MEM); j++){
			MEM[j] = readEEPROM(j);
		}
		Serial.println("Read Complete");
		Serial.println("EEPROM Content:");
		Serial.println(MEM);
	}	 
}


void writeEEPROM(int address, byte data){
	Wire.beginTransmission(EEPROM_ADDR);
	Wire.write((int)highByte(address));
	Wire.write((int)lowByte(address));
	Wire.write(data);
	Wire.endTransmission();
	delay(5); // wait to complete the write cycle
}


byte readEEPROM(unsigned int address){
	byte data;
	Wire.beginTransmission(EEPROM_ADDR);
	Wire.write((int)highByte(address));
	Wire.write((int)lowByte(address));
	Wire.endTransmission();
	Wire.requestFrom(EEPROM_ADDR,(byte)1);
	while(Wire.available() == 0);  // wait for data
	data = Wire.read();
	return data;
}

To use the I2C protocols, the wire.h library is used. We have used the writeBTN and readBTN to as alias name for the pins 9 and 8 respectively for purpose of easier coding. The constant variable EEPROM_ADDR is 0x50 which is the address of the 24LC256 EEPROM used as slave here. Arduino is the master. Then we have two array variable myMessage[50] which contains the string text "This text was saved in EEPROM memory." which we will write into the EEPROM and MEM[50] which will hold the text retrieved from the EEPROM.

In the setup() function we initiate the wire protocol by writing Wire.begin() and serial with Serial.begin(). Then we have setup the pin mode of the two push button.

In the loop() function we read the state of the write button and if it is low then we write the string text into the EEPROM. The writeEEPROM() function is called to write byte by byte the string stored in the myMessage[50]  array to the EEPROM. Similarly we monitor and read the state of the read button and if it low we read the content of the EEPROM byte by byte using the readEEPROM() function and store it in the MEM[] array variable. Then the content of the MEM[] variable is printed on the serial monitor. After each push button press there is 100ms delay for debounce but if you need better code for debounce see the Arduino debounce code tutorial.

The writeEEPROM() and readEEPROM() functions implements the I2C protocol. The Wire.beginTransmission(), Wire.write(), Wire.read(), Wire.available(), Wire.endTransmission() are inbuilt function in the Wire.h file. These are used to implement the I2C read and write to the EEPROM.

The following video demonstrates how the read and write operation to external 24LC256 EEPROM with Arduino works with the above program code.

So in this external serial EEPROM programming tutorial, we showed how to read and write to the EEPROM device like 24LC256. External EEPROM devices are used to store permanent data which are important in electronics devices such as test instruments to store calibration data, telecom electronics equipments to store communication data, to save password and credentials in automobile electronics equipments, arduino uno keypad to to store password, etc.

Post a Comment

Previous Post Next Post