Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory limits on Arduino

Tags:

arduino

I have recently bought an Arduino Uno, and now I am experimenting a bit with it. I have a couple of 18B20 sensors and an ENC28J60 network module connected to it, then I am making a sketch to allow me to connect to it from a browser and read out the temperatures either as a simple web page or as JSON. The code that makes the web pages looks like this:

client.print("Inne: ");
client.print(tempin);
client.println("<br />");
client.print("Ute: ");
client.print(tempout);
client.print("<br /><br />");
client.println(millis()/1000);
//    client.print("<a href=\"/json\">j</a>");

The strange thing is: if I uncomment the last line, the sketch compiles fine, uploads fine, but I cannot get to connect to the board. The same thing happens if I add on some more characters in some of the other printouts. Thus, it looks to me as if I'm running into some kind of memory limit (the total size of the sketch is about 15 KB, and there are some other strings used elsewhere in the code - and yes I know, I will rewrite it to use an array to store the temporaries, I've just stolen some code from an example).

Is there any limit on how much memory I can use to store strings in an Arduino and are there any way to get around that? (using GUI v 1.0.1 on a Debian PC with GCC-AVR 4.3.5 and AVR Libc 1.6.8).

like image 365
MortenSickel Avatar asked Feb 02 '13 15:02

MortenSickel


People also ask

How much memory is on an Arduino?

The biggest difference between these microcontrollers and your general purpose computer is the sheer amount of memory available. The Arduino UNO has only 32K bytes of Flash memory and 2K bytes of SRAM. That is more than 100,000 times LESS physical memory than a low-end PC!

How many bytes can Arduino store?

On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value.

How much can Arduino hold?

Arduino UNO has 32kB of Flash memory which is sufficient to write thousands of lines of code. Normally while writing Arduino code SRAM is the most valuable memory on Arduino boards. Arduino UNO has only 2kB of SRAM which equals 2048 bytes.

Does Arduino have memory storage?

There are three pools of memory in the microcontroller used on avr-based Arduino boards : Flash memory (program space), is where the Arduino sketch is stored. SRAM (static random access memory) is where the sketch creates and manipulates variables when it runs.


2 Answers

The RAM is rather small, as the UNO's 328 is only 2K. You may just be running out of RAM. I learned that when it runs out, it just kind of sits there.

I suggest reading the readme from this library to get the FreeRAM. It mentions how the ".print" can consume both RAM and ROM.

I always now use (for Arduino IDE 1.0.+)

Serial.print(F("HELLO"));

versus

Serial.print("HELLO");

as it saves RAM, and this should be true for lcd.print. Where I always put a

Serial.println(freeMemory(), DEC);  // print how much RAM is available.

in the beginning of the code, and pay attention. Noting that there needs to be room to run the actual code and re-curse into its subroutines.

For IDE's prior to 1.0.0 the library provides getPSTR()).

IDE 1.0.3 now starts to display the expected usage of RAM at the end of the compile. However, I find it is often short, as it is only an estimate.


I also recommend that you look at Webduino as it has a library that supports JSON. Its examples are very quick to get going. However it does not directly support the ENC28J60.

like image 112
mpflaga Avatar answered Oct 06 '22 06:10

mpflaga


I use the following code to get free available RAM

int getFreeRam()
{
  extern int __heap_start, *__brkval; 
  int v;

  v = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);

  Serial.print("Free RAM = ");
  Serial.println(v, DEC);

  return v;
}
like image 39
ossamacpp Avatar answered Oct 06 '22 07:10

ossamacpp