What is Serial.println(F(""))
What is Serial.println(F("This is a constant string"))? What is Serial.print(F("This is a constant string"))?
Answer
Serial.println(F("This is a constant string")) is a combination of Serial.println and The F() macro. This helps save SRAM memory by moving the constant string from SRAM to FLASH memory. Usually, FLASH memory is much more available than SRAM memory. Let's see two examples with Arduino UNO.
Example of using constant string without the F() macro
void setup() {
Serial.begin(9600);
Serial.println("ArduinoGetStarted.com");
}
void loop() {
}
If we compile the above code for Arduino Uno, we will see the compiled log on Arduino IDE as below:
Sketch uses 1496 bytes (4%) of program storage space. Maximum is 32256 bytes.
Global variables use 210 bytes (10%) of dynamic memory, leaving 1838 bytes for local variables. Maximum is 2048 bytes.
Example of using constant string with the F() macro
void setup() {
Serial.begin(9600);
Serial.println(F("ArduinoGetStarted.com"));
}
void loop() {
}
If we compile the above code for Arduino Uno, we will see the compiled log on Arduino IDE as below:
Sketch uses 1506 bytes (4%) of program storage space. Maximum is 32256 bytes.
Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.
Let's analyze the logs:
- In both examples, we can see that FLASH memory (program storage space) is 32256 bytes, much more than SRAM (dynamic memory - Maximum is 2048 bytes)
- When using the F() macro, the used SRAM is reduced by 22 bytes (from 210 bytes to 188 bytes). The 22 bytes is the size of the string we print (including the string-terminating character - null)
Buy Arduino
1 × Arduino UNO Buy on Amazon | |
1 × USB 2.0 cable type A/B Buy on Amazon | |
1 × Jumper Wires Buy on Amazon |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.
Additionally, some links direct to products from our own brand, DIYables.