Sub topic 6
Storing Lookup Tables in Flash with PROGMEM
This chapter assumes you can write and upload an Arduino sketch, use the Serial Monitor, and understand variables, arrays, functions, and pointers.
A lookup table stores fixed values that a program reads instead of calculating them repeatedly. Examples include temperature limits, calibration values, font data, and sine-wave samples. On an AVR-based Arduino such as an Uno, a normal global array uses SRAM. The Uno has only 2 KB of SRAM, so a large constant table can consume space needed by variables and the stack.
PROGMEM is an AVR library feature that places constant data in flash memory instead of SRAM. Flash holds the program and remains available while the sketch runs. Data in flash is not read with ordinary array indexing. You use special functions from <avr/pgmspace.h>.
How PROGMEM changes data access
The following diagram shows the difference between declaring a normal constant array and declaring an array with PROGMEM.
A normal declaration such as const uint16_t table[] may still reserve SRAM on AVR boards. Add PROGMEM to request flash storage:
#include <avr/pgmspace.h>
const uint16_t thresholds[] PROGMEM = {
100, 250, 500, 750, 1000
};
void setup() {
Serial.begin(9600);
for (uint8_t i = 0; i < 5; i++) {
uint16_t value = pgm_read_word(&thresholds[i]);
Serial.println(value);
}
}
void loop() {
}The declaration has three important parts:
constdocuments that the table should not change.PROGMEMplaces the table in flash on AVR boards.uint16_tgives each value 16 bits, allowing values from 0 through 65,535.
pgm_read_word() reads one 16-bit word from flash and returns it as a normal value. The returned value is copied into value, which is an ordinary SRAM variable. That temporary variable is usually small, but it exists only while the loop uses it.
Choosing the correct read function
The read function must match the stored type. Common choices include:
| Stored type | Flash read function | Typical use |
|---|---|---|
| `uint8_t` or byte | `pgm_read_byte()` | Small flags or samples |
| `uint16_t` | `pgm_read_word()` | Thresholds and measurements |
| `uint32_t` | `pgm_read_dword()` | Large counters or timestamps |
| Pointer | `pgm_read_ptr()` | Tables containing addresses |
For structures or unusual types, read each field with the appropriate function, or use the library's structure-copy helpers. Do not assume that a flash address can be dereferenced like a normal SRAM pointer.
Common errors
A frequent mistake is using ordinary indexing and then treating the result as a flash read:
uint16_t value = thresholds[i]; // Wrong for a PROGMEM table on AVRUse pgm_read_word(&thresholds[i]) instead. Another mistake is modifying a PROGMEM table. Flash is intended for constant data during normal sketch execution. Create a new SRAM variable when you need to change a value.
PROGMEM is specific to AVR-style memory handling. On some newer Arduino boards, the architecture uses a unified address space or different flash-access rules. Check the board core's documentation before using AVR-specific headers in portable code.
Exercise
Why does the example copy a PROGMEM value into value before printing it?
Show answer
A PROGMEM item is stored in flash, so it must be read with a flash-access function. The returned value is then an ordinary SRAM value that Serial.println() can use.