Arduino and memory how to

There are three types of memories used at arduino.

  • Flash memory
  • RAM (SRAM)
  • EEPROM

In this article we will discuss all of them giving some basic code sources too.

The flash memory is where arduino sketch is stored. It is non volatile memory. That means that the information persists after the power is turned off. The flash memory usually is bigger in size than the other two types. It can be written 10.000 times. The arduino bootloader is written there so you loose 2KB if you use the arduino bootloader.

The SRAM (static random access memory) is where the sketch creates and manipulates variables when it runs. (The runtime memory)
This is a volatile memory and can be written unlimited times. When you declare a new run time variable this will be stored in SRAM.
The SRAM is relatively small memory (1024 bytes) and you have to use it with caution.
For example when you declare : char message[] = "Hello";
You are putting 5+1=6 bytes into SRAM on runtime (the one byte is the terminating null byte).
As you can see if you want to use constant long strings you can pretty easily reach the 1KB limit of your SRAM .
Using the PROGMEM command you can avoid this problem since you can store the constant string into flash memory instead of the SRAM. Unfortunately this trick cannot be done with variables since PROGMEM cannot modify the variables at run time. PROGMEM can not store non constant data.

EEPROM is a non volatile memory. This memory space can be used to store long-term information. It can be written 100.000 times
The eeprom is the smallest amount of ram offered by ATMega microcontrollers but it is extremelly useful when you need to store data. Consider the EEPROM as your hard disk. In order to use this kind of memory you have to use the EEPROM library.

Here are the amount of memories used by Arduino.

Memory for various microcontrollers used by Arduino
ATMega168 ATMega328P ATmega1280 ATmega2560
FLASH (2 Kbyte used for bootloader)  16 KBytes  32 KBytes  128 KBytes  256 KBytes
SRAM (RAM)  1024 bytes  2048 bytes  8 KBytes  8 KBytes
EEPROM  512 bytes  1024 bytes  4 KBytes  4 KBytes

More informations about arduino memories can be found at the official site.
In the next pages we will see three memory examples