Thursday, May 2, 2013

Arduino and Memory

To date I have not done very complicated work on the Arduino itself as my interface library allows me to offload any heavy duty processing to the host RPi.   I am now working on something that would run solely on the Arduino and am being forced to come to terms with the limitations of a micro controller environment.
  • Program logic has to fit within the Arduino's flash memory which static (burned when you sketch is uploaded)
  • There is EEProm memory available for sketches to use to store things that are needed across sessions.   This is in very short supply but in a microcontroller environment how much should you need anyway?
  • Most importantly, however, is the random access memory, or RAM, needed for a program during it's execution.   This is in relatively short supply especially if you have any desire to use arrays.
Here is a table from the Arduino Playground that shows the constraints that must be observed:

ATMega168ATMega328PATmega1280ATmega2560
Flash
(1 Kbyte used
for bootloader)
16 KBytes32 KBytes128 KBytes256 KBytes
SRAM1024 bytes2048 bytes8 KBytes8 KBytes
EEPROM512 bytes1024 bytes4 KBytes4 KBytes

My development environment is an Uno (ATMega168) so it is pretty constrained.  In a test case a sketch with two integer arrays (a voltage reading and a matching time stamp) maxed out at 400 entries.  On my ATmega1280 that is driving the 'Bot the top limit was 1400 entries.   I have a Gert Board on order and it uses the one in between (ATMega328).

This was not a realistic case as any reasonably complicated program is going to use more RAM than my example.   There are ways of maximizing RAM.   One is simply to be cautious with everything and especially with buffers that can be reused and character strings that can be abbreviated.   Static information can also be stored in program memory using PROGMEM (here is an example).

One last comment on the Arduino and Memory.   How did I first realize that I was having problems with memory management???   Sketches either did not run at all or when they ran they were corrupted and doing REALLY weird things.  Drove me a little bit banana's.

No comments:

Post a Comment