Note: the notes on this page are for all Arduino boards except the Arduino DUE, which has a different architecture.
There are three pools of memory in the microcontroller used on avr-based Arduino boards:
- Flash memory (program space), is where the Arduino sketch is stored.
- SRAM (static random access memory) is where the sketch creates and manipulates variables when it runs.
- EEPROM is memory space that programmers can use to store long-term information.
The ATmega328 chip found on the Arduino UNO has the following amounts of memory:
- Flash 32k bytes (of which .5k is used for the bootloader)
- SRAM 2k bytes
- EEPROM 1k byte
The ATmega2560 in the Arduino MEGA2560 has larger memory space:
- Flash 256k bytes (of which 8k is used for the bootloader)
- SRAM 8k bytes
- EEPROM 4k byte
Notice that there's not much SRAM available in the Arduino UNO. It's easy to use it all up by having lots of strings in your program. For example, a declaration like:
char message = "I support the Cape Wind project.";
If you run out of SRAM, your program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely. To check if this is happening, you can try commenting out or shortening the strings or other data structures in your sketch (without changing the code). If it then runs successfully, you're probably running out of SRAM. There are a few things you can do to address this problem:
If your sketch talks to a program running on a (desktop/laptop) computer, you can try shifting data or calculations to the computer, reducing the load on the Arduino.
If you have lookup tables or other large arrays, use the smallest data type necessary to store the values you need; for example, an int takes up two bytes, while a byte uses only one (but can store a smaller range of values).
If you don't need to modify the strings or data while your sketch is running, you can store them in flash (program) memory instead of SRAM; to do this, use the PROGMEM keyword.
To use the EEPROM, see the Arduino EEPROM Library.