2018 April REVIEW
2017 | by Gene Casanova
Senior Systems/Network/Internet/Robotics Engineer
There are three memory resources in the microcontroller used on an Arduino systemboard:
- "Flash memory" ("Program Memory") The memory a program is stored in on-chip. The program is retained when electrical power is removed.
- SRAM (static random access memory); where a program creates and manipulates variables when it runs.
- EEPROM, a long term storing memory.
BEWARE - 'Flash memory' and 'EEPROM memory' are non-volatile; meaing content is retained after power is removed fromt he device.
SRAM is volatile and will be lost when the electrical power is removed.
The 'ATmega328' microcontroller, the Arduino UNO REV3 is based on, fhas the following amounts of memory:
Flash 32K bytes (0.5k is used for the "Arduino bootloader")
SRAM 2K bytes
EEPROM 1K byte
The 'ATmega2560' in the 'Arduino Mega2560' has larger memory space:
Flash 256k bytes (8k is used for the "Arduino bootloader")
SRAM 8K bytes
EEPROM 4K byte
Notice The small amount of SRAM available in the Arduino UNO. It is easy to use it all by having lots of strings in a program.
For example, with a declaration like:
char message = "Tasmania Cradle Mountain is an Amazing And Unique Environment On Earth I Enjoy Experinecing With A Special Person; ..y
each character is 1 byte, plus the '\0' terminator; therefore the example above requires 119 bytes of SRAM. It does not take much to fill SDRAM to the 2048 capacity; especially when large amounts of text is being sent to a display, or a large lookup table.
When SRAM becomes filled, a program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely. To check if this is happening, try commenting out the source code line in question, or shorten suspected strings or other data structures in the source code. If the programs then runs successfully, then filled SDRAM probably was the cause of prorgam failure. There are a few things to address SDRAM problems:
- If a program communicates with a program running on another computer, then try shifting data or calculations to the external computer, reducing the load on the microcontroller of the Arduino.
- If lookup tables or other large arrays are being used, use the smallest data type necessary to store the values needed. For example, an "int" integer takes up two bytes, while a 'byte' uses only one byte. A byte stores an 8-bit unsigned number; from 0 to 255.
- If there is no need to modify the strings or data, while a program is running, store them in flash (program) memory instead of SRAM; to do this, use the 'PROGMEM' source code keyword.
Flash memory is used to store a program and any initialized data.
A program can execute from flash, but data cannot be modified in flash memory from the executing program.
To modify any data loaded with a program into flash memory, it must first be copied to SRAM.
Flash memory is the same memory technology used for SD flash memory cards. It is non-volatile; a program will be retained when electrical power is removed. Flash memory has a finite lifetime of about 100,000 write cycles!
BEWARE: Upload 10 programs a day, every day, for 27 years, the microcontroller flash memory has passed its expected life and should be taken out of service and through away.
SRAM (Static Random Access Memory), can be read and written to from an executing program.
Static Data - This is a block of reserved space in SRAM for all the global and static variables from an executing program. For variables with initial values, the runtime system copies the initial value from Flash when the program starts.
Heap - The heap is for dynamically allocated data items. The heap grows from the top of the static data area up as data items are allocated.
Stack - The stack is for local variables and for maintaining a record of interrupts and function calls. The stack grows from the top of memory down towards the heap. Every interrupt, function call and/or local variable allocation causes the stack to grow. Returning from an interrupt or function call will reclaim all stack space used by interrupt or function.
BEWARE: Most memory problems occur when the stack and the heap collide. When this happens, one or both of these memory areas will be corrupted with unpredictable results. In some cases it will cause an immediate crash. In others, the effects of the corruption may not be noticed until much later.
The microcontroller on the Arduino and Genuino systemboards have EEPROM memory.
EEPROM memory retains content when electrical power is removed from the systemboard, or microcontroller chip.
The microcontrollers on the various Arduino and Genuino systemboards, have different amounts of EEPROM:
1024 bytes on the ATmega328
512 bytes on the ATmega168 and ATmega8,
4 KB (4096 bytes) on the ATmega1280 and ATmega2560.
The Arduino and Genuino 101 systemboards have an emulated EEPROM space of 1024 bytes.
Use The Technology Wisely & Keep It Simple
Cyber Security Since 1979 - Consulting Available | IoT Full Stack And App Developer
CGI Computer Wares | EST 1979