Let’s get started with the second part of the Arduino programming series. This time we’ll be talking about memory management on the Arduino, how to use it and how best to manage it – showing you how to free up memory and guidelines for programming that will help you reduce your memory footprint on the Arduino.
This article requires some basic understanding of memory, datatypes and basic C/C++ programming skills.
There are 3 types of memory on the Arduino:
- Flash Memory – This is where your Arduino Sketch is stored.
- SRAM – This memory is used for variables and manipulation.
- EEPROM – This is a persistent memory space for long-term storage.
How is this useful? Well, from inside of the Arduino IDE you can use different types of memory to better manage your program, for example if you’re coming to the memory limits of the Arduino then you can use different types of memory for storage.
Normally when you create a variable (for example, a string of “Hello World”) this will get created and stored in the SRAM memory – Having many string variables will eat up your memory limits quite quickly, knowing this we could create the strings at burn-time (Flash memory) instead of initialising them when the code in the sketch executes.
This would mean that string manipulation is limited; you cannot modify a variable in Flash memory directly – It has to be done when the data is burned to the microcontroller, basically when you upload your sketch.
So, what are the memory types and limits?
(1 Kbyte used
|16 KBytes||32 KBytes||128 KBytes||256 KBytes|
|SRAM||1024 bytes||2048 bytes||8 KBytes||8 KBytes|
|EEPROM||512 bytes||1024 bytes||4 KBytes||4 KBytes|
The above table of information is taken from the Arduino.cc website. This shows how much storage is allocated for each type of memory on the Arduino. As you can see, there is lots of Flash memory to play around with.
Which memory type should I choose for storage?
Good question. It’s entirely up to the purpose of the application. One thing we do point out though, is that the EEPROM memory has a limited number of erase/writes, meaning that if it’s used too much it will eventually not work and come to the end of it’s life, there are no limits on read cycles though.
EEPROM has around 100,000 erase/write cycles before it will eventually come to the end of it’s life. Flash memory (sketch uploads) are rated for around 10,000 erase/write cycles. SRAM has no limits for erase/read/write cycles and is much faster to access than EEPROM or Flash memory.
If you are rapidly changing variables we would suggest using the default SRAM; if you’re storing static strings we would suggest using Flash memory. For data that doesn’t change very often (e.g. program settings) we would suggest EEPROM so that you don’t use many of the erase/write cycles and reduce the life span of the Arduino.
How do I use it?
See the example code below on using the different types of memory. Note that the Arduino IDE now supports the use of the F() which is a helper function for storing data in flash memory. This will cut down bytes in the SRAM memory pool.
As you can see there is already a built-in buy viagra online free shipping library and handy functions for reading and writing to EEPROM. You can view more information and more example code on the Arduino.cc website by clicking here: http://arduino.cc/en/Reference/EEPROM
Also check out a third-party library for EEPROM on the Arduino. This provides handy functions for storing strings and other data types. Check out the project here: http://arduiniana.org/libraries/flash/
There isn’t any specific code to use SRAM as any variables you create “normally” will automatically be stored in the SRAM memory space. Anything inside of the SRAM is lost once the Arduino is turned off – The same as RAM in your PC.
The code above is only used for debugging purposes, this shouldn’t be used in an actual sketch. It uses dynamic allocation and deallocation of memory to find out what available RAM is left on the Arduino.
Tips for saving memory
- F() all of your strings. This will automatically save some SRAM.
- Remove any unneeded variables; functions or #includes.
- Array sizing; make sure you’re only allocating what you need. If you allocate a and only use a the memory will still be reserved for that variable and not available for anything else to use.
- Use functions. All parameters passed to a function and variables created inside of a function are automatically freed once the function exits!
- Use the correct data types. Int, Double, Float – Do you really need the datatype that you’ve allocated?
You can use the below table for comparing data type memory sizes.
The one last thing we can think of is that there is a way of taking back some of the used space in the flash memory. There is a HardwareSerial.cpp file located in \hardware\arduino\cores\arduino\ this can be modified to free up more memory.
Look for the line #define SERIAL_BUFFER_SIZE 64 – This can be halved, or more if you are not receiving any high speed data on the Arduino.
If this isn’t enough you can go to the extreme and get rid of the Arduino bootloader. This would mean that the USB connection won’t work and you will have to program the Arduino using an ICSP connection.
- There are 3 types of memory pools on the Arduino. SRAM, Flash Memory and EEPROM
- SRAM is used for variables and variable manipulation.
- Flash Memory is for storage of the Sketch (this is persistent)
- EEPROM is persistent storage
- F() is a function used for storing data in Flash memory.
- PROGMEM is a variable modifier for storing variables inside of Flash memory.
- Flash Memory and EEPROM has a limited number of erase/write cycles.
- SRAM is very fast to access, where as Flash memory is slow.
- See “tips for saving memory” above for simple programming guidelines to save memory.
We recently found by changing around some programming habits we saved quite a bit of memory in some of our sketches, more so on our Bluetooth project that was 90% Serial.print’s to our Bluetooth COM port.
Hopefully we have covered quite a bit on memory management for the Arduino; if you think we’ve missed anything off or would like to ask any questions, have any comments or suggestions then please feel free to leave a comment below or send us a message using the contact form.