How to Use the AT24C256 EEPROM with Arduino

How to Use the AT24C256 EEPROM with Arduino

The AT24C256 is a 256-kilobit EEPROM that provides non-volatile storage, allowing data to persist even when the power is off. It communicates with a microcontroller via the I2C protocol, making it ideal for storing sensor data, configurations, or other information in Arduino projects. This tutorial will guide you through interfacing and using the AT24C256 with Arduino.


What You Will Need

  1. AT24C256 EEPROM Module or IC
  2. Arduino Board (e.g., Uno, Mega, Nano)
  3. Breadboard and Jumper Wires
  4. A computer with the Arduino IDE installed

Step 1: Understanding the AT24C256

The AT24C256 EEPROM has the following features:

  • Storage Capacity: 32 KB (256 Kbits = 32,768 bytes)
  • Communication Protocol: I2C
  • I2C Address: Configurable between 0x50 and 0x57 using A0, A1, and A2 pins
  • Operating Voltage: 2.5V to 5.5V

Pinout

Pin Function
A0, A1, A2 I2C Address Configuration
VCC Power Supply (5V)
GND Ground
SDA I2C Data Line
SCL I2C Clock Line
WP Write Protection (optional, connect to GND to enable write access)

Step 2: Wiring the AT24C256 to Arduino

Connect the AT24C256 to the Arduino as follows:

AT24C256 Pin Arduino Pin
VCC 5V
GND GND
SDA A4
SCL A5
WP GND

Note: If using a module with pull-up resistors, no additional resistors are needed. For standalone ICs, use 4.7kΩ pull-up resistors on the SDA and SCL lines.


Step 3: Install the Required Library

To simplify working with the AT24C256, install the "Wire" library (built into the Arduino IDE) and the "EEPROM_AT24C256" library.

Steps to Install EEPROM_AT24C256 Library:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Search for "AT24C256" and click Install.

Step 4: Upload the Code

Example: Writing and Reading Data

This code demonstrates how to write a string to the EEPROM and then read it back:

#include <Wire.h>
#include <AT24C256.h>

AT24C256 eeprom(0x50); // Initialize EEPROM with default I2C address

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("AT24C256 EEPROM Test");

  // Write data to EEPROM
  String dataToWrite = "Hello, EEPROM!";
  Serial.print("Writing to EEPROM: ");
  Serial.println(dataToWrite);
  for (int i = 0; i < dataToWrite.length(); i++) {
    eeprom.writeByte(i, dataToWrite[i]);
  }

  delay(1000); // Wait for data to be written

  // Read data from EEPROM
  Serial.println("Reading from EEPROM:");
  for (int i = 0; i < dataToWrite.length(); i++) {
    char c = eeprom.readByte(i);
    Serial.print(c);
  }
  Serial.println();
}

void loop() {
  // Nothing to do here
}

Step 5: Test the Setup

  1. Connect the Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port under the Tools menu.
  3. Upload the code by clicking Upload.
  4. Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to 9600.
  5. Observe the written and read data displayed in the Serial Monitor.

Optional: Erasing Data

To erase the EEPROM, write 0xFF to all memory locations:

for (int i = 0; i < 32768; i++) {
  eeprom.writeByte(i, 0xFF);
}

Applications of the AT24C256

  1. Storing sensor data logs
  2. Configuration settings for IoT devices
  3. Storing user preferences
  4. Building data-logging systems

Troubleshooting

  • No communication: Check the I2C wiring and ensure the correct I2C address is used.
  • Incorrect data read: Ensure the write process has completed before reading.
  • Write protection enabled: Verify the WP pin is connected to GND for write access.

Conclusion

You’ve successfully interfaced the AT24C256 EEPROM with Arduino, allowing you to store and retrieve non-volatile data in your projects. Experiment with its capabilities to create data-logging systems, configuration storage, or other memory-dependent applications!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.