Arduino

How to Download Program from Arduino to Computer

Arduino is a very diverse platform allowing users to interact with its ecosystem in multiple ways possible. Arduino provides us different ways in which we can read or write code on Arduino. Here in this article, we will cover how we download a program from an Arduino board and upload its Hex file to another Arduino.

How to download code from Arduino to PC

Arduino code is written in C or C++ language with little additional functions. All the Arduino boards have microcontrollers that can be programmed and it accepts information in the form of code. Microcontrollers cannot understand C/C++ code directly. Main code also known as sketch is converted into a Hex file which the microcontroller understands.

Note: Once our program is compiled and uploaded in the Arduino board it is not possible to extract code in C/C++ language. After compilation it is only possible to download the Hex file which later can be used in any microcontroller or Arduino board.

Now, we will cover some ways through which we can download a Hex file from Arduino and use it in any other Arduino or microcontroller. We have divided the process into two portions:

  • Downloading program from Arduino as Hex file to computer
  • Reading Hex file from one Arduino and programming another Arduino using Hex

Downloading Arduino Hex file to Computer

After the Arduino code is compiled and uploaded, we can access the Hex file from the Arduino board only. To download the Arduino program an open-source platform named AVRDUDE is used. AVRDUDE is a program used for uploading and downloading Atmel AVR microcontroller chip memory such as EEPROM.

To continue with AVRDUDE here are some steps to follow:

Step 1: First we must download AVRDUDE. Download it from the official site. Click here to download it:

Step 2: After downloading, complete its installation. First, open the download folder. Extract AVRDUDE file:

Copy these files and create a new folder inside C:\ drive with the name AVRDUDE. Paste these files there.

Step 3: After pasting the files to this folder, it will look like this C:\AVRDUDE:

Step 4: Open My PC, right click and select Properties:

Step 5: Click Advanced system settings:

Step 6: New window will open, click the Environment Variables.

Step 7: Select Path, and click Edit to add a new path.

Step 8: Click New, then paste the path copied earlier in this window. Then select OK:

Step 9: We have completed AVRDUDE installation and now open Command Prompt by typing cmd in the Windows search bar.

Step 10: Now, type AVRDUDE to check if it is installed properly or not. It will show some command options if AVRDUDE is installed successfully.

Step 11: As our AVRDUDE is installed, connect the Arduino with the PC.

Note: Remember to check the COM port before going forward.

Step 12: Our hardware is ready. Now upload a program on the Arduino board.

As an example, open the Blink LED program. Compile and upload this program into an Arduino board.

Step 13: To read Code from Arduino type command below in Command Prompt.

AVRDUDE -p atmega328p -c arduino -P com4 -U flash:r:board.hex:i

Sketch which is stored in flash memory of the board will be read by PC and it will create a new file with the name board.hex on the computer.

To write any code to Arduino just change r to w:

Here:

  • -c is the platform
  • -p is the port at which Arduino board is attached
  • -u memory flash type
  • r to read Arduino code
  • w to write code to Arduino
  • i to format file into Hex

How to Access the Generated Hex File

To Access output hex file follow these steps:

Step 1: First open command prompt by typing cmd in Windows search bar:

Step 2: Type the following command to access the output hex file of Arduino code.

board.hex

Step 3: After typing the “board.hex” in the command prompt a pop up will appear. Select Notepad:

The hex file in the Notepad will look like this:

How to Program Arduino with a Hex File

To program another Arduino with the extracted hex file, follow the below-mentioned procedure:

Step 1: Connect Arduino board to PC from which we are going to read code.

Step 2: Open Command Prompt, type following command:

AVRDUDE -p atmega328p -c arduino -P com2 -U flash:r:board.hex:i

Step 3: AVRDUDE will read code from Arduino memory and generate an output file.

Step 4: To write a HEX file into any other Arduino. First connect the second Arduino to PC and type write command in Command Prompt. Don’t forget to read the COM Port of the Arduino which is going to be programmed:

AVRDUDE -p atmega328p -c arduino -P com4 -U flash:w:board.hex:i

Note: Always remember to check the COM port of the second Arduino before writing code otherwise errors will appear Board not recognized.

Step 5: Now verify output of the second Arduino.

Conclusion

Arduino uses AVR microcontrollers which use assembly language to generate output bases upon provided input. Once we upload any code in the Arduino IDE, the compiler converts our C/C++ language code into a HEX file. HEX files come in handy when we must upload the same code into multiple Arduino boards. It also allows third parties to upload programs into Arduino without using IDE. Here in this article, we have covered all the steps needed to generate a hex file from programmed Arduino and upload it to any other using AVRDUDE.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.