I have burned an arduino bootloader to an ATmega168PA. I want to use the internal 8MHz oscillator for the ATmega, and I used an arduino Uno as an ISP. For that, I did the following:

  • Created the directory at Documents/Arduino/hardware/breadboard/avr. In there I placed a boards.txt file, and a bootloaders folder, which contained the .hex file. Both files can be found at the end.

  • I found the hex file I used in this link. From it, I used the optiboot_atmega168pa_8000000L_57600.hex

The burning of the bootloader completed successfully. Also I successfully uploaded a blink example to my ATmega168PA via the arduino ISP and the Arduino IDE.

The problem is that when I try to upload something a second time, I get this error:

enter image description here.

If I reburn the bootloader, I can again successfully upload a sketch. But once more, I cannot upload a second time.

optiboot_atmega168pa_8000000L_57600.hex file:


Boards.txt file:

atmega168pabb.name=ATmega168PA on a breadboard (8 MHz internal clock)




  • \$\begingroup\$ Do you do an erase before the write? \$\endgroup\$
    – Ron Beyer
    Commented Apr 4, 2020 at 19:38
  • \$\begingroup\$ @Ron Beyer I do not think I do. I have been using the Arduino IDE to upload the code, so I use the standard procedure one would do in order to reprogram the arduino. What would you suggest? \$\endgroup\$
    – NickG
    Commented Apr 6, 2020 at 10:35
  • 1
    \$\begingroup\$ if you upload a sketch over ISP, you delete the bootloader \$\endgroup\$
    – Juraj
    Commented Apr 7, 2020 at 9:07
  • \$\begingroup\$ @RonBeyer How do I do the erase? \$\endgroup\$
    – NickG
    Commented Apr 29, 2020 at 10:51
  • \$\begingroup\$ @Juraj Isn't the bootloader necessary in order to run the arduino code and use arduino IDE and libraries to standalone atmegas? How does it work if the bootloader is deleted? \$\endgroup\$
    – NickG
    Commented Apr 29, 2020 at 10:52

1 Answer 1


It appears that you haven't written the lock bit after loading the bootloader. If the lock bit isn't set to its "locked" state, the data will be overwritten by the code that you load and you will not be able to load a sketch a second time or any thereafter without erasing the memory and writing a new bootloader. You can make use of avrdude through the terminal, where you can specify the bootloader hex file you wish to load, and you can write the fuse bits and the lock bit as required for your microcontroller version.

You might find this blog entry useful: https://foraysinto3d.blogspot.com/2011/09/chapter-17-in-which-i-finally-burn.html

It also appears your boards.txt file has the same value written for lock_bits and unlock_bits:


These entries should have different values so that you can toggle the lock bit (it will not work if they are the same).

Here you can read about avrdude: https://avrdudes.github.io/avrdude/7.1/avrdude.html#Introduction

If you choose to make use of avrdude from the command line, the entries should look like this:

$ /usr/local/bin/avrdude -p atmega168p -c avrisp -P /dev/ttyUSB0 -b 115200 -e -v -C /home/myusername/Arduino/hardware/MiniCore/avr/avrdude.conf -Ulock:w:0xff:m -Uefuse:w:0xfc:m -Uhfuse:w:0xdf:m -Ulfuse:w:0xf7:m -Uflash:w:/home/myusername/Arduino/hardware/MiniCore/avr/bootloaders/optiboot_flash/bootloaders/atmega168p/16000000L/optiboot_flash_atmega168p_UART0_115200_16000000L_B5.hex:i 
$ /usr/local/bin/avrdude -p atmega168p -c avrisp -P /dev/ttyUSB0 -b 115200 -v -D -Ulock:w:0xcf:m 

In the first line, I write the lock bit OFF (unlocked):


and in the second line, once the bootloader is written and verified, I write the lock bit ON (locked):


Of course, you should become familiar with the format for avrdude, and you should identify the specific command line options for your microcontroller. In these lines you will see that I used Minicore, which offers support for your version of the ATmega168 (i.e. version PA). Make sure that the values you write for the fuses match those in the boards.txt file (in my case, the one from Minicore).

See Minicore: https://github.com/MCUdude/MiniCore

Also, please note that this method requires the use of an ICSP (I used an arduino pro mini loaded with the example ArduinoISP sketch that comes in the Arduino IDE, only having modified the baudrate based on my requirements, and it worked for me).


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.