Free up some Arduino SRAM

Here is a slightly advanced hack. If you have a project that needs lots of ram for buffering data or other nonsense, you can get 10% more by cutting down the ram used in the Serial library. Unless you’re doing a project with a lot of Serial data coming in, you wont need 128 characters…I find 16 or 32 is plenty!
Open up hardware/cores/arduino directory, and edit the file named wiring_serial.c

Near the top is a #define RX_BUFFER_SIZE 128, which means 128 bytes are used for the buffer. You can change this to 32 (or even 16!). If you have almost no serial input, make it as low as you’d like as long as its > 0.

You can get another 2 bytes by changing the head and tail index values from int to uint8_t type. Just recompile your sketch and it will automatically do your thing.

Check out more hacks here…

Filed under: random — by ladyada, posted April 17, 2008 at 4:58 pm


Try Adafruit's new iPhone & iPad app for makers! Circuit Playground! "Incredibly handy for anyone working in electronics. Perfect for engineers and non-engineers alike."
Looking for engineers, makers and the builders of dreams? Try our Adafruit job boards.
Join our weekly Adafruit SHOW-AND-TELL at 9:30pm ET every Saturday night! Then at 10pm, ASK-AN-ENGINEER with Ladyada and the Adafruit team!

4 Comments

  1. The file named wiring_serial.c referred to above does not exist in the arduino16 library.

    Comment by Bill Gentles — August 25, 2009 @ 8:58 pm

  2. What Bill said.
    In Arduino v.17, the file wiring_serial.c does not exist, as in v.16 like Bill mentioned.
    I think I have found the solution to this problem though.
    go to:

    hardware/cores/arduino/HardwareSerial.cpp

    Right click the file and select “edit”. The file opened in notepad for me. Once open in notepad, hit Ctrl + F and type (or copy and paste) “#define RX_BUFFER_SIZE 128″ into the search box, click next or find.
    Change the 128 to a 32 or 16.

    Comment by Dustin — September 19, 2009 @ 4:38 pm

  3. Bill,

    In the arduino17 library, the file is “HardwareSerial.cpp”. It’s probably the same in arduino16.

    Regards,
    Eric

    Comment by Eric Schuyler — September 20, 2009 @ 3:12 pm

  4. You can save another couple of bytes of RAM by changing nearly all the “int” data types to “uint8_t” in HarwareSerial.cpp.

    In particular, look for “head” and “tail” inside “struct ringbuffer” (in Arduino 17, the head/tail variables have changed from those in the article), and “i” in “inline void store_char()”.

    I think you can also change the return type of HardwareSerial::read() to a uint8_t instead of an int too. That won’t save you any constantly used RAM, but it might save you a byte of stack.

    If you’re using your serial port at 9600, then setting the buffer to 16 is already pretty high. It’ll take 1.6ms to fill, which is quite a few cycles. If all you’re using the serial port for is human input, then you’re probably able to empty the buffer far quicker than a person can type.

    Comment by Coofer Cat — November 24, 2009 @ 6:10 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

www.flickr.com
adafruit's items Go to adafruit's photostream
www.flickr.com
items in Adafruits More in Adafruits pool