low memory available, stability problems may occur?

Arduino IDE shows a low memory warning:

Sketch uses 3328 bytes (10%) of program storage space. Maximum is 32256 bytes. Global variables use 2016 bytes (98%) of dynamic memory, leaving 32 bytes for local variables. Maximum is 2048 bytes. Low memory available, stability problems may occur.

How can I solve it?

Answer

In the above warning, 'program storage space' is FLASH memory, and 'dynamic memory' is SRAM memory. Arduino usually has much more FLASH memory than SRAM memory. Low memory here is on SRAM memory. Io reduce SRAM memory usage, You can do the following:

Store static strings in flash memory by using the F() macro

For example, change:

Serial.println("Learn Arduino on ArduinoGetStarted.com");

To

Serial.println(F("Learn Arduino on ArduinoGetStarted.com"));

Change variable size to the minimum size by changing variable type

For example:

void setup() { Serial.begin(9600); } void loop() { for (int count = 0; count < 255; count ++) Serial.println(count); }

In the above code, maximum value of variable is 254. it only need one byte to store. int type takes two bytes. We can reduce the size by changing the type to byte, which takes only one byte. The above code become:

void setup() { Serial.begin(9600); } void loop() { for (byte count = 0; count < 255; count ++) Serial.println(count); }

Remove unused variables

If variables are declared but not used, remove it to save memory.

Use String.reserve() when using String object

If you use String objects, use reserve() function to your strings to prevent the memory from fragmenting

In this case, you need to know maximum size of String you will use. For example:

String myString; void setup() { Serial.begin(9600); myString.reserve(26); myString = "Arduino"; myString += "GetStarted"; myString += ".com"; // print the String: Serial.println(myString); } void loop() { }

Use local variables instead of global variables if possible.

For example, change

int analogValue; // global variable void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } void loop() { // reads the input on analog pin A0 (value between 0 and 1023) analogValue = analogRead(A0); Serial.print(F("Analog reading = ")); Serial.print(analogValue); delay(500); }

To

void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int analogValue = analogRead(A0); // local variable Serial.print(F("Analog reading = ")); Serial.print(analogValue); delay(500); }

Move constant data to PROGMEM

See Arduino - PROGMEM

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
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

The Best Arduino Starter Kit

※ OUR MESSAGES