# How to extract code from Arduino

#### Killerbee65

Joined May 15, 2017
248

#### MrChips

Joined Oct 2, 2009
22,912

Lesson to learn: always backup your work.

#### ZCochran98

Joined Jul 24, 2018
125
This link and this link have a suggestion as to a way you might be able to get the Arduino to dump its 32kB flash memory (where the program is stored). They seem to be "simple" solutions, in that they consist of a couple command-line operations (though you may have to set up AVRDude to work outside of the Arduino IDE - it's the compiler/debugger program).

HOWEVER: as a warning, it will very likely be stored as the numeric hex code (opcodes) equivalent of assembly (in other words, you'll just get a text file full of lines that read something like "0x100102BA," as a random example), not the original C/C++ code. It would certainly be a decent challenge to hand-decompile it (and an opportunity to learn assembly along the way...).

I agree with @MrChips : assuming they haven't reformated the drives on the laptops, it would be much easier to request access to the code. Remember to back up future work - it's definitely a lifesaver (I even had to use my own recently, as my previous hard drive gave out after 5 years of constant use and jostling).

Good luck with retrieving your program!

#### Killerbee65

Joined May 15, 2017
248

Lesson to learn: always backup your work.
Tried that the day after turning in the computer but my school was very strict and basically didn't want to invest in an engineering course until my last year. That being said, not even my instructor had access to these computers. And yea too bad the lesson was learned the hard way.

#### MrChips

Joined Oct 2, 2009
22,912
When working on small projects the simple thing to do is email the code to yourself.

#### Killerbee65

Joined May 15, 2017
248
This link and this link have a suggestion as to a way you might be able to get the Arduino to dump its 32kB flash memory (where the program is stored). They seem to be "simple" solutions, in that they consist of a couple command-line operations (though you may have to set up AVRDude to work outside of the Arduino IDE - it's the compiler/debugger program).

HOWEVER: as a warning, it will very likely be stored as the numeric hex code (opcodes) equivalent of assembly (in other words, you'll just get a text file full of lines that read something like "0x100102BA," as a random example), not the original C/C++ code. It would certainly be a decent challenge to hand-decompile it (and an opportunity to learn assembly along the way...).

I agree with @MrChips : assuming they haven't reformated the drives on the laptops, it would be much easier to request access to the code. Remember to back up future work - it's definitely a lifesaver (I even had to use my own recently, as my previous hard drive gave out after 5 years of constant use and jostling).

Good luck with retrieving your program!
Doesn't the arduino ide translate C into assembly? if so, isn't there a possible way to reverse the code back into C?

#### MrChips

Joined Oct 2, 2009
22,912
It is possible to reverse engineer your hex code to ASM.
That is relatively straight forward.
It is possible to reverse engineer ASM to C but that is much more complicated. The resulting C code will not be anywhere close to your original code. All variable names, function names and code comments will not be there.

#### AlbertHall

Joined Jun 4, 2014
10,915
It would probably be easier to recreate the C code than to generate the C from the ASM.

#### dl324

Joined Mar 30, 2015
12,518
how I can somehow reclaim the code by extracting it straight from the Arduino board but most answers and questions were confusing, hard to understand seeing how these were experienced programmers asking and answering the question, and not exactly what I was looking for. Simply put I am very careful how I want to go through with this because if I lose the code then that would mean a whole lot of unnecessary work.
Read post #8 in this thread on the Arduino forum. As is typical, the first response by a high post count member was clueless, but someone later provided an appropriate answer.
The gist of the post:
[/URL]

#### Killerbee65

Joined May 15, 2017
248
I saw somewhere that you had to pull up the command prompt and enter the quote placed in post #9 but I don't know what to do after that.

#### ZCochran98

Joined Jul 24, 2018
125
If you use the first command in post 9, then the contents of the Arduino's flash memory is pasted directly into a file called "test.hex" (where, I'm not sure - probably the directory your CMD prompt is currently active in, or a folder in that directory called "hexfiles"). The hex file could then be opened with a hex editor, or, if you have the tool, something that can translate AVR's opcodes into assembly (Atmel studio, maybe?).

#### Killerbee65

Joined May 15, 2017
248
When I type in the command line above it gives me an error saying it wasn't recognized.

let me know if the attachment is hard to see.

#### ZCochran98

Joined Jul 24, 2018
125
AVRDude isn't set up in your PATH, so it won't recognize it as a command. The program "avrdude" is located (for me, anyway), in "C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\", so take a look there. If you find a program called "avrdude," add that directory to your PATH variables (look up how to do that if you don't know how - it's not too hard).

Edit: an alternative thing you can do, if you don't want to add anything to your PATH, is to, instead, type (exactly as you see it, including quote marks):

"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude" -c arduino -P com20 -p atmega328p -Uflash:r:d:\hexfiles\test.hex:i

This is assuming that avrdude on your machine is located in "C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\".

Also make sure to confirm that your Arduino is actually on COM20. It may be on COM3, COM4, or another serial port (check hardware manager or the Arduino IDE - it'll usually tell you which port the device is connected on)

Final edit: you may want to change the directory part of it. That's the "d:\hexfiles\test.hex:i" part (the ":i" indicates Intel hex, according to the thread). So, if you have a specific folder you want to save this hex file to (for example, in a folder called "Projects" in "My Documents", you'd replace d:\hexfiles\test.hex:i with "C:\Users\[username]\My Documents\Projects\test.hex:i", with the quotes included and your username substituted in appropriately).

Last edited:

#### Killerbee65

Joined May 15, 2017
248
So this popped up

not sure I did it right but other than that how do I find com20? i tried all the usb ports on my laptop but all of them say com5, which is weird considering the school laptop had different numbers for each port.

#### ZCochran98

Joined Jul 24, 2018
125
Try typing com5 instead of com20. In the example this originated from the original person had his Arduino connected to com20. Loosely-speaking, COM# just indicates a particular location of a serial communication or serial port. All your USB ports may say com5 because it's not related to a specific hardware device, but over a "port," for lack of a better term, agreed upon by your computer and the Arduino.

#### ZCochran98

Joined Jul 24, 2018
125
If it helps at all clarify what I was poorly explaining about COM ports, here's a Wikipedia summary on them.

#### Killerbee65

Joined May 15, 2017
248
If it helps at all clarify what I was poorly explaining about COM ports, here's a Wikipedia summary on them.
Thanks a lot, after changing the com20 to com5 the same error popped up. Currently looking at how to add the directory to the path see if that works.