Arduino Nano I2C EEPROM programming tutorial

 In this Arduino Nano EEPROM programming tutorial, it is shown how to write to external EEPROM and read from external EEPROM using the I2C protocol. The EEPROM IC used here is the microchip serial I2C 24LC256 EEPROM. Arduino Nano has internal EEPROM but its memory size is only 512 bytes therefore external EEPROM can be useful when using Arduino Nano.

Arduino Nano I2C interfacing with EEPROM hardware and circuit diagram

In this Arduino nano EEPROM example, we will use two push buttons to write and read to the 24LC256 I2C EEPROM device. If the write push button is pressed a string text is written to the EEPROM and if the read push button is pressed the data saved is read from the EEPROM device and the content is displayed on the serial monitor.

The following shows Arduino Nano 24LC256 I2C EEPROM interfacing circuit diagram along with  pull-up resistors for the EEPROM I2C pins(SDA, SCL) and the two push buttons.

Arduino Nano I2C 24LC256 EEPROM interfacing circuit diagram

The Microchip 24LC256 in 8 pin DIP package is shown below.

24LC256 I2C EEPROM with pinout diagram

The 24LC256 EEPROM has a memory size of 256KB. It supports the I2C interface for serial data communication.  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 operations 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. It should be pulled up high using a pull-up resistor.
  • SCL (pin 6) – The Serial Clock for the I2C connection. It should be pulled up high using a pull-up resistor.

The address pins A0 to A3 are used to set up the I2C EEPROM address. One can either connect them to the ground or left floating. If they are left floating then 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. The SDA and SCL pins are the I2C signal lines. They are pulled high using the 10KOhm resistors. They are connected to the SDA SCL Arduino Nano pins A4 and A5 respectively as shown in the above interfacing circuit diagram. The two push buttons for write and read are connected to Arduino pins 9 and 8 respectively as shown in the picture below.

buttons with EEPROM

Arduino Nano Program for Write/Read I2C EEPROM

The following is Arduino Nano program code for writing and reading the 24LC256 serial I2C EEPROM using the I2C communication protocol.

#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(1); // 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 Aruino Nano I2C EEPROM program code, we have included the wire.h library to use the I2C protocol. We have then set alias names writeBTN and readBTN for the two push buttons connected to pins 9 and 8 respectively. A debounceDelay variable of 50ms is used for stable contact bounce reading. This Arduino Debounce Code explains how the debounce code for the push buttons works in detail. The constant variable called EEPROM_ADDR holds the I2C EEPROM address which is 0x50 when all the address pins A0, A1, and A2 are grounded. The myMessage[50] array variable is created to hold the string text which we want to write to the external EEPROM. The MEM[50] array variable is used to hold the string text retrieved from the EEPROM in the read operation.

The I2C EEPROM write and EEPROM read operations are implemented with the writeEEPROM() and readEEPROM() functions. The writeEEPROM() function requires (1)the memory address where the data is to be written and (2)the data byte of the text. The I2C readEEPROM() function requires an address from where the data is to be read. The read-write EEPROM with Arduino Uno tutorial explains in greater detail how these I2C write and I2C read functions work.

The video below demonstrates how to write to EEPROM and read from EEPROM with push button press operation works with the external 24LC256 EEPROM and Arduino Nano.


 

So in this Arduino Nano I2C EEPROM programming tutorial, we showed how to interface Arduino Nano with a 24LC256 I2C EEPROM device and how to write a program to write the EEPROM and read the EEPROM using two push buttons.

I2C protocol is common nowadays in interfacing microcontrollers or MCU board like Arduino Nano with sensors and devices that supports the I2C interface. Like EEPROM used here, we can interface I2C LCD as illustrated in the tutorial Arduino Nano I2C LCD Interfacing & Programming or save sensor data into EEPROM as illustrated in the tutorial write read sensor data to I2C EEPROM. I2C EEPROM is useful if the microcontroller that you using has limited permanent memory like Arduino Nano. But it is also useful in order kinds of electronics projects like in IoT projects where you need more space although you have a moderate amount of EEPROM memory. An example is NodeMCU which is used in IoT projects but which has already 4KB of EEPROM. In IoT projects where data logging is required higher EEPROM size may be needed and in such cases we can use I2C EEPROM as illustrated in the tutorial LCD I2C NodeMCU.

Post a Comment

Previous Post Next Post