How to use F() macro in Arduino?

ArduinoHardwareSoftware & Coding

Often, you may have a lot of print statements in your Arduino code. These are generally stored in the SRAM.

However, if your sketch has too many of these print statements, they can fill up the SRAM very quickly. In such a scenario, it may be wise to store these print statements within flash memory (flash memory is generally much larger in size than SRAM). This is assuming that your sketch doesn't occupy the entire flash memory (which it generally doesn't).


A print statement like −

Serial.print("A typical constant string to be printed");

Can be replaced with the following −

Serial.print(F("A typical constant string to be printed"));

The addition of the F() macro will ensure that "A typical constant string to be printed" will be stored in Flash memory instead of SRAM.

Updated on 24-Jul-2021 12:48:45