How to Interface EEPROM with Arduino Using SPI

How to Interface EEPROM with Arduino Using SPI

In this beginner-friendly tutorial, you'll learn how to communicate with an EEPROM (25AA160A) using SPI (Serial Peripheral Interface) on an Arduino Uno. We’ll cover hardware requirements, EEPROM pin configuration, and demonstrate writing and reading data using SPI commands. This project is ideal for learning about non-volatile memory in embedded systems.

Recommended Microcontroller Boards (Amazon)

Project Overview: Arduino and EEPROM via SPI

This tutorial demonstrates SPI-based communication between an Arduino Uno and a 25AA160A EEPROM. Using the SPI.h library, you’ll write and read string data stored in non-volatile memory.

Components Required

  • Arduino Uno
  • 25AA160A SPI EEPROM
  • Breadboard and jumper wires
  • SPI communication enabled

Project Features

  • Write string data to the EEPROM
  • Read data back to verify write success
  • Display EEPROM data in the Serial Monitor

EEPROM Pin Explanation

Write Protect (WP): Prevents accidental writes. Connect to GND to allow writing, VCC to block it.

Hold (HOLD): Pauses SPI communication. Connect to GND to pause, VCC for normal operation.

EEPROM to Arduino SPI Pin Mapping

EEPROM Pin Arduino Uno Pin Description
CS Pin 10 Chip Select (Active Low)
SCK Pin 13 Serial Clock
SI Pin 11 Serial Input (MOSI)
SO Pin 12 Serial Output (MISO)

Useful EEPROM SPI Commands

  • #define WRITE_ENABLE 0x06 – Enables write operations.
  • #define WRITE_DISABLE 0x04 – Disables write operations.
  • #define READ_DATA 0x03 – Reads data from EEPROM.
  • #define WRITE_DATA 0x02 – Writes data to EEPROM.
  • #define RDSR 0x05 – Reads the status register.
  • #define WRSR 0x01 – Writes to the status register.

Arduino Code: EEPROM SPI Interface

#include <SPI.h>

const int CS_PIN = 10;

#define WRITE_ENABLE 0x06
#define WRITE_DISABLE 0x04
#define READ_DATA 0x03
#define WRITE_DATA 0x02
#define RDSR 0x05
#define WRSR 0x01

void setup() {
  Serial.begin(9600);
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH);
  SPI.begin();

  Serial.println("EEPROM Initialized with WP disabled and HOLD enabled.");

  enableWrite();
  writeEEPROM(0x00, "Hello EEPROM");
  delay(10);

  char buffer[20];
  readEEPROM(0x00, buffer, 12);
  Serial.println("Read from EEPROM:");
  Serial.println(buffer);
}

void loop() {
  // Nothing in loop
}

void enableWrite() {
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(WRITE_ENABLE);
  digitalWrite(CS_PIN, HIGH);
}

void writeEEPROM(uint16_t address, const char* data) {
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(WRITE_DATA);
  SPI.transfer((address >> 8) & 0xFF);
  SPI.transfer(address & 0xFF);
  for (size_t i = 0; i < strlen(data); i++) {
    SPI.transfer(data[i]);
  }
  digitalWrite(CS_PIN, HIGH);
  delay(5);
}

void readEEPROM(uint16_t address, char* buffer, size_t length) {
  digitalWrite(CS_PIN, LOW);
  SPI.transfer(READ_DATA);
  SPI.transfer((address >> 8) & 0xFF);
  SPI.transfer(address & 0xFF);
  for (size_t i = 0; i < length; i++) {
    buffer[i] = SPI.transfer(0xFF);
  }
  buffer[length] = '\0';
  digitalWrite(CS_PIN, HIGH);
}

Upload the code to your Arduino, open the Serial Monitor, and you'll see “Hello EEPROM” read back from the memory chip.

Conclusion

This tutorial covered interfacing a 25AA160A EEPROM with an Arduino Uno using the SPI protocol. EEPROMs are useful in embedded systems when you need to store data that persists after power-off. Try extending this by storing more complex data or interfacing multiple EEPROMs!

Post a Comment

0 Comments