How Do You Add Memory To Arduino? (Plus More Memory Hacks)

Do you remember what you ate for lunch 3 days ago? You most likely won’t remember. However, if you wrote it down in a notebook 3 days ago, you could easily take out that notebook and find that page to remember what you ate. Similarly, the memory used in the Arduino can be limited, but its memory can be expanded using other tools.

Consequently, one question remains: “How do you add memory to your Arduino board?

You can use an SD card, a shield, or a module to add memory to your Arduino board, or you can utilize a different Arduino board with more memory. If you choose to use an SD card, you must get an SD card shield unless you have an Arduino Yun Rev 2 or the MKR Zero. These two boards have an SD card slot built directly into them.

There are also a few Portenta models that come with this feature. For instance, the Portenta Breakout board comes with a micro SD slot.

For users who don’t have these boards, an SD card shield is essential (if you need the extra memory). They can be relatively cheap such as this SD card Shield from SeeedStudio, which costs around $15.

Note: You cannot add RAM (Random Access Memory) to your Arduino because there is no bus to support it.

Keep reading to better understand how you can add memory to your Arduino, the memory types, and some more memory optimization tips that allow you to use more memory for your project.

How Do You Add Memory To Arduino?

As I’ve mentioned above, you can add memory using an SD card, a shield, or a module. For instance, you can use the Arduino MKR Mem Shield, and stack it on the Arduino MKR board since its female and male header pins fit perfectly with it.

Note: there are some shields that require soldering.

If you want to learn more about soldering and how you can implement it with an Arduino, consider checking out my guide on soldering. I’ve also included soldering tips that can improve your soldering experience.

However, if you know your project will require a lot of memory, then you should start with the Arduino Mega 2560 Rev 3. This board is bigger than most Arduino boards and comes with 256 KB of flash memory, 8 KB of SRAM, and 4 KB of EEPROM. In other words, it has 4 times the memory of a regular UNO Rev 3 board.

Here is a great table to help you decide which board you should get depending on your memory usage and the associated price.

BOARDFLASH (KB)SRAM (KB)EEPROM (KB)PRICE ($)
UNO R33221$21.90
Mega 256025684$40.30
Leonardo322.51$20.70
Micro322.51$20.70
Nano3221$20.70
Due512960$40.30
MKR WiFi 1010256320$32.10
UNO WiFi Rev 24860.25$44.80
Zero256320$39.50
Yun Rev 2322.51$56.40
Nano Every4860.256$10.90
Here are some of the popular Arduino boards and their memory and price!

The next table I made evaluates which board gives you the most bang for your buck in terms of the total memory. I know that there are other factors that can influence the price (such as clock speed, size, etc), but if you’re only focused on memory and cost, this table can help you out.

BOARDTOTAL
MEMORY (KB)
PRICE ($)PRICE PER
MEMORY ($/KB)
UNO R335$21.90$0.63/KB
Mega 2560268$40.30$0.15/KB
Leonardo35.5$20.70$0.58/KB
Micro35.5$20.70$0.58/KB
Nano35$20.70$0.59/KB
Due608$40.30$0.66/KB
MKR WiFi 1010288$32.10$0.11/KB
UNO WiFi Rev 254.25$44.80$0.83/KB
Zero288$39.50$0.14/KB
Yun Rev 235.5$56.40$1.59/KB
Nano Every54.256$10.90$0.20/KB
I did all the calculations myself

You can see that the MKR Wifi 1010 board is most worth it regarding its price per memory.

Anyways, here are the steps to using an SD card with the SPI (Serial Peripheral Interface) to add storage space to your board:

  1. Obtain an SD card shield or module and an SD card.
    1. Note: if you obtain a micros SD card shield (or module), then you would need to use a micro SD card
  2. Connect your board to the shield or module.
  3. Connect to the SPI.
  4. Install and declare the SPI.h and SD.h libraries.
  5. Set up the SD card in the setup() function.
  6. Use setup.open() to create/open a file for memory use.
  7. Read the file using the read() function.
  8. Write to the file using the println() function.
  9. Use close() function when you’re done to close the file.

If you want to learn how to connect your Arduino board to a module, I have embedded a Youtube video below for your viewing pleasure. However, if you don’t want to watch it and you need instant answers, I will summarize it below the video for you.

In educ8s.tv’s channel, he is using the Micro TF Card Memory Shield Module SPI Micro Storage Card Adapter as an example. This is relatively inexpensive and costs around 6 dollars.

Here are the steps for connecting them (I will be going back and forth from the module’s pins to the board’s pins). You need to connect (a total of 6) jumper wires from:

  • the GND (ground) pin to the (Arduino board’s) ground pin
  • the VCC (voltage common collector) pin to the 5V pin
  • the MISO (Master In Slave Out) pin to the digital pin 12
  • the MOSI (Master Out Slave In) pin to digital pin 11
  • the SCK (Serial Clock Output) pin to the digital pin 13
  • the CS (Chip Select) pin to the digital pin 10

For a visual representation of what I mean, you should go to the video above to the time: 2:05.

Here is also the code he used:

#include <SD.h>
#include <SPI.h>

int CS_PIN = 10;

File file;

void setup()
{

  Serial.begin(9600);

  initializeSD();
  createFile("test.txt");
  writeToFile("This is sample text!");
  closeFile();

  openFile("prefs.txt");
  Serial.println(readLine());
  Serial.println(readLine());
  closeFile();
}

void loop()
{
}

void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    return 1;
  } else
  {
    Serial.println("Error while creating file.");
    return 0;
  }
}

