Hex File read error in arduino mega

Hello Everyone,

i uploaded my code in arduino while back but now i deleted my firmware i did not find my source code and i want my arduino uploaded code.
I have USBasp programmer with me and i have all the required components for ICSP programming.
also my Arduino mega fuse bits and lok bitsare

L=0xFF
H=0xD9
E=0xFD
LB=0xFF

I do some research about how to get hex file from arduino i tried different methods but i am filed to get hex file from arduino.
So any one can solution for getting hex file from arduino using USBasp programmer???

Can't you use the serial chip onboard?

Use this command.

avrdude install path -C "Arduino install path \hardware\tools\avr\etc\avrdude.conf" -v -p atmega mega chip name -c arduino -P COM port -b 115200 -D -U flash:r:download.hex:i

Where can i send this command ???

Here is a link that describes the process.
read flash from chip

1 Like

You run it from a command line / bash shell. Note: the Mega uses wiring for the -c parameter, not arduino (corrected below). The full command for my Windows system

"C:\Users\Wim\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\Wim\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v -patmega2560 -cwiring "-PCOM3" -b115200 -D "-Uflash:r:download.hex:i"

Adjust the username in above !!

Note if you're running an older IDE (1.8.x) where the AVR board package was not updated, you can find the avrdude command in C:\Program Files (x86)\Arduino\hardware\tools\avr\bin and avrdude.conf in C:\Program Files (x86)\Arduino\hardware\tools\avr\etc

1 Like

Now I am able to Read hex file from my arduino but problem is that my hex file not that where I want I uploade that hex file in another arduino and that arduino board is not work like current board.
I also found that arduino mega flash memory is 256 KB but my hex file size is around 615 KB that could be issue?

Hi @EDExpert

I'm not sure I understood correctly what you mean by this. Please provide a more detailed description to help me to understand it.

The hex file is a human readable interpretation (in a specific format) of the binary that goes in the flash memory. It will at least be twice as big (because is byte is represented in ascii) as the actual binary data that you did read back from the Mega; add some additional stuff and you get to that 600-something bytes. See Intel HEX - Wikipedia.

If you have already uploaded your code to the Arduino board but no longer have access to the original source code, you can still retrieve the hex file from the Arduino board using the USBasp programmer.
You can do the same with another Arduino too: How to Read and Extract HEX File from Arduino Board And Upload the HEX File to Another Arduino - YouTube

A USBasp programmer is not required because the bootloader has flash read capabilities in addition to write. You can read the hex file even when the board is connected to your computer with a USB cable as usual. The only time you would need an ISP programmer is if there was no bootloader on the microcontroller (as is the case if you uploaded the sketch via Upload Using Programmer instead of doing a normal upload).

The procedure is described in the post at the link shared by theeccentricgenius in post #5. My impression is that @EDExpert has already been successful in reading the hex file.

1 Like

Here is a simple (GUI) program: GA Flasher. That can not only read the sketch (hex), but also write it back without the need for an external programmer https://gra-afch.com/download/46108/

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