Main Content

Communicate with a SPI EEPROM Device

This example shows how to store and retrieve data from an EEPROM on a SPI device. Using EEPROM you can read, erase, and rewrite individual bits of data from the sensor’s memory. This example uses SPI EEPROM 25AA080C device from Microchip Technology with the Arduino® Mega 2560. Before using this example, wire an EEPROM to the SPI pins on your Arduino as described:

SPI EEPROM PinsArduino Pins
/CSDigital pin 10
/WP50k or 100k ohm resistor tied to Vcc
VSSArduino Gnd
Vcc5v pin

It is also recommended that you place a 1uF decoupling capacitor between VCC and Gnd.

Create a SPI connection on your Arduino.

Create an Arduino object and include the SPI library.

a = arduino('COM10', 'Mega2560', 'Libraries', 'I2C, SPI, Servo');

Write to, and read back data from an EEPROM, which is a micro controller designed to interface directly with the Serial Peripheral Interface (SPI). For more information about the read and write sequence in an EEPROM, refer to the functional description in the EEPROM Data Sheet.

Create a chip select pin connection.

Cspin = 'D10';

Create a SPI device object.

eeprom = device(a, 'SPIChipSelectPin', Cspin', 'BitOrder', 'msbfirst', 'SPIMode', 0)
eeprom = 

  device with properties:

        Interface: SPI
 SPIChipSelectPin: D10             
           SCLPin: D21
          SDIPin: D50
          SDOPin: D51
  Show all properties, functions

Specify the read, write and write enable commands referring to the EEPROM instruction set .

Instruction NameInstruction FormatDescription


0000 0011

Read data from memory array beginning at selected address


0000 0010

Write data to memory array beginning at selected address


0000 0100

Reset the write enable latch (disable write operations)


0000 0110

Set the write enable latch (enable write operations)


0000 0101

Read STATUS Register


0000 0001

Write STATUS Register

readCmd = 0b00000011;
writeCmd = 0b00000010;
writeEnable = 0b00000110;

Write enable the device.

writeRead(eeprom, writeEnable);

Set the device address. The address is a 16–bit value, with 8 upper and 8 lower bits. The highest value on the address can be 0x03FF.

address = [0 0];

Write 'Hello' at the 0th address of EEPROM.

dataToWrite = [writeCmd,address double('Hello')];
writeRead(eeprom, dataToWrite)
ans =

     0     0     0     0     0     0     0     0

Read back the data from the EEPROM.

dataToWrite2 = [readCmd address zeros(1,5)];
returnedData = writeRead(eeprom, dataToWrite2)
returnedData =

     0     0     0    72   101   108   108   111

Display the result.