Spi RAM

If you need more storage and only need to retain data while the Arduino is powered, then adding RAM is a handy solution. The 23K256 (Datasheet - PDF) is 32k SRAM chip which uses an Spi interface. :

Version tested and working with Arduino IDE 1.0.5 and the latest SPI Library:

Attach:spiRAM3a.zip

Below is the original version of the library, which requires the Spi library as a dependency. If you are running older (pre 0019) versions of Arduino, use this library. It has been tested against Arduino 0017 only:

Attach:SpiRAM.zip

Both versions of the library implement a class called SpiRAM, which provides a bunch of read/write methods. The 23k256 has three operating modes: Byte mode (r/w one byte at a time), Page mode (r/w in pages of 32 bytes), and Stream mode (r/w abitrary numbers of bytes). I've therefore written three sets of read/write functions, which prototype as follows:

char SpiRAM::read_byte(int address);
char SpiRAM::write_byte(int address, char data_byte);

void SpiRAM::read_page(int address, char *buffer);
void SpiRAM::write_page(int address, char *buffer);

void SpiRAM::read_stream(int address, char *buffer, int length);
void SpiRAM::write_stream(int address, char *buffer, int length);

The library instantiates an SpiRam object which can be used to access these methods.

Code Example

#include <SPI.h>
#include <SpiRAM.h>

#define SS_PIN 10

byte clock = 0;
SpiRAM SpiRam(0, SS_PIN);

void setup()   {                
  Serial.begin(9600);
}

void loop()                    
{
  char data_to_chip[17] = "Testing 90123456";
  char data_from_chip[17] = "                ";
  int i = 0;

  // Write some data to RAM
  SpiRam.write_stream(0, data_to_chip, 16);
  delay(100);

  // Read it back to a different buffer
  SpiRam.read_stream(0, data_from_chip, 16);

  // Write it to the serial port
  for (i = 0; i < 16; i++) {
    Serial.print(data_from_chip[i]);
  }
  Serial.print("\n");
  delay(1000);                  // wait for a second
}

Other Notes

The 23K256 runs at 3.3V so remember to power it from the 3.3V line of your Arduino (or voltage divide the 5V rail) and voltage divide the transmit lines from the Arduino (i.e. MOSI, Clock, and Chip Select).

Spi attached EEPROM chips look like they work in a very similar way to the 23K256, so it should be fairly easy to modify this library to work with EEPROMs.

LichP? 20091108, 20101118

SpiRAM it's not for Mega2560, somebody for give the correction and give us an example.

Thanks.

Also see http://forum.arduino.cc/index.php?PHPSESSID=j5ogefr7mm7kii6235ltm3j7t0&topic=222386.0 for suggestions for improvement of this library.

Also see Spi RAM With Ethernet Shield.

SPI RAM is one of many kinds of storage mentioned in InterfacingWithHardware.

Share