Make and Share Compiled Files of Arduino Sketches!


Maybe you want to share your Arduino code in compiled format file for a Homework project or simply to impress your friends that you know Assembly ;), or you want to prevent illegal use and re-distribution of your proprietary libraries, or better, you only want to move further and flash your Arduino Board with HEX files generated with a different IDE.

Well, this instructable is for you!

When you execute or run any code, you never upload the source code to an Arduino or MCU development board, you always upload the compiled files, except if you are using an interpreted language in yor project like Johnny-Five JS framework (out of scope in this instructable).

I will explain you how you can create Hex. or Bin. files for your embedded project according to the MCU architecture that you are using (In my case 8-bit AVR and 32-bit ARM Cortex® M0+ core), using the Arduino IDE (I'm using the Arduino IDE 1.8.0 version) and then uploading your code using the windows Command Prompt.

I want to clarify, this process doesn't overwrite the bootloader
You must have a bootloader in your MCU because we are using serial communication to "burn" or better said "upload" the code into your microcontroller.

Lets begin!

Step 1: AVR

You can upload your arduino compiled file to your 8-bit AVR MCU using 3 techniques:

A) Windows Command Prompt and AVRDUDE.
B) Using Xloader software (it's free):
C) Using ArduinoSketchUploader (It's free):

I will only focus in the "A" option, the two others are too easy to use and there is a lot of documentation inside the web. At the same time, I will be using the Arduino Blink example for this tutorial.

First of all, what is "AVRDUDE"?
It's an open-source command line program, for reading, writing and manipulating AVRs, you can know more about this tool here:

Steps 1-4: Spying the Arduino IDE to see what commands it uses to upload a hex file.

The Hard Way:

1) Start the Arduino IDE.

2) Go to File -> Preferences and check Show verbose output during upload ("Subir salida detallada mientras: subir" in the first image).

3) Go to File->Examples->01.Basics->Blink, and then press upload to run the Blink example.

4) In the Text Console of the Arduino IDE, you will see an avrdude command line similar to this one (Second image):

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude" -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM36 -b115200 -D -Uflash:w:"C:\Users\Juan\AppData\Local\Temp\arduino_build_266611\Blink.ino.hex":i

This command is very important because we are going to copy and past this in the windows CMD later, the commad is specificified by:

[PATH]/bin/avrdude -C[Conf file] -v -p[AVR model] -c[stk500v1: protocol for getting information from a serial line into its flash memory] -P[Serial Port] -b[Baud velocity] -D- Uflash:w: [Hex file path of your skecth] :i

Steps 5-7: Utilizing the command to upload the hex file

5) Open the MS Windows application cmd and copy the above mentioned command exactly as it is displayed by your Arduino IDE. Remember add quotation marks between paths.

6) Replace the string between w: and :i (marked in bold style) with the path to your HEX file obtained using the Arduino IDE (You can use even other IDE). If, for example, you have the file C:\MCUProjects\BlinkLed.hex, then this is the new string that must be between w: and :i
If you want to know what means each letter in the command (optional lecture):

-p declares the chip being programmed.

-c gives the name of the programmer. It should be arduinoisp but this doesn’t read the fuses of the chip correctly. On my system it works with -carduino.

-P tells avrdude which serial connection is to be used. You can find this out when you have your Arduino IDE up and running. Look at tools –> serial port and see the name of the port that is activated. This is the port that connects to your arduino.

-b is the baudrate of the serial connection and has to be set to 19200. Omitting this won’t work.

-v is the verbose mode of avrdude and tells you a lot about the programmer and also the chip that is connected. At the end of this report the device signature and the settings of the fuses are displayed: lfuse is the Low Byte fuse, hfuse is the High Byte fuse and efuse is the Extended Byte fuse.

7 ) In CMD, position the cursor just after :i and press Enter. After a short time you will finally see the message "avrdude done. Thank you." which confirms the hex file was uploaded (third and fourth image).

The easy way:

As you can see, in the hard way version, the steps 1-4 are only necessary to find the path where the Arduino IDE saved the hex file of our sketch code.

1) We can generate the hex file of our skecth using only: Sketch->Export compiled Binary, or the shortcut "Ctrl +Alt + S".
2) In MS Windows Command Prompt run the AVRDUDE command:

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude" -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM36 -b115200 -D -Uflash:w:"path to your HEX file":i

Note: Please look that you have to put the the path string to my HEX file between w: and :i (marked in bold style in the last command).

3) After a short time you will finally see the message "avrdude done. Thank you." which confirms the hex file was uploaded (third and fourth image).

General Questions:

1) Does my MCU needs to have bootloader loaded?
R= Yes, the comunication betwen the MCU and the computer is by AVRDUDE, it uses the protocol STK500 that is property of Atmel. It is a serial protocol and programmers emulate this over a virtual serial port on a USB. The protocol is indicated by "-carduino" in the AVR command.

2) What if I don't want to use a Bootloader?
R= If you have an external programmer (e.g. an AVR-ISP, STK500, or parallel programmer), you can burn sketches to the Arduino board without using the bootloader. This allows you to use the full program space of the MCU on the Arduino board (flash memory). You are going to burn the hex file directly in the Flash memory of your AVR. You can still use AVRDUDE, only change the -c[Programmer] sentence in the AVR command line for "-c avrisp". So basically it should look like:

"avrdude -c avrisp -p atmega328 -P COMPORT -b 19200 -U flash:w:filetoburn.hex"

3) Do I really need only one file...just the .hex file? Really?

