Table of Contents
Here’s how to use the CMD prompt to upload a .hex file (compiled hex file) to Arduino without using the Arduino IDE. Also, how to use the command prompt to upload a .bin file to an ESP8266 or ESP32.
If you’re not sure what .hex and .bin files are, read on. The code written in the Arduino IDE is written in C and/or C++, a high-level programming language that is easy for us to read and write. Microcontrollers are incapable of understanding high-level programming languages.
However, when you click upload, the Arduino IDE converts your code into hex or/and bin files, which are then uploaded to microcontrollers. In general these files act as your device’s firmware.
Many times, a developer would make .hex or .bin file accessible to a client because the developer put in a lot of effort to write that code and doesn’t want to show you how he did it, so the developer can just provide you with a compiled .hex or .bin file instead of programming code, and you would need to learn these steps to upload .hex and .bin file using command prompt (CMD prompt) without using Arduino IDE.
HOW TO CREATE A .hex OR .bin FILE
Select your ‘development board’ under “Tools,” then click on ‘Export compiled binary’ under “Sketch.”
-
- Tools → Board: → Arduino Genuino/Uno (let)
- Sketch → Export compiled Binary
- Tools → Board: → Arduino Genuino/Uno (let)
This will generate two hex files if you’re using an Arduino board, or bin files if you’re using an ESP8266 or ESP32 board, in the same folder (same location) where the ino file is saved.
HOW TO UPLOAD .hex FILE IN ARDUINO UNO/NANO/MEGA USING COMMAND PROMPT
Before you begin, keep in mind that you will need three things
-
- avrdude location
- avrdude.conf location
- .hex file location
STEP 1
To begin open the Arduino IDE and go to File > Preferences, check the box upload under Show verbose output during:
STEP 2
Select a board from the tools menu, in my case Arduino Genuino/Uno, and a COM port from the tools menu. Mine is COM14, but it’s possible that yours is different.
STEP 3
If everything is in order, you can upload any code or sketch (either blank or any)
STEP 4
Before the red colour text began from, something like “avrdude: Version 6.3-20171130” as seen in the screenshot, after the upload was successful.
You’ll come across avr dude command like this, but your path may be slightly different. Have a look at these points and see if they contain -CC, -Uflash:w:\path_of_hex_file:i
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM14 -b115200 -D -Uflash:w:D:\MyCode\String_Seprator\String_Seprator.ino.standard.hex:i
Copy this line and paste into some text editor like notepad++, next 2 steps are important.
STEP 5
Replace the path between ‘w’ and ‘i’ in the last red line with the path to the hex file you intend to upload.
STEP 6
Since the CMD prompt doesn’t recognize spaces between paths see here in Program Files (x86), use double-course “” for the path This is what the command above would look like. (see below).
"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude" "-CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM14 -b115200 -D -Uflash:w:"D:\PIJA Education\String_Seprator\String_Seprator.ino.standard.hex:i"
If you don’t use double-course, you’ll get an error like this:
‘C:\Program’ is not recognized as an internal or external command, operable program or batch file.
STEP 7
Now open a command prompt (Windows+r), type cmd in the run wizard, and click OK.
STEP 8
The path from step 6 should be copied, right-clicked, and pasted; the shortcut key ctrl + v would not work here in cmd prompt.
STEP 9
If all of the steps above are completed correctly, you can see that the upload was successful. with a message at end “avrdude done. Thank you.”
HOW TO UPLOAD .bin FILE IN ESP8266 USING COMMAND PROMPT
To upload .bin file to ESP8266, follow the same instructions from 1 to 3 as we did for Arduino, except board and port selection, these will be different different this time. For example board may be NodeMCU 0.9 (ESP-12 Module) and Port is COM11.
STEP 1
Once you upload code successfully, Before the red colour text began from, something like “esptool.py v2.8” as seen in the screenshot
You’ll come across upload command like this, but your path may be slightly different.
C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\tools\python3\3.7.2-post1/python3 C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py --chip esp8266 --port COM11 --baud 115200 --before default_reset --after hard_reset write_flash 0x0 C:\Users\abc\AppData\Local\Temp\arduino_build_9655/test.ino.bin
Copy this line and paste into some text editor like notepad++, next 2 steps are important.
STEP 2
Replace the path in the last red line with the path to the bin file you intend to upload.
STEP 3
Since the CMD prompt doesn’t recognize spaces between paths see here in Program Files (x86), use double-course “” for the path, if space present in path. This is what the command above would look like. (see below).
C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\tools\python3\3.7.2-post1/python3 C:\Users\abc\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py --chip esp8266 --port COM11 --baud 115200 --before default_reset --after hard_reset write_flash 0x0 "D:\PIJA Education\test/test.ino.nodemcu.bin"
STEP 4
Now open a command prompt (Windows+r), type cmd in the run wizard, and click OK.
STEP 5
The path from step 3 should be copied, right-clicked, and pasted; the shortcut key ctrl + v would not work here in cmd prompt.
STEP 6
If all of the steps above are completed correctly, then click enter and you can see that the upload was successful with a message at end “Hard resetting via RTS pin…”