Arduino String Memory

I did a quick test tonight to see just how big a text string I could allocate memory for on the Arduino ATMEGA168. Since there’s 1KB of RAM on the 168, the theoretical maximum would be 1024 characters. However my particular test (with Serial on and a few words for debugging) failed after 623 characters. How much is that? Here’s 613 characters:

When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature’s God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.

Not so much, especially when you consider that it’s a _maximum_. As you add any variables to the program you’ll have proportionally even less space.

Here’s the code I used:


char* incomingResponse; // a pointer for a character string
int length =623; // the length of the string we’re going to allocate

void setup() {
pinMode(13,OUTPUT); // set up the status led
digitalWrite(13, HIGH); // turn on the status LED
Serial.begin(9600); // start serial for debugging output

if((incomingResponse = (char*) malloc(length+1)) == NULL) { // attempt to allocate string memory
Serial.println(“memory allocation failed”); // report if allocation fails
}
else {
Serial.println(“memory allocated successfully”); // otherwise report that allocation succeeded
}
}

void loop () {
digitalWrite(13, LOW) ; // turn off the status LED because we’re done
}

1 thought on “Arduino String Memory”

Comments are closed.

Scroll to Top