R= Yeah, no matter if your sketch has 6 external libraries, when you compile your code, all the instructions are converted to hexadecimal, all you need is the .hex file generated to burn AVRs with your code. It may be useful in mass productions.

Step 2: ATSAMD21G18--> ARM Cortex M0+

How you upload binary files of your code to an ARM Cortex M0+?

Well, I did't find enough information of how you can do this, so I created an empiric method. I will be using the ATSAMD21G18 because is a powerful MCU of 32-bits and ARM Cortex M0+. Yo can find this MCU embedded in a lot of development boards like: Arduino M0, Arduino Zero, Sodaq, Adafruit Feather, etc.

If you find another way to do this, please share your method with the community, you can comment in this instructable or share a link with a good tutorial. Thanks!

Ok, lets begin ;)

I'm using the Sodaq One V2 for this tutorial.

Steps 1-3: Creating the bin file

1)Connect your sodaq one via USB cable to your computer and upload the "Example 1->3 colors" that they have in their web page:

2)In theText Console of the Arduino IDE, you will see a similar command (before the orange lines) (Image one):

C:\Users\Juan\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.6.1-arduino/bossac.exe -i -d --port=COM34 -U true -i -e -w -v C:\Users\Juan\AppData\Local\Temp\arduino_build_535841/sketch_jun26a.ino.bin -R

Now, if you copy and paste this command in your MS Windows Command Prompt, you are going to upload your skecth to yor sodaq. Ok, we want to do that, but where is my binary file?!

3)The most important thing here, is that this command is what Arduino IDE have to do to upload your compiled code to your board. The bin file can be obtained by two ways:

The hard way is going to the path were Arduino saved our bin file: C:\Users\Juan\AppData\Local\Temp\arduino_build_535841/ and then copy the bin into any other ubication (This is the last path in the last command, and is between -v and -R).

The easy way to obtain the bin file of your sketch from de Arduino IDE is Sketch->Export compiled Binary, or the shortcut "Ctrl +Alt + S". This is going to generate a bin file in the same folder where we saved our sketch.

Steps 1-3: Uploading th Bin file without Arduino IDE.

4)Now that we already have our bin file, we have to run MS Windows Command Prompt and make the following command.

C:\Users\Juan\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.6.1-arduino/bossac.exe -i -d --port=COM34 -U true -i -e -w -v C:\Users\Juan\AppData\Local\Temp\arduino_build_535841/sketch_jun26a.ino.bin -R

Look that we are not using AVRDUDE, because the architecture is different, in the case of the SodaqOne we are using BOSSAC, thats is a command-line implementation of BOSSA - "Basic Open Source Sam-ba Application". "SAM-BA" is the "SAM Boot Assistant", something that Atmel implements in their built-in (and published) bootloaders.So BOSSAC.EXE is the utility used to upload the sketch to Due; essentially the Atmel ARM equivalent of "avrdude.exe".
If you are using an arduino Zero, instead of using avrdude to program the microcontroller you have to use a utility called "openOCD."

You have to change the COM port of your board and the path to your bin file.

The tricky part here is the COM port of your Sodaq one. One misconception you might have about the 32-bit boards is that two ports mean one is for the device, and one is for the host. That is not the case. Both ports are device-only. To select the correct COM port, go back to step 1 (Upload a skecth), in the Text console of Arduino IDE you are going to see what is the first COM port and the other. You have to select the fist COM Port that The text console show you, other way to do this is looking in the Arduino Port wich COM port are you connected, In my case I have two COM ports: COM 33 and COM34, I will be using COM33 because is the COM port that my arduino IDE use to serial communication. (Image 2). Please consider write the 1° COM port and the 2° COM port in a sheet because we are going to use this in the next step.

The path to your bin file is where you saved it!

5)Now that we already have theBOSSAC line with our Correct COM port selected and our bin file path selected too, we can write this in the CMD and run. Yo will see (Image 3):

"Set binary Mode"

"Send auto-baud"

After that, we have to reset our sodaq one (connecting the reset line 2x fast to ground), and nao we will see:

"Set binary Mode"

"No device found in COM33"

After that we have to introduce the same BOSSAC line that we typed in, only change the COM port for the 2° COM port that you already have found.

If everything is correct you have to see (Image 4):

"Set binary Mode"

"readWord "

More comands

6) After that we already generated and uploaded our bin file to a Sodaq One using BOSSAC command line and Windows command Prompt. We didn't have to run the IDE to upload a new sketch, and we can share only our bin code with others! (Please see image 5 and 6 to see how the process should look in the CMD).




    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest

    2 Discussions


    Question 6 months ago on Step 2

    Hi, thanks for the instruction. Is it also possible to use this for uploading files to ESP8266/NodeMCU ?

    1 more answer

    Answer 6 months ago

    Hi ObiToo55!

    Yes, in the case of the ESP8266/NodeMCU,after compile your code in the Arduino IDE it is going to give you a ".bin" file.
    You can view your ".bin" file (after compile) by:

    *Looking for the ".bin" file path in the black screen that is at the end of the IDE. (The status level).
    *Look for the bin file under «C:\Users\simbu\AppData\Local\Temp\buildXXXX.tmp folder»

    Remember that you compiled for ESP8266/NodeMCU, so of course you can't use that file with an ATmega microcontroller or other MCU (microcontroller), your ".bin" file will only works with others ESP8266/NodeMCU.

    Follow me in twitter if you want: @juanantonio488

    Happy Hacking!