Read Write to External EEPROM with Arduino

This is Arduino programming tutorial on how to read and write string text to an external EEPROM using I2C serial communication protocol. In this external EEPROM programming example we will be using microchip AT24C32A serial I2C EEPROM. We will be using two push button to write and read to the EEPROM device. When the write button is pressed a string text is written to the EEPROM and when the read button is pressed a string text is read from the EEPROM device and the content is displayed on the serial monitor.

External EEPROM with Arduino Hardware and Circuit Diagram

The following picture shows the AT24C32A EEPROM connected to Arduino Uno and the two push button on breadboard.

AT24C32A EEPROM interfacing with Arduino on breadboard

And the following is the circuit diagram of interfacing the external AT24C32A EEPROM with Arduino and the two push buttons.

AT24C32A EEPROM Arduino circuit diagram

The AT24C32A EEPROM has memory size of 32KB. It supports the I2C serial communication protocol. It has 8 pins as follows.

  • 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 address pins A0 to A3 can either to grounded or left floating. When left floating the internal pull down resistors will pull these pins low. The Write Protect pin should be grounded for write operation otherwise write operation will be prohibited and we cannot write to the EEPROM(Security feature). The SDA and SCL pins are the I2C signal lines. They are pulled high using the 10KOhm pull up resistors. They are connected to the Arduino SDA pin A4 and SDL pin A5 as shown in the above circuit diagram. Two push buttons for write and read are connected to Arduino pin 9 and 8 respectively as shown in the picture below.

buttons with EEPROM

Program Code for reading external EEPROM

The Arduino program code for reading the AT24C32A external serial EEPROM using the I2C communication protocol is below.

#include <Wire.h>


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

const int debounceDelay = 50; // 50 ms delay to wait until stable read

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(!debounce(writeBTN)){    
    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(!debounce(readBTN)){     
    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(unsigned 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;
}

boolean debounce(int pin){
  boolean state;
  boolean previousState;
  previousState = digitalRead(pin); // store switch state

  for(int counter=0; counter < debounceDelay; counter++){
  delay(5); // wait for 1 ms
  state = digitalRead(pin); // read the pin
    if( state != previousState){
    counter = 0; // reset the counter if the state changes
    previousState = state; // save the current state
    }
  }
  
  return state; // now return the stable state
}

In the above code, to use the I2C library we have included the wire.h library. Two alias names writeBTN and readBTN are set for the pin 9 and 8 for easier coding. The debounceDelay of 50ms is the time delay for stable contact bounce reading. This Arduino Debounce Code explains the debounce code in more details. We can created a constant variable called EEPROM_ADDR that is set to 0x50 because all the address pin A0 to A2 are grounded. The myMessage[50] is used to hold the string text which we want to write to the external EEPROM. And the MEM[50] is used hold the string text after retrieving the same written text from the EEPROM.

The I2C write and read operations are implemented using the writeEEPROM() and readEEPROM() functions. The I2C writeEEPROM() function takes as arguments the address where the data within the EEPROM is to be written and the data byte itself. The I2C readEEPROM() takes in the address only within the EEPROM from where the data is to be retrieved. The How to Read Write External EEPROM with Arduino tutorial explains in greater details how these I2C write and read functions work.

The following video demonstrates how to write and read operation with external AT24C32A EEPROM with Arduino.


Post a Comment

Previous Post Next Post