How do I load a hex file into a nano?

I am trying to load a hex file onto a nano.
I am reading arduino as ISP and arduino bootloaders.
I have set my uno and nano up so the uno is the programmer and the nano the target. I think I have successfully loaded the bootloader onto my nano, just to test the wiring and connection.

Below the heading the page then says
"Arduino ISP turns your Arduino into an in-circuit programmer to re-program AtMega chips.
It stops at the bootloader and doesn't tell how to go any further.
Is it possible to load hex files using this method and if so how do I do it?

Yes, it's possible.

First enable Show verbose output during upload in file → preferences in the IDE.
Next I suggest that you first upload an innocent sketch like blink via ICSP. This will give you the avrdude command that is executed when uploading via ICSP. Below for a Windows system

"C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\bugge\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v  -patmega328p -cstk500v1 -PCOM4 -b19200 "-Uflash:w:C:\Users\bugge\AppData\Local\arduino\sketches\C2BB89089FBB8E1897B952FC31B78159/Blink.ino.hex:i"

After that you can run copy that command and run it from the command line replacing the name of the file by your hex-file like

"C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\bugge\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v  -patmega328p -cstk500v1 -PCOM4 -b19200 "-Uflash:w:pathTo\myHex:i"

It will overwrite the bootloader but that is not a problem; the only purpose of burning the bootloader is to set the fuses correctly for the 328P and that will be maintained.

thank you sterretje,
I am not quite sure about how to do this.
I have enabled show verbose output during compile as well as upload and I am getting a long list of commands when I compile the blink sketch, none that include ones similar to your lines.



C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Bob\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Bob\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries P:\sketches\libraries -fqbn=arduino:avr:nano:cpu=atmega328 -ide-version=10813 -build-path C:\Users\Bob\AppData\Local\Temp\arduino_build_496674 -warnings=none -build-cache C:\Users\Bob\AppData\Local\Temp\arduino_cache_860807 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Program Files (x86)\Arduino\examples\01.Basics\Blink\Blink.ino
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Bob\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Bob\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries P:\sketches\libraries -fqbn=arduino:avr:nano:cpu=atmega328 -ide-version=10813 -build-path C:\Users\Bob\AppData\Local\Temp\arduino_build_496674 -warnings=none -build-cache C:\Users\Bob\AppData\Local\Temp\arduino_cache_860807 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Program Files (x86)\Arduino\examples\01.Basics\Blink\Blink.ino
Using board 'nano' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Detecting libraries used...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674\\sketch\\Blink.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
Generating function prototypes...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674\\sketch\\Blink.ino.cpp" -o "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674\\preproc\\ctags_target_for_gcc_minus_e.cpp" -DARDUINO_LIB_DISCOVERY_PHASE
"C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674\\sketch\\Blink.ino.cpp" -o "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674\\sketch\\Blink.ino.cpp.o"
Compiling libraries...
Compiling core...
Using precompiled core: C:\Users\Bob\AppData\Local\Temp\arduino_cache_860807\core\core_arduino_avr_nano_cpu_atmega328_0c812875ac70eb4a9b385d8fb077f54c.a
Linking everything together...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674/Blink.ino.elf" "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674\\sketch\\Blink.ino.cpp.o" "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674/..\\arduino_cache_860807\\core\\core_arduino_avr_nano_cpu_atmega328_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674" -lm
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674/Blink.ino.elf" "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674/Blink.ino.eep"
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-objcopy" -O ihex -R .eeprom "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674/Blink.ino.elf" "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674/Blink.ino.hex"
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-size" -A "C:\\Users\\Bob\\AppData\\Local\\Temp\\arduino_build_496674/Blink.ino.elf"
Sketch uses 924 bytes (3%) of program storage space. Maximum is 30720 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

I assume I have got your instructions wrong.
I assume my hex file has to be in the same directory as the other files
Where do I put this command so it loads the file into the nano?
Is it loading 3 files avrdude, avrdude.conf and my hex file?

It looks like you did not do an upload over ICSP. It's the menu Sketch → Upload Using Programmer. Sorry that I did not make that clear.

Your hex file is at the end of the command; it was (in my example) *
Blink.ino.hex*. You only need to replace that by the full path to your hex file.

Another approach would be to use Zak Kemble's AVRDudess, which is a GUI front end for AVRDude.

You would select Arduino as ISP as the Programmer, and select its COM port. Then at the upper right, select ATmega328P as the MCU. Enter the hex file you want to upload in the Flash box, and select Intel Hex as the Format. Then select Write, then Go.

The picture shows Bandgap.ino as my sketch, including the location of the file. I had to know the location. After Verifying your sketch in the IDE for the Nano and its appropriate old or new bootloader, but before shutting down the IDE, you will find the hex file in your AppData\Local\Temp\Arduino_build_xxxxxx.

