Sub topic 5
Keeping String Literals Out of SRAM with `F()`
This chapter assumes you can write and upload Arduino sketches, use the Serial Monitor, and understand variables and functions. The examples target AVR-based boards such as the Arduino Uno.
Why string literals use SRAM
A string literal is text written between quotation marks, such as "Sensor ready". On an Uno, a normal string literal passed to Serial.println() is treated as a C string in SRAM. The text also occupies space in flash, where the program is stored.
The Uno has only 2 KB of SRAM. Long messages, menu text, and debugging output can consume that space quickly. A sketch may compile and upload successfully while leaving too little SRAM for variables and the stack.
The F() macro tells the Arduino library to keep a string literal in flash and read it from there when printing. It does not remove the text from the program. It prevents the text from taking an additional copy of SRAM.
void setup() {
Serial.begin(9600);
Serial.println(F("Sensor ready"));
Serial.println(F("This message remains in flash memory."));
}
void loop() {
}Open the Serial Monitor at 9600 baud. The output is the same as it would be for ordinary string literals, but the F() versions use less SRAM on AVR boards.
How `F()` changes the path
Without F(), Serial.println() receives a pointer to a normal character array. The Print class assumes that the pointer refers to SRAM.
With F(), the macro creates a flash-string type. The Arduino library selects a different println() overload that reads bytes from program memory instead of treating the pointer as an SRAM address.
The flash-aware overload matters. F() is useful with Arduino functions that support flash strings, including common Serial.print(), Serial.println(), and LCD library functions.
> callout note > Use F() for fixed text that your sketch only needs to display. Do not use it when you need to modify the text. A flash string is read-only.
Applying `F()` consistently
Use F() around each fixed message. It also works with pieces of output:
const int sensorValue = 512;
void setup() {
Serial.begin(9600);
Serial.print(F("Sensor value: "));
Serial.println(sensorValue);
Serial.print(F("Status: "));
Serial.println(F("OK"));
}
void loop() {
}The number stored in sensorValue is still held in the normal data areas. Only the fixed text is moved out of SRAM.
Do not write F() around a variable:
char label[] = "Temperature";
// Serial.println(F(label)); // IncorrectF() expects a string literal at compile time. For a variable character array, use Serial.println(label). If the variable must also avoid SRAM, it needs a different design, such as a PROGMEM array or a fixed buffer built at run time.
F() is mainly important on AVR boards with small SRAM. Boards with larger memory may not need it, but using it for fixed diagnostic text remains a clear habit.
Exercise
Rewrite this statement so the fixed message does not consume an SRAM copy on an Arduino Uno: Serial.println("Calibration complete");
Show answer
Serial.println(F("Calibration complete"));