Need to store values that stay saved even when the Arduino loses power? In that case, an external EEPROM connected through SPI is exactly what you need. EEPROM (Electrically Erasable Programmable Read-Only Memory) allows you to keep important data permanently, and the SPI interface provides a fast and reliable way to communicate with it. This is useful in projects like data recorders, configuration memory, and devices that remember settings after shutdown.
In this guide, you will connect a 25AA160A SPI EEPROM to an Arduino Uno, write a string into the chip, and then read it back. By the end, you will understand the wiring, necessary SPI commands, and how the code works.
Why Use External EEPROM with Arduino over SPI?
The Arduino’s RAM is temporary and resets when power is removed. EEPROM, on the other hand, stores data permanently. This makes it suitable for saving things like calibration offsets, system parameters, logs, or user settings.
SPI is ideal for exchanging data with memory chips. It works quickly, uses dedicated hardware pins on the Arduino, and supports adding multiple SPI devices if needed. Learning this gives you practical experience with embedded memory and communication protocols.
Required Components
To follow this project, prepare the following items:
- Arduino Uno (or compatible board)
- 25AA160A EEPROM or any SPI-based EEPROM such as 25LC256
- Breadboard
- Jumper wires
- Arduino IDE
The Arduino IDE already includes the required SPI.h library.
Make sure to refer to your EEPROM’s datasheet. Pin labeling may vary slightly, but SPI functionality remains the same.
Understanding the EEPROM Pins
The 25AA160A has several pins, but only a few are necessary here:
- VCC / GND: Power connections
- CS (Chip Select): Enables communication with the chip
- SCK (Clock): Drives data timing
- SI (MOSI): Data sent from Arduino to EEPROM
- SO (MISO): Data sent from EEPROM to Arduino
- WP (Write Protect): Disable writing when pulled HIGH
- HOLD: Pauses communication if pulled LOW
For this setup:
- Tie WP to GND to allow writing.
- Tie HOLD to 5V for continuous operation.
Wiring the EEPROM to Arduino
Use the Arduino Uno hardware SPI pins:
| EEPROM Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| CS | Pin 10 |
| SCK | Pin 13 |
| SI (MOSI) | Pin 11 |
| SO (MISO) | Pin 12 |
| WP | GND |
| HOLD | 5V |
Ensure the connections are firm and correct.
Important SPI Commands for EEPROM
The EEPROM follows specific commands sent through SPI:
| Command | Hex | Purpose |
|---|---|---|
| WRITE_ENABLE | 0x06 | Allows writing to memory |
| WRITE_DISABLE | 0x04 | Disables writing |
| WRITE_DATA | 0x02 | Write data to memory |
| READ_DATA | 0x03 | Read data from memory |
| RDSR | 0x05 | Reads status register |
In this project, the main commands used are WRITE_ENABLE, WRITE_DATA, and READ_DATA.
Arduino Code for SPI EEPROM
#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...");
enableWrite();
writeEEPROM(0x00, "Hello EEPROM");
delay(10);
char buffer[20];
readEEPROM(0x00, buffer, 12);
Serial.println("Read from EEPROM:");
Serial.println(buffer);
}
void 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);
}
Verifying the Output
Upload the code and open the Serial Monitor at 9600 baud. You should see:
EEPROM Initialized...
Read from EEPROM:
Hello EEPROM
To confirm persistence, reset the board. The stored data will still appear.
Common Errors to Watch For
- Not sending
WRITE_ENABLEbefore writing - Incorrect
CSwiring - Missing delay after writing
- Writing more than the EEPROM page limit at once
Avoid these mistakes to ensure reliable memory operation.
Expanding the Project
Once the basics work, you can:
- Save logged sensor readings over time
- Store user settings and load them during startup
- Connect multiple EEPROM chips using additional
CSlines
Summary
Using SPI EEPROM with Arduino allows you to store data permanently and retrieve it later, even after restarting the system. Mastering this builds strong embedded development skills while enabling more capable and practical Arduino projects.
0 Comments