IOT

How To Interface an Arduino to an I2C Serial EEPROM IC AT24C256?

In this article, we’ll show you how to connect an Arduino to an I2C Serial EEPROM IC AT24C256. Before understanding how the Arduino AT24C256 EEPROM Interface operates, we will first learn a little bit about the AT24 Series EEPROMs, the pinout and pin description of the AT24C256, write a few functions to READ and WRITE data from/to the EEPROM, and finally see how the AT24 Series EEPROMs are organised.

Arduino-AT24C04-EEPROM-Connections-Pin-Diagram

Introduction

Electrically Erasable Programmable Read Only Memory, sometimes known as EEPROM, is a kind of non-volatile memory that keeps its contents even when the power is switched off.

Regarding the actual location, there are two sorts of EEPROM. The Internal EEPROM is built within the Microcontroller, but the External EEPROM is an IC that must be attached to a Microcontroller using a Serial Interface (like I2C or SPI).

In a previous lesson, I had covered how to access the Arduino’s internal EEPROM. Check out “ARDUINO EEPROM TUTORIAL” for additional details about EEPROM (and its history), volatile and non-volatile memory, and reading and writing to internal EEPROM of Arduino.

Internal EEPROMs are frequently small (typically a few kB) but extremely fast because they are built on the same silicon as the MCU’s processor. The ATmega328P MCU, the microcontroller on the Arduino UNO, has just 1 kB of EEPROM, as an example.

External EEPROM ICs are useful in this situation. You may simply interface with capacities up to 256 kB (or more) using either I2C or SPI.

A Brief Note of AT24C256

The Atmel (now Microchip) AT24 and AT25 series are particularly popular when it comes to Serial EEPROM ICs. While the AT25 Series uses SPI EEPROM ICs, the AT24 Series uses I2C EEPROMs.

I selected the 256 kbit EEPROM AT24C256 IC for this project. This is equivalent to 32,768 bytes or 2,62,144 bits (32 kB).

This 8-pin IC comes in a variety of packages, including 8-lead SOIC, 8-lead TSSOP, 8-pad UDFN, and 8-ball VFBGA. In Arduino modules, the 8-lead SOIC is frequently used.

Pin Diagram

The AT24C256 EEPROM IC’s pin diagram is displayed in the image below.

AT24C256-Pin-Diagram

Pin Description

Number Pin Function
1 A0 Address Input
2 A1 Address Input
3 A2 Address Input
4 GND Ground
5 SDA Serial Data
6 SCL Serial Clock
7 WP Write Protect
8 VCC Power Supply

Arduino AT24C256 EEPROM Interface

Let’s go on to connecting the Arduino UNO to the AT24C256 EEPROM IC. The AT24C256 (or any other AT24 Series EEPROM) talks over the I2C interface, which is the first thing to note. Therefore, for I2C communication, use Analog Pins A4 (SDA) and A5 (SCL).

Because it is an I2C communication, the SDA and SCL lines require pull-up resistors. I performed pull-ups using a few 10 k Resistors. The circuit diagram shows the rest of the connections, which are all quite straightforward.

The device address comes next. A graphic showing how to set the IC’s Device Address using the Address Input pins can be found in the datasheet.

AT24C256-Device-Address

For all Atmel I2C EEPROMs, the first four MSBs must be in the sequence “1010.” You can connect up to 8 devices to the same I2C Bus using the following three bits, A2, A1, and A0.

The three Address Input pins A2, A1, and A0 are connected to ground since we are only utilising one AT24C256 IC, which results in their corresponding values in the Device Address being “000”.

The Read or Write operation selection bit is located in the final bit (LSB). The Read operation is chosen if this bit is set to “1,” while the Write operation is chosen if it is set to “0.”

NOTE: Depending on the operation, the Arduino Wire (and internal TwoWire) library takes care of the LSB of the device address, so we may ignore it while writing the code. We can therefore define the address to be “0x50.”

NOTE: During shipment, my AT24C256 EEPROM IC was broken. For the demonstration, I’ll be utilising an outdated AT24C04 IC with a 4 kb (4096 bits or 512 B) storage capacity. Due to the fact that both EEPROMs are I2C devices with identical pin configurations, the circuit remains unchanged.

AT24C04-EEPROM-IC

Components Required

  • Arduino UNO
  • AT24C256 EEPROM IC
  • 2 x 10 kΩ Resistor
  • Connecting Wires
  • Breadboard
  • Breadboard Power Supply (optional)

Circuit Diagram

The circuit diagram for the Arduino AT24C256 EEPROM Interface may be seen in the image below.

Code

You simply need to download the “Wire” library because we will only need it for developing the code (which comes with Arduino IDE). I made the decision to develop two functions utilising the Wire library for the Read and Write operations of the AT24C256 rather than using a specific library.

Write a Byte to EEPROM

Arduino-AT24C256-EEPROM-Write

Read a Byte from EEPROM

Arduino-AT24C256-EEPROM-Read

Code

  #include <Wire.h>
   
  #define EEPROM_I2C_ADDRESS_0 0x50
  #define EEPROM_I2C_ADDRESS_1 0x51
   
  int EEPROM_I2C_ADDRESS = NULL;
  int i=0;
  void setup()
  {
  Wire.begin();
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  }
   
  void loop()
  {
   
  for(i=0;i<94;i++)
  {
    if(i<256)
    {
      EEPROM_I2C_ADDRESS = 0x50;
      writeAT24(i, 33+i);
    }
    else
    {
      EEPROM_I2C_ADDRESS = 0x51;
      writeAT24(i-256, 0);
    }
  }
  delay(1000);
  Serial.println(“Data from EEPROM”);
  Serial.println(“—————–“);
  for(i=0;i<94;i++)
  {
    if(i<256)
    {
      EEPROM_I2C_ADDRESS = 0x50;
      Serial.println((char)readAT24(i));
    }
    else
    {
      EEPROM_I2C_ADDRESS = 0x51;
      Serial.println((char)readAT24(i-256));
    }
    
  }
  delay(1000);
  while(1)
  {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
  }
   
  }
   
   
  // Function to write to EEPROOM
  void writeAT24(byte dataAddress, byte dataVal)
  {
  Wire.beginTransmission(EEPROM_I2C_ADDRESS);
   
  Wire.write(dataAddress);
  Wire.write(dataVal);
  Wire.endTransmission();
   
  delay(5);
  }
   
  // Function to read from EEPROM
  byte readAT24(byte dataAddress)
  {
  byte readData = NULL;
  Wire.beginTransmission(EEPROM_I2C_ADDRESS);
  Wire.write(dataAddress);
  Wire.endTransmission();
   
  delay(5);
  Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
  //delay(1);
   
  if(Wire.available())
  {
    readData =  Wire.read();
  }
   
  return readData;
  }

I created a little piece of code that uses the write function to save ASCII values from 33 (‘!’) to 126 (“) in order to show how the Arduino and external EEPROM IC Interface interact.

I read them using the read function after storing them in the EEPROM and printed the characters on Serial Monitor.

Arduino-AT24C256-Read-Serial

ConclusionI hope all of you had understand how to interface AT24C256 EEPROM IC with Arduino. We Matha Electronics will be back soon with more informative blogs soon.

Leave a Reply

Your email address will not be published. Required fields are marked *