Using the AT24C256 EEPROM with the Raspberry Pi

Using the AT24C256 EEPROM with the Raspberry Pi

The AT24C256 is an I2C EEPROM (Electrically Erasable Programmable Read-Only Memory) that allows the Raspberry Pi to store and retrieve data even after power loss. This guide explains how to interface the AT24C256 with the Raspberry Pi, including wiring, enabling I2C, and reading/writing data using Python.


What You Will Need

  1. Raspberry Pi (any model with GPIO and I2C support, e.g., Pi 3, Pi 4)
  2. AT24C256 EEPROM Module
  3. Breadboard and Jumper Wires
  4. Python installed on the Raspberry Pi

Step 1: Wiring the AT24C256 to the Raspberry Pi

The AT24C256 communicates using the I2C protocol.

Connections

AT24C256 Pin Raspberry Pi Pin
VCC 3.3V (Pin 1)
GND Ground (Pin 6)
SDA SDA (Pin 3, GPIO2)
SCL SCL (Pin 5, GPIO3)
WP (Write Protect) GND (Disable write protection)

Step 2: Enable I2C on the Raspberry Pi

  1. Open the Raspberry Pi configuration tool:
    sudo raspi-config
    
  2. Navigate to Interface Options > I2C and enable it.
  3. Reboot the Raspberry Pi:
    sudo reboot
    

Step 3: Install Required Libraries

  1. Update your Raspberry Pi’s package list:
    sudo apt update && sudo apt upgrade -y
    
  2. Install the necessary I2C tools and Python libraries:
    sudo apt install -y i2c-tools python3-smbus
    
  3. Verify that the AT24C256 EEPROM is detected on the I2C bus:
    sudo i2cdetect -y 1
    
    The EEPROM should appear at 0x50 (or another address based on A0-A2 pin configurations).

Step 4: Reading and Writing Data to the AT24C256

The AT24C256 stores data in 256KB of memory, and each byte is accessed using its memory address.

Writing Data to EEPROM

import smbus2
import time

EEPROM_ADDRESS = 0x50  # Default I2C address of AT24C256
bus = smbus2.SMBus(1)  # Use I2C bus 1

def write_byte(address, data):
    bus.write_i2c_block_data(EEPROM_ADDRESS, address, [data])
    time.sleep(0.05)  # EEPROM needs time to write

# Write data (example: store 'A' at address 0x00)
write_byte(0x00, ord('A'))
print("Data written successfully!")

Reading Data from EEPROM

def read_byte(address):
    return bus.read_byte_data(EEPROM_ADDRESS, address)

# Read the stored byte at address 0x00
stored_value = read_byte(0x00)
print(f"Stored Data: {chr(stored_value)}")

Step 5: Applications of the AT24C256

  1. Data Logging: Store sensor data over time for offline access.
  2. Configuration Storage: Save system settings or calibration data.
  3. Secure Authentication: Store encrypted keys or authentication tokens.
  4. IoT Projects: Enable persistent storage for Internet of Things applications.

Troubleshooting

  1. EEPROM Not Detected:

    • Check wiring and confirm SDA/SCL connections.
    • Run sudo i2cdetect -y 1 to verify the EEPROM’s I2C address.
  2. Incorrect Read/Write Operations:

    • Ensure Write Protect (WP) is grounded to enable writing.
    • Introduce delays (time.sleep(0.05)) to allow EEPROM write operations to complete.
  3. I2C Communication Errors:

    • Check for conflicting I2C addresses on the bus.
    • Ensure smbus2 is properly installed using pip3 install smbus2.

Conclusion

The AT24C256 EEPROM is a simple yet powerful way to store data persistently on a Raspberry Pi. Whether for data logging, storing settings, or IoT applications, this guide provides the foundational knowledge needed to interact with EEPROM storage effectively. Experiment with reading/writing larger datasets to explore its full potential! 🚀

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.