Arduino boards are great for controlling and monitoring various electronic systems. However, the internal memory of the board is limited and therefore it may not be sufficient to store large amounts of data. This is where external EEPROM (Electrically Erasable Programmable Read-Only Memory) comes into play. In this tutorial, we will focus on the AT24C32A EEPROM, which is a 32Kb I2C-based memory device, and learn how to interface it with an Arduino board, read and write data to it.
Introduction to External EEPROM Memory
EEPROM is a type of non-volatile memory that can be programmed and reprogrammed electrically. It is often used to store small amounts of data that need to persist even when the power is turned off. Unlike flash memory, which can be erased in blocks, EEPROM can be erased one byte at a time. The AT24C32A is an I2C-based EEPROM memory that can store 32,768 bytes of data. The device operates at a voltage range of 2.5V to 5.5V and has an I2C address range of 0x50 to 0x57.
Internal External EEPROM with Arduino
The AT24C32A EEPROM has 8 pins and the connections between the EEPROM and the Arduino board are very simple. The connections are as follows:
- Vcc of the EEPROM to 5V of the Arduino board.
- GND of the EEPROM to GND of the Arduino board.
- SDA (Serial Data) of the EEPROM to A4 of the Arduino board.
- SCL (Serial Clock) of the EEPROM to A5 of the Arduino board.
The circuit diagram of connecting AT24C32A EEPROM with Arduino is shown below.
Arduino Code for reading and writing to external EEPROM
The following is Arduino program for reading and writing to the AT24C32A external EEPROM.
#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
}
The above Arduino sketch is to interacts Arduino with an external AT24C32A EEPROM using I2C communication. The program reads and writes to the EEPROM by using two buttons connected to pins 8 and 9 respectively.
The program includes the Wire.h library which provides the necessary functions for I2C communication.
The writeBTN and readBTN are constants that represent the digital pins to which the write and read buttons are connected. The debounceDelay constant is the amount of time, in milliseconds, to wait for a button state to become stable. The EEPROM_ADDR constant is the I2C address of the AT24C32A EEPROM. The myMessage and MEM arrays are used to store the data to be written to and read from the EEPROM respectively.
In the setup function, the serial communication is initiated, the Wire library is started and the pins to which the write and read buttons are connected are set as inputs with pull-up resistors enabled.
The loop function continuously checks the states of the buttons. If the write button is pressed, the writeEEPROM function is called to write the data in the myMessage array to the EEPROM. If the read button is pressed, the readEEPROM function is called to read the data from the EEPROM into the MEM array and then display the data on the serial monitor.
The writeEEPROM function writes a single byte of data to a specified address in the EEPROM. It starts a transmission to the EEPROM's address, writes the high byte and low byte of the address, and then writes the data. The transmission is then ended and a delay of 5ms is added to complete the write cycle.
The readEEPROM function reads a single byte of data from a specified address in the EEPROM. It starts a transmission to the EEPROM's address, writes the high byte and low byte of the address, and then ends the transmission. The function then requests 1 byte of data from the EEPROM and reads the data when it becomes available.
The debounce function is used to debounce the state of a button. It stores the initial state of the button and then repeatedly checks the state for a specified amount of time. If the state changes during this time, the counter is reset and the current state is saved. The function returns the stable state of the button.
Programming Techniques for efficient Read-Write Operations to External EEPROM
Page Writing: The AT24C32A EEPROM has a page write capability that allows up to 32 bytes of data to be written to the memory in a single write cycle. It is more efficient to write data in page sizes rather than writing one byte at a time.
Write-Protect Pin: The AT24C32A EEPROM has a write-protect pin that can be used to protect the data in the memory from being overwritten. This is useful when you want to store critical data that should not be modified.
I2C Address: The AT24C32A EEPROM has a 7-bit I2C address, which allows up to 128 devices to be connected on the same I2C bus. If multiple devices are used, it is important to ensure that each device has a unique I2C address.
Conclusion
In this tutorial, we learned how to interface, read and write data to the AT24C32A EEPROM with an Arduino board. This can be a useful tool for storing large amounts of data that need to persist even when the power is turned off. With the knowledge gained from this tutorial, you can now integrate external EEPROMs into your Arduino projects to store and manage data more efficiently.
If you are interested in memory usage then see the tutorial How to use SPIFFS with NodeMCU.

