Does Flash need to be erased before uploading sketch

I have a bunch of ATMega328P (dip) to program. If I use the IDE (1.8.15) it erased flash before uploading the sketch and there is no option to stop that. If I use AVRDude or AVRDudess I have the option to erase flash (and eeprom)or not.

I uploaded the bootloader to all the chips (just to set the fuses). Then I went back and uploaded the compiled sketch (hex file) to the chips but in using AVRDudess I told it not to erase flash. Now I am wondering if I should have had it erase flash. Again all that is on it is the bootloader.

Does flash need to be erased before an upload? Or does it matter?

Thank you for your help.

What exactly do you mean by "upload"? Are you doing a standard "Upload" via the bootloader, or an "Upload using programmer" via an ISP programmer?

Some relevant information from the AVRDUDE documentation:


Disable auto erase for flash. When the -U option with flash memory is specified, avrdude will perform a chip erase before starting any of the programming operations, since it generally is a mistake to program the flash without performing an erase first. This option disables that. Auto erase is not used for ATxmega devices as these devices can use page erase before writing each page so no explicit chip erase is required. Note however that any page not affected by the current operation will retain its previous contents.


Causes a chip erase to be executed. This will reset the contents of the flash ROM and EEPROM to the value ‘0xff’, and clear all lock bits. Except for ATxmega devices which can use page erase, it is basically a prerequisite command before the flash ROM can be reprogrammed again. The only exception would be if the new contents would exclusively cause bits to be programmed from the value ‘1’ to ‘0’. Note that in order to reprogram EERPOM cells, no explicit prior chip erase is required since the MCU provides an auto-erase cycle in that case before programming the cell.


Erasing sets the cells to 1. Writing changes the appropriate cells to 0. There is no way to write a 1. So, yes, it matters a great deal if you want your 1s to be correct.

Including erasing?

Actually I used the Canaduino bootloader loader to install the bootloader on the ATMega328P. The chip was new prior to that so it had never been programmed and was fresh from the factory.

I loaded the bootloader using a Canaduino bootloader loader. The chip was factory new prior to that. I assume that it erased the chip prior to uploading that bootloader to the chip.

Then I used AVRDudess to upload a hex file to flash memory but did not have it erase flash prior to that. I was uploading using Programmer AVR ISP hat through a Uno that had gotten the AVR ISP example sketch loaded on it. AVRDudess said it loaded the hex file successfully but then I got to wondering how well that would work if it had not erased flash first.

Thank you.

In that scenario, the risk is the interrupt vector table. I assume, while uploading the bootloader, the fuses are set so the bootloader's table is in place. Then, while uploading the application, the fuses are set so the application's table is in place.

Is there a reason you did not use the bootloader to upload the application?

I did not use the bootloader to upload the application because I had 60 of these chips to program and AVRDude is much faster than using the IDE. I saw no easy way to set the fuses in the IDE even though it can be easily done in AVRDude and AVRDudess. I could skip the bootloader completely by just burning the fuses. I failed to do that on the first batch and the program used the ATMega328P internal 1mhz oscillator instead of the external 16mhz clock that I needed it to use.

Thank you for your help.

1 Like

I believe others have had success, without much effort, combining the two into a single upload...

...for next time. :grin:

I tried to combine the bootloader into the sketch at compile time with the IDE as it creates a Intel Hex file of the sketch and also a second hex file that is the sketch plus bootloader but when I tried to upload the combined file I got an error that said there was not enough room on the ATMega328P so I reverted to putting bootloader on first and then upload the sketch. I am still a bit confused as I had AVRDude do a flash and eeprom erase before uploading the hex file to the chip so the bootloader is gone (or I assume it is).

Thank you for your help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.