Is there an established system, that, if a new version of the application is uploaded to Flash, switches to it automatically, but keeps the old one for the cases if, due to some problems, we need to return to it?

I ask for such a system in general, but also for the one, that operates with a CAN transceiver.


1 Answer 1


More powerful MCU with large flash memory have partitioning or flash memory banks where they can switch between different versions of the firmware. They have a register to set the start address so the application compiled for a specific start address can run on different real addresses in the flash memory.

Th Arduino Nano can't run a binary compiled for start address 0 on any other location in the flash memory.

InternalStorageAVR in my ArduinoOTA library can store the OTA uploaded update in upper half of the flash memory of a classic AVR. But the program can't run there. The bootloader must copy it to address 0 and start it from there. So the previous version is lost. In a large flash of Arduino Mega maybe it could store a copy of the previous version, but not for Nano. Any logic for this would make the sketch larger than a third of the flash memory.


If the bootloader does the transfer of the updated binary, then it can first copy the old sketch to upper half of the flash and then write the new sketch from address 0. At watchdog reset the bootloader could copy the old sketch back.

Optiboot could be modified to do this. As starting point, here is my copy_flash_pages function for Optiboot.

  • Thank you for a comprehensive answer. I really don't mind the scetch taking a third of the flash or more. Do I understand correctly, that if you could store two versions of a firmware in Nano's flash, you could switch between them just by letting the bootloader copy one to the address 0? Apr 3 at 11:43
  • I would be grateful, if you would outline (or provide a link to a documentation, where it is cleared up), how a bootloader can be triggered to execute different instructions? I think, I need two: one for saving the new version and using it, and one for switching to the old version. Apr 4 at 9:38
  • 1
    Writing the new version of sketch received over UART is what the bootolade does normally. See the source code of Optiboot. There is a code which detects upload so there you would activate the backup copying. To switch to backup, it depends on how do you want to detect that you need to switch to backup.
    – Juraj
    Apr 4 at 11:10
  • I need to signal it from the base station to a node using CAN Protocol, if it's possible Apr 4 at 11:42
  • 1
    that would assume that the sketch is running and you want to signal to it that it should activate the bootloader with a flag in EEPROM that it should copy the old version to address 0.
    – Juraj
    Apr 4 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.