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…


Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards

Join Adafruit on Mastodon

Adafruit is on Mastodon, join in! adafruit.com/mastodon

Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.

Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7pm ET! To join, head over to YouTube and check out the show’s live chat – we’ll post the link there.

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Join over 36,000+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython – The easiest way to program microcontrollers – CircuitPython.org


Maker Business — “Packaging” chips in the US

Wearables — Enclosures help fight body humidity in costumes

Electronics — Transformers: More than meets the eye!

Python for Microcontrollers — Python on Microcontrollers Newsletter: Silicon Labs introduces CircuitPython support, and more! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi

Adafruit IoT Monthly — Guardian Robot, Weather-wise Umbrella Stand, and more!

Microsoft MakeCode — MakeCode Thank You!

EYE on NPI — Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey

New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — #NewProds 7/19/23 Feat. Adafruit Matrix Portal S3 CircuitPython Powered Internet Display!

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !



4 Comments

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

  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.

  3. Bill,

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

    Regards,
    Eric

  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.

Sorry, the comment form is closed at this time.