Memory Problem? my sketch randomly freezes, or behaves strangely


I'm currently doing my first "big" project with Arduino, and I have some problems.

My sketch was basically working, and I added more and more features to it. Then it reached a point where it began to behave strangely. It could freeze, and not respond anymore, or it could suddenly go back to the setup() function.

My sketch uses a pair of switches, a pair of sensors, and an LCD screen (16*2).

The switches are used to toggle menus, change settings and take measures via the sensors.

So basically the software is a cycling menu with 5 differents actions (taking mesures, setting sensor 1 threshold, setting sensor 2 threshold, changing a parameter, resetting to "factory" default values).

Since I use the LCD screen to display all informations to screen, I use many Strings for the text to be displayed (that can be results of measures, so text with numeric variables embedded).

Also i'm using Arduino 1.0 RC2 (that might be a mistake).

The sketch runs on an Arduino Uno, it takes up 17000 bytes of the 30000 (or so ) available.

I tried to monitor the SRAM in several places, and it was around 1000 bytes, but I'm not sure the number is correct.

What could be the steps to debug this?

thanks a lot.

The typical advice to problems like this is to move all the LCD constant strings to flash memory using the proper commands to write them into flash and later read them back into sram when used. As I've never done it myself, only read about using the method several times here, I will leave further instructions to others. The 2k SRAM is used for all variables, constants, arrays, and stack use and is exhausted pretty easily with no easy error detection method.


It does sound like memory problems. Here are some things to look at:

  • avoid using large arrays, or if you must, make sure you know how much memory they use
  • don't use the String type
  • if you use a lot of string literals, put them in flash memory

The code I use to check the amount of free memory is this:

// Find out how much free memory we have
unsigned int getFreeMemory()
  uint8_t* temp = (uint8_t*)malloc(16);    // assumes there are no free holes so this is allocated at the end
  unsigned int rslt = (uint8_t*)SP - temp;
  return rslt;

However, it assumes that the malloc call succeeds and allocates memory at the end of the heap, which may not be true if you have ever called free() or used the String class (or anything else that calls free()).

Sorry to be late to answer,

I'll try first to get rid of all strings I use, and replace them by char arrays. Then I'll see if it's necessary to use the progmem thing.

I'll probably have a few questions along the way...

thanks a lot for your help!

COuld also be a power problem? how do you power the Arduino? What hardware is connected to it?


thanks for your answer,

Attached are, roughly:

1 serial LCD (16*2), with backlight, 1 LM386 and an elecret microphone, a photodiode, 2 switch... plus the few needed resistor, capacitors, etc...

For powering I use either: a laboratory power supply or a simple low cost external 1,5A switching power supply, or the USB bus of my MBP.

I didn't check yet the overall current consumption, but I was thinking it was not too much. I'm working right now with very minimal gear, but I'll check that when I'll be back in my "lab" (the place where are my measuring tools, etc... ), as for now I just have the circuit on my breadboard, the simple switching power supply and my computer.


I'm done replacing all Strings by char arrays, and now I'm moving the char arrays into flash memory using PROGMEM.

so far I have 27 char arrays of 16 chars in flash. (more could be transfered).

No more problems!

thanks a lot for your help!

BTW, I opened a new threads on RAM economy, so if someone is interrested to know how to reduce the RAM usage, go here:,79436.0.html