How can I write the bootloader and sketch code to an Arduino through the ISP and then write the sketch code again using the Arduino IDE later?

Currently, I can write the bootloader through the ISP and then write the sketch through the Arduino IDE. This works just like normal.

I can also write the bootloader and sketch through the ISP. The code runs like normal, but I can't load a new version of the sketch through the Arduino IDE. It just timesout.

Here's what the IDE says when it attempts to upload the program:

avrdude: Version 6.0.1, compiled on Apr 15 2015 at 19:59:58
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM5
         Using Programmer              : wiring
         Overriding Baud Rate          : 115200
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

The Arduino IDE creates two hex files after compiling the sketch: one is just the compiled sketch (Sketch.ino.hex) and the other is the compiled sketch with the bootloader code appended to the end (Sketch.ino.with_bootloader.hex). So, I feel like this should be possible because Arduino produces the file for me.

migrated from electronics.stackexchange.com Nov 30 '15 at 18:30

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • There are several plausible reasons that the bootloader doesn't work when contacted by acrdude, driven by the Arduin IDE. It may be that writing a combination of compiled Sketch and bootloader using ISP does not enable the ATmega to run the bootloader in response t the IDEs 'remote' RESET. For example the combination are not linked correctly, so the bootloader is not 'connected' to the RESET interrupt. Or the bootloader is not put into the correct place. DO you know how to use objdump to check that code is at the correct address, and interrupt vectors have the right value? – gbulmer Nov 30 '15 at 18:49
  • 1
    Summary: IMHO In theory, anything can be written into ATmega flash over ISP. It should be possible to write the bootloader and program at the same time over ISP. However, IMHO you'll need to post the link script, avrdude configuration and command line, program, and a link to the bootloader to get an accurate answer. – gbulmer Nov 30 '15 at 18:54
  • You may well be able to simply concatenate the hex files. – Chris Stratton Nov 30 '15 at 19:00
  • The Arduino IDE concatenates the files for me. That's the file I'm uploading. Also, I'm not using AVR dude. My sketch code is compiled by the Arduino IDE. Everything is done by the Arduino IDE except I am loading the bootloader using the mkII AVR-ISP which connects to Atmel Studio. – Joey M Nov 30 '15 at 19:08
  • 2
    I figured out the problem. Turns out the Arduino wasn't being reset after loading the code so it wouldn't startup properly, leading me to believe it wasn't working. I just had to hit the reset button and then the sketch code executed as normal. So, you can load the bootloader and sketch code at the same time without doing anything special and it will work as if you loaded the bootloader through ISP and sketch code through Arduino IDE. – Joey M Nov 30 '15 at 21:28

If you program via ISP, it erases the boot loader, because you do not need it. The whole point of the boot loader is to allow programming over USB. So if you want to use the normal Arduino USB input to program, burn the bootloader and then do not use the ISP.

Note my knowledge of this dates back to older arduino ~1.0 software, it is possible modifications have been made, but the above should still be true. If you have an ISP you do not need the bootloader at all.

  • I am programming the sketch's code and bootloader at the same time so the bootloader is not erased. The bootloader code is definitely there. I can read the memory through the ISP and see that the bootloader code is there. – Joey M Nov 30 '15 at 18:39
  • Are you setting the programmer option back to the arduino option in the Arduino software? Stk500, is that the programmer that arduino uses by default or is that your ISP? – MadHatter Nov 30 '15 at 18:42
  • I am using Atmel Studio to run the AVR-ISP. Then I connect the USB cable and use the Arduino IDE. So I'm not changing anything in the Arduino IDE. The Arduino IDE uses STK500. – Joey M Nov 30 '15 at 18:51
  • Hmm odd, I guess I was not to helpful, hopefully someone else will have more experience with this. – MadHatter Nov 30 '15 at 18:54

Normally (using the IDE via the serial/USB interface) the bootloader is retained, and the bootloader itself replaces the main code.

If you do ICSP programming then the first thing the programmer has to do is erase the entire chip (including the bootloader). Now nothing is stopping you putting the bootloader back as part of the programming sequence (it would add to the programming time slightly).

As Chris Stratton suggests, this is basically combining the .hex file for the sketch, and the .hex file for the bootloader. I'm not sure that literally concatenating them would work because there are a couple of other "record types" in the .hex file which might confuse the programmer if they were encountered twice - in particular the "end of file" record type.

However a strategy along these lines should give you a file that could be uploaded and put the bootloader back:

  • Take the .hex file from the compiler, and copy up to, but excluding, the "end of file" record to a temporary file.
  • Append the desired bootloader .hex file (including the "end of file" line).
  • Program the chip with the resulting file.

For larger chips (like the Atmega2560) you would need to be cautious about handling the "extended segment address" record because that sets the high-order byte of the address to be programmed.

The hex file format, as documented in my programmer sketch is:

Line format:


  :      = a colon

  (All of below in hex format)

  nn     = length of data part
  aaaa   = address (eg. where to write data)
  tt     = transaction type
           00 = data
           01 = end of file
           02 = extended segment address (changes high-order byte of the address)
           03 = start segment address *
           04 = linear address *
           05 = start linear address *
  (data) = variable length data
  ss     = sumcheck

            * We don't use these

I belive what you want to do is program a Arduino by ISP and retain the bootloader, I have done this on occasion by first loading the bootloader onto a chip, then upload the Arduino sketch, then i use the ISP programmer to read the whole flash back into hex file.

This hex file is then used to program the rest of the chips for production. (If you feel this takes too much time, it is possible to strip the hex file down, but that might be a bit complicated)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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