Arduino Mega2560 to flash from file on SD card

Hi all,

This might prove useful to someone - I’ve modified the Mega2560 bootloader using some code from Petit FatFS (Petit FAT File System Module) so you can overwrite your sketch using a file uploaded to the SD card.

To use it, you need to compile your sketch, and then convert it from hex to binary - I use avr-objcopy, but I’m not sure if that’s part of the standard Arudion distribution. I think there are various hex>bin converters if you Google it. Once you have your binary file, you can copy it onto the SD card (using something like GitHub - ovidiucp/TinyWebServer: Small web server for Arduino, fits in 10KB ROM, less than 512 bytes RAM) and make sure it’s called “app.bin”. To get the bootloader to overwrite the sketch, you need to set eeprom bit 0 to 1, and reboot the Arduino, either with the reset pin or with the watchdog.

If it successfully overwrites the flash on the Mega eeprom bit 0 will get set to 2, and if it can’t open your “app.bin” file, eeprom bit 0 will get set to 3.

I’m not an expert in bootloaders so it’s somewhat hacked together (and only minimally tested!), but it might prove a useful starting point for someone. The source is in the attachment, along with the compiled bootloader.

Jamie (54.9 KB)

I meant to say I've #undef ENABLE_MONITOR to get enough space to fit the extra code in, so the bootloader serial monitor won't work; you can still upload sketches using USB though.


Hi Jamie,

It looks promising, but it cant seem to find the sd card. Getting EEPROM(0) as 3. The sd card is set as standard on the mega2560, ** MOSI - pin 50 ** MISO - pin 51 ** CLK - pin 52 ** CS - pin 53

Card is FAT as per recommended.

Any thoughts or comments?

Thanks man!