Most Arduino users probably are aware that Arduinos contain memory but have you ever thought about what type of memory there is? There is actually three types of memory in an Arduino! These are: flash, SRAM and EEPROM. In this article we will look at each type in turn and look at why you might actually want to control what is stored in these areas.
Flash is a form of storage that you have no doubt used, even if you’re not aware of it! USB drives and memory cards are examples of flash memory. Flash memory is non-volatile. This means that it retains its content, even when there is no power. Flash can be reprogrammed multiple times. However, flash suffers from memory wear – this is where there is a finite number of times that flash memory can be reprogrammed. However, this is constantly being improved. Flash is where your sketch is stored. Although flash can be reprogrammed, in an Arduino you cannot modify code from an executing program. To modify this data, it is copied into the SRAM.
There is a keyword, PROGMEM, that allows you to store data in the flash, instead of the SRAM. One reason that you may wish to do this is to store large chunks of data, that may be too large for the SRAM. However, do this means that it cannot be modified at runtime! Therefore, it is best to use it for storing data that is immutable! As long as you are using an IDE of 1.0 or above, then you can use the keyword PROGMEM. If not, you need to add (to the top of your program):
PROGMEM only works with global variables and ones that have been defined with the static keyword.
SRAM can also be written to multiple times. However, SRAM is volatile, this means that any data that it holds will be lost when the power is turned off.
We can split SRAM into three sections: static data, heap and stack.
This is the space where your global and static variables are stored.
The heap is for anything that needs to be allocated dynamically.
The stack is the area for your local variables, interrupts and function calls.
EEPROM is a non-volatile memory that can be written to multiple times.