Included in that folder is, in my case, the file Bandgap.ino.with_bootloader.hex. I haven't ever tried it, but I think if you upload that file, you will have both your sketch and the bootloader, if that's what you want. So the question is - do you want the bootloader to still be there after you've uploaded your hex file?

I should add that this works for IDE v1.8. I don't know about v2.

AVRDudess can also be used to read and set the fuses, as shown in the picture, as well as read and write EEPROM data. It's quite a useful program.

If the object is simply to load a hex file into a Nano that has a bootloader on it, you don't need to use an ISP. Just arduino-cli.

arduino-cli -tv upload -b arduino:avr:nano:cpu=atmega328 --input-file <your hex file here> --port <your port here>

Sorry gents I am still lost,
I am going to repeat something I asked earlier,
where do I put this command line?

You don't use any other command lines .

Just click UPLOAD WITH PROGRAMMER.

Programmer = Arduino as ISP.

Do you use the Arduino IDE?

mikedb, putting it in capitals doesn't make it any clearer and I assume it is 'upload using programmer' in the sketch tab.
I assume I have the 'arduinoISP' sketch in the UNO?
Where do I put the lines "C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\bugge\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v -patmega328p -cstk500v1 -PCOM4 -b19200 "-Uflash:w:pathTo\myHex:i"
or at least my version of them so that the hex file goes into the nano?

1 Like

Command prompt (or trrminal in linux/mac).

Thank you for bearing with me sterretje, I really would like to succeed with this.
I am using windows 10 and the IDE version 2.3.4.
I now have a hex file called nano_file.hex in the c:directory (the root directory) and the other files avrdude.exe and avrdude.conf in the files pointed to in the following entry, now bearing in mind that some of the slashes are forward and some are back the same as yours in post 2 does this look about right

C:\Users\User\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude” “-CC:\Users\User\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf” -v  -patmega328p -cstk500v1 -PCOM4 -b19200 "-Uflash:w: C:/nano_file.hex:i”

I think my COM is 20 but that will depend on which lead I use and hence which com port is connected so I will alter PCOM4 to PCOM20.
Do I need the UNO set up connected to the computer on COM 20 if it is relevant with the ISP sketch in it, set to 'programmer Arduino as ISP' and the nano connected to the UNO via ISP?
Or am I loading direct into the nano with no UNO connected?

Remember a capacitor for your Uno after you uploaded the ISP sketch to it.

You need to upload the ArduinoISP sketch to the Uno using the IDE; use the Uno's COM port. That will make the Uno a programmer.
Next you can run the avrdude command, again using the Uno's port.

Sorry guys, I still can't get the dam thing to work.
I have an Uno containing the Arduino ISP, a nano connected to pins 10,11,12 and 13 of the UNO into the isp socket of the nano and a capacitor inserted in the UNO reset as it should be.
The full line I have been quoting above seemed to be too long for the command prompt so I have moved the /bin and the /etc directories down to the c: drive.
I now have two directories in the root directory.
/bin contains the 7 files that are present in the /bin directory when it is so far in the drive and the /etc directory contains avrdude.conf so I should be able to call it with
"C:\bin\avrdude” -C "C:\etc/avrdude.conf” -v -patmega328p -cstk500v1 -PCOM10 -b19200 -Uflash:w: "C:/nano_file.hex:i”
The hex file to load is in the root directrory.
When I put the instruction above in the command prompt and press enter I get


I have tried changing the slashes \ for / and vice versa (does it matter which way round they are?) but with no improvement.
Can you see what I am doing wrong?

Hehe, now I understand why it fails every time, you've mixed linux and windows directory structure.

Edit: Also when using paths in windows you never use / only
Edit2: Apparently you can :upside_down_face:

The problem is caused by the use of "curly quotes" (”) in the command. You can use these characters in prose, but you should never use them in machine syntax. You must always use straight quotes " in that application.

Word processors often have a "smart quotes" feature where it will automatically use curly quotes. You must avoid ever using such word processors with command lines or code.

Probably not. Although \ is the native Windows path separator, the POSIX / is also supported by Windows in most contexts.

What do you mean by

?
I have altered the " to straight ones and it looks to have gone in?
"C:\bin\avrdude" -C "C:\etc\avrdude.conf" -v -patmega328p -cstk500v1 -PCOM10 -b19200 -Uflash:w: "C:\nano_file.hex:i"
I have set up the test with 3 leds as shown in
https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoISP/
that indicate a heartbeat, error and programming and they flashed so it looks as if it worked, I now have to try to build a bit more of the unit before I can confirm it.
Thanks for the advice I do appreciate it and will return when I get some indication that it worked or otherwise.

1 Like

Great news!

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