int writeToFile(char text[])
{
  if (file)
  {
    file.println(text);
    Serial.println("Writing to file: ");
    Serial.println(text);
    return 1;
  } else
  {
    Serial.println("Couldn't write to file");
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}

String readLine()
{
  String received = "";
  char ch;
  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      return String(received);
    }
    else
    {
      received += ch;
    }
  }
  return "";
}

What Types Of Memory Are There?

For those who don’t know much about the memory in Arduino or want a refresher, the Arduino uses three types of memory: flash memory (also known as program memory), SRAM (Static Random Access Memory), and EEPROM (Electrically Erasable Programmable Read-Only Memory).

Memory TypePurposeRetention/Volatility
Flash MemoryStores your Arduino sketchNON-VOLATILE
Data is retained when powered off
Data is lost when a new sketch is uploaded
SRAMStores the current data of the sketchVOLATILE
Information is lost when powered off
EEPROMStores long-term informationNON-VOLATILE
Data is saved just in case power is lost
Here’s a table for easy understanding

Here is some more useful information about these memory types (based off of an Arduino UNO R3):

Memory TypeSize (KB)Data Amount
(Approximations)
Lifespan
(Approximations)
Flash Memory321000 lines of code100,000 write cycles
SRAM21000 numbers20 years
EEPROM1500 numbers 100,000 write cycles

Under the “Lifespan” column, I didn’t include a specific length of time for either the flash memory or the EEPROM. That’s because it depends on your usage. However, if you want a rough estimate, both the EEPROM and flash memory can last for at least 27 years. You can get this number if you uploaded 10 sketches every day for the next 27 years.

This specific scenario may not likely happen to everyone, especially since the Arduino board itself may not possibly last that long. For instance (from forums sources), I’ve gotten a rough estimate of 20 to 30 years for the lifespan of an Arduino Uno Rev 3 board.

Amazing Memory Optimization Tips

I’ve included some optimization techniques for improving memory in my previous memory post: “Best Guide To Arduino Memory (Plus Memory Optimization Tips)”. The following list won’t include that same information, so if you want those tips, you can go back and read that memory guide.

  • Use other functions that use less memory.
    • For instance, use the function “millis()” instead of “delay()”.
  • Avoid using strings
    • If you can’t, try using the F() macro function. This will make the array of characters stay in PROGMEM.

For example, instead of using this code:

Serial.print("Hello Eveyone");  // This eats up memory!

Use this instead:

Serial.print(F("Hello Everyone"));  // Keeps the arrays of character in PROGMEM

Note: the strings kept in PROGMEM can’t be changed when the program is being run.

  • Remove code that you’re not using.
    • If memory is an issue, go through your code see if you have any “dead” code. Ask yourself these questions:
      • Are you using all the libraries?
      • Are you calling any of the functions you’ve created?
      • Are you using any of the variables?
        • Should the variable be local or global?
      • Are there any if/else statements or conditional expressions that won’t ever be true?
  • Get rid of the bootloader.
    • If you really need the memory, think about whether you need the bootloader or not.
    • Doing this saves between 2 to 4 kilobytes of flash memory (since it depends on the type of bootloader you’re using).
    • Do keep in mind that with the bootloader gone, you can’t use a regular USB (Universal Serial Bus) to upload your code to your board.
      • You’ll need to use an ISP (In System Programming) programmer.
      • Or you can program by using a USBASP (Universal Serial Bus AVR Serial Programmer) board.
        • Fun tidbit: the acronym AVR stands for Advanced Virtual RISC, which comes from the phrase “Alf-Egil Bogen Vegard Wollan RISC”.
  • Utilize the reserve() function. This creates a buffer for a string.
    • By using this, your sketch doesn’t have to readjust and redistribute the memory to the previously mentioned string if it somehow grows in the future.
    • Without this, the heap memory allocation becomes disorganized because it’s using intermediary variables, which are created to help change the strings.

To use the reserve() function, you take a look at the following syntax code:

myString.reserve(size);
// where myString is a variable of type String
// the number of bytes (unsigned int) in memory to reserve.

Here is an example code of how you should be implementing it in real life:

String myString;

void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }

  myString.reserve(26);
  myString = "i=";
  myString += "1234";
  myString += ", is that ok?";

  // print the String:
  Serial.println(myString);
}

void loop() {
  // nothing to do here
}
  • Change buffer sizes
    • You can modify your own buffer by making sure it’s not bigger than they need to be.
    • There are some libraries that use buffers to function smoothly. Consequently, if you have the patience and stamina to sit and read through the library code, then you should do it and see if you can reduce the consumption of memory anywhere.
    • There are also systems that will distribute buffers for different backend purposes. For instance, the 64 byte serial gets buffers from the system, so you can make the buffer size smaller for it if you don’t need the high speed serial communication it provides. To change it, go to your HardwareSerial.h file and find the following line:
#define SERIAL_BUFFERSIZE 64

Now, change the number 64 to something lower, and you’ll have a little more memory. However, you’ve sacrificed some high-speed serial communication.

What are buffers?

A buffer is a basically a physical memory storage space that momentarily stores data as it is being transferred from one area to another.

For instance, the reserve() function creates a buffer for a string.

Summary – tl;dr (Too Long; Didn’t Read)

Here are the key takeaway points you should keep in mind after reading this article:

  • You can add memory to your Arduino by using an SD card, a shield, or a module
  • Arduino has 3 types of memory: EEPROM, flash memory, and SRAM
  • Here are some memory optimization tips:
    • Use functions that use less memory
    • Avoid strings
    • Remove “dead” code
    • Get rid of bootloader
    • Use the reserve() function
    • Change buffer size

In the end, I hope this article didn’t RAM-ble on about memory and that you found it useful.

References

To ensure that I provide the best information possible for you:

Similar Posts