Free Up Storage Space on Some of Your Arduino Boards with Almost No Extra Work
Computers are comprised of several different parts, including the CPU, IO buses, memory, and various other peripherals/modules that assist in specific tasks.
On the ATmega328p (the integrated circuit used in the Arduino Uno R3 and Arduino Nano boards), memory is split into two categories, with 2 kilobytes of volatile SRAM for keeping temporary values and 32 kilobytes of flash to store data over a long period of time, such as programs and constant strings.
However, you might have noticed that the Arduino IDE only says 30720 bytes are available, not the full 32768 bytes as advertised: where did they go?
One of the main benefits when uploading new sketches to an Arduino Uno or Nano board is the ability to use USB, rather than the more complicated ICSP header. This is all made possible by a little program called the bootloader that resides in the flash memory and runs when the board is first powered on.
If there's a new sketch waiting in the UART buffer, the bootloader will continually read the data and store this new program alongside itself in flash.
Although this extra functionality is great, having to lose 2 kilobytes of program storage space can be inconvenient in some circumstances, so being able to shrink the bootloader is a very useful thing. Luckily, one YouTuber called Design Build Destroy was able to come up with a solution to this problem by just changing two small things.
AVR microcontrollers have a very small set of bits called "fuse bits" that act as little switches for controlling certain fundamental settings. Some of these include clock speeds, clock divisions, serial programming enabling, and more, but the one Design Build Destroy was interested in was the Fuse High Byte within the bootloader size selector. The last three bits control how much space is allocated for the bootloader, and he changed it from 2KB to 512B- a space savings of 75%!
With the correct amount of space now allocated, it was time to change the bootloader from the default one that comes bundled with the Arduino IDE to Optiboot, which is a much smaller, more optimized bootloader.
Thankfully, the Arduino IDE makes it easy to burn to the board, as all he had to do was specify the path to the hex file within the boards.txt definition and click "Burn Bootloader." Just before doing this, he set up a second Arduino Nano as an ISP device since the fuse bits can't be set over USB.
As seen in his video, freeing up 1.5KB of space enabled him to store much more text than would otherwise be allowed, and this can be extended to other programs that also need more space. Because Optiboot supports nearly all AVR microcontrollers, from the ATtiny8 all the way to the ATmega2560, this process can be used on several other boards too.