Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Using the Flash Program Memory of Arduino to Pre-store Data

Contents

If your application needs to pre-store data inside your program (or flash) memory in your Arduino to access them later, this is a good place to start.

Introduction

There are several situations where it’s more practical to store data in your program flash memory rather than your RAM. One such situation is you might run out of RAM during program execution. Imagine trying to display several text or graphics entries on your LCD display during runtime. You’ll surely have issues if all these data only reside in RAM.

In this regard, An Arduino UNO has only around 2K bytes of SRAM compared to 32K bytes of program memory. Similarly, there are only 32k bytes of SRAM and 4MB of program memory on an ESP32-WROOM-32. You’ll see that you have so much space left to use for your data besides storing it all in RAM.

In this article, we’ll focus on data that must be pre-stored (or declared) at the beginning of your code in program memory. You can fetch these data later in your code anytime you want.

Using the PROGMEM variable modifier

The PROGMEM variable modifier is a reserved keyword in Arduino. It simply tells the program to allocate a declared variable in program memory space instead of in RAM. As you may have guessed, variable declarations without this keyword are automatically stored in RAM space.

Here are sample variable declarations using PROGMEM

Example 1: Store a Byte Value in Program Memory Space

				
					const uint8_t myVar PROGMEM = 128;
				
			

To access this byte stored in the program memory space, use the pgm_read_byte_near() function. Note that this function requires an address as a parameter. With this:

				
					// temp byte storage

uint8_t tempByte;

  // read back a byte

  tempByte = pgm_read_byte_near(&myVar);

  Serial.println(tempByte);
				
			

note that the parameter of pgm_read_byte_near() takes in an address.

Example 2: Store an Array of Bytes in Program Memory Space

				
					const uint8_t mVars[] PROGMEM = {1, 2, 4, 8, 16};
				
			

To access each byte in this array stored in the program memory space, still use the pgm_read_byte_near() function. With this:

				
					// temp byte storage

uint8_t tempByte;

uint8_t i;

  for (i = 0; i < sizeof(myVars); i++) {

    tempByte = pgm_read_byte_near(myVars + i);

    Serial.println(tempByte);

  }
				
			

Each address of the parameter of pgm_read_byte_near() is iterated by the number of elements in the array of bytes.

Example 3: Store an Array of Characters in Program Memory Space

const char myTXT[] PROGMEM = “My TXT 1”;

To access each character in this array stored in the program memory space, still use the pgm_read_byte_near() function. Also, use the strlen_P() function to find out the length of the array in program memory. With this:

				
					// temp char storage

char myChar;

uint8_t i;

// read back a char

for (i = 0; i < strlen_P(myTXT); i++) {

  myChar = pgm_read_byte_near(myTXT + i);

  Serial.print(myChar);

}
				
			

Above, you can see that you can store a single variable, an array of bytes, and an array of characters, all in program memory space. You can do this too with word length values but you have to use pgm_read_word_near() to fetch the values instead.

Example 4: Store arrays of strings in program memory space

				
					const char openingMessage1[] PROGMEM = "My TXT 1";
const char openingMessage2[] PROGMEM = "My TXT 2";
const char openingMessage3[] PROGMEM = "MY TXT 3";

const char *const messages[] PROGMEM = 
{
  openingMessage1,
  openingMessage2,
  openingMessage3,
};
				
			

The way to store arrays of strings in program memory space is quite unique. The first part does the same as storing an array of characters, only that you can declare more entries for a comfortable retrieval process later. The second part deals with a declaration so that you can easily access them as arrays of strings. This is the perfect scenario for text entries on simple LCD displays.

const char *const is a pointer to a character. This pointer will make it easy to access each of the arrays of characters openingMessage1[]. openingMessage2[]. openingMessage3[].

				
					// temp string buffer variable
char buffer[30];
  

strcpy_P(buffer,(char *)pgm_read_word(&messages[0]));
Serial.println(buffer);

strcpy_P(buffer,(char *)pgm_read_word(&messages[1]));
Serial.println(buffer);

strcpy_P(buffer,(char *)pgm_read_word(&messages[2]));
Serial.println(buffer);
				
			

Above, to access each of the strings easily, declare a string buffer then use the strcpy_P() function. strcpy_P is the same as the C string operator strcpy() except that the source data to work on resides in program memory. The buffer string variable will hold the array of characters you want to retrieve. The pgm_read_word() function will read the contents at the address of the specific element in the messages array.

Serial Monitor Example

The image below is a sample run printing out the array of strings from program memory space using the Serial Monitor.

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top