upper limit on sketch size for Uno
Moderators: adafruit_support_bill, adafruit

upper limit on sketch size for Uno

by botbilder on Wed Jan 19, 2011 6:54 am

I think I just hit the wall on flash memory with my Wave shield project, when I thought I still had several K remaining.

My Arduino v.22 sketch worked fine compiling and uploading to my Uno at this size:

Code: Select all | TOGGLE FULL SIZE
Binary sketch size: 28032 bytes (of a 32256 byte maximum)


But when I added a few more functions and debugging strings it got up to 29142 bytes, at which point the sketch took way too long to upload, and finally Arduino printed some red error messages (which I didn't save). I had to trim my code back under 29K before it started working again.

Have others experienced a similar limitation, below 32K?

Any thoughts on what's stealing my last few K -- is it the bootloader, or my program data, or what? I do use PSTR() a lot.

Isn't it a bug that the Arduino environment reports the incorrect maximum sketch size?
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: upper limit on sketch size for Uno

by adafruit_support_bill on Wed Jan 19, 2011 7:08 am

Some space is taken up by the bootloader.
User avatar
adafruit_support_bill
 
Posts: 29657
Joined: Sat Feb 07, 2009 9:11 am

Re: upper limit on sketch size for Uno

by westfw on Wed Jan 19, 2011 11:47 am

In theory, the bootloader occupies 512 bytes, and your sketch can can be up to 32256 bytes long.
In reality, the current bootloader shipped with Uno has a bug that prevents it from being able to load any sketch that exceeds approximately 30k. See http://code.google.com/p/arduino/issues/detail?id=380
User avatar
westfw
 
Posts: 1373
Joined: Fri Apr 27, 2007 12:01 pm
Location: SF Bay area

Re: upper limit on sketch size for Uno

by botbilder on Thu Jan 20, 2011 7:04 am

Good to know, thanks for the pointer!

How easy would it be for me to build/install a patched bootloader in my Uno? I'm using Arduino 0022.
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: upper limit on sketch size for Uno

by westfw on Thu Jan 20, 2011 10:57 am

User avatar
westfw
 
Posts: 1373
Joined: Fri Apr 27, 2007 12:01 pm
Location: SF Bay area

Re: upper limit on sketch size for Uno

by botbilder on Mon Jan 24, 2011 5:30 pm

Fantastic! Thanks for putting together this solution.

I verified that I can now build and upload sketches past the 30K limit. In my case the limit was even lower, around 28700 bytes -- maybe because I use a lot of PROGMEM strings (PSTR) to conserve RAM.

Here are the steps I took to fix it in case other people have the same problem.

First I had to get a new Uno (which happens to be the SMD version) to use as a programmer for my old Uno. I built and uploaded the "optifix" sketch from this thread to the new Uno. Then I unplugged it (powered off) and hooked up the pins between the new Uno and the old Uno with breadboard wire as shown in this diagram. After triple-checking my wiring I plugged the new Uno back into the computer, opened the Arduino serial monitor (which you might have to switch to 19200 if you're normally at 9600 baud), and saw that as soon as the "new Uno" reset it recognized the attached "old Uno" and burned the new bootloader into it. You can also type a "G" into the serial monitor to force another upload. Then I unplugged the new Uno from the computer (powered off), took out all the breadboard pins, and verified that my old Uno still worked -- but now had about 3K more of program memory to fill up!
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am