Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I made a sketch, but then I lost it. However, I uploaded it to the Arduino before losing it. Is there any way I can get it back?

share|improve this question
3  
electronics.stackexchange.com/questions/28211/… but as others say, this only spits out .hex files, not the originating C source. –  jippie Feb 14 '14 at 0:34
    
Cross site dupe: reverseengineering.stackexchange.com/q/125/189 –  AsheeshR Feb 14 '14 at 1:14

5 Answers 5

up vote 16 down vote accepted

It should be possible as long as the security bit isn't set. This question was asked on EE a while back.

Is it possible to extract code from an arduino board?

But you won't get the Arduino code you wrote back. The code is compiled into assembly and you'll have to convert that back to C yourself.

share|improve this answer

This answer doesn't directly answer the question, but still will result in the same end result.

The Arduino IDE uses temporary directories to store build files, including the original sketch as well as the HEX and intermediate files.

On a Mac, these are /var/folders by default, and on a Windows machine Local Settings\Temp\ (which depending on the version of Windows could be in several places).

I've found that especially in Windows, these build files don't get deleted when you close the sketch or IDE, so may exist on your machine for far longer than you would think.

Also, by default, Crashplan, Backblaze and Time Machine back-up these locations, so even if they have been deleted, they may still be in a backup.

The advantage here is that you will recover C code rather than ASM.

share|improve this answer
    
This is the best solution IMHO. Very creative, too... –  dda Jul 3 at 5:10

You can download the hex dump of the file, but there is no easy way to get all the C++ code and comments. If you can understand assembly, you could recreate the program, but that would take a while. There are dissasemblers available for some devices, i don't know about arduino.

share|improve this answer
3  
There are no disassemblers for arduino, because it is not a MCU type. There are multiple ATmega disassemblers (google atmega disassembler). –  Connor Wolf Feb 24 '14 at 4:28
    
If you have the arduino envitonment, you already have avr-objdump as it comes with avr-gcc. –  Chris Stratton Jan 21 at 2:03

I think there is another way to do this, without extracting the code from the Arduino board. Remenber the date you create the sketch, and look for it in C:\Users\User\AppData\Local\Temp\ (For example : C:\Users\User\AppData\Local\Temp\untitled4390292704786567977.tmp\sketch_jan19a ). You could find your unsaved code unless it was deleted by some "freeing space program". Good luck!

share|improve this answer

Unless your objective is to simply copy the identical code to another device, the simple answer is: no

As has been pointed out in other answers, you can turn the hex code on the device back into assembler code.

For example (once you have copied the code from the device back onto your disk):

avr-objdump -j .sec1 -d -m avr5 yourFileHere.hex

Run on one of the bootloader files I get these results (in part):

00007e00 <.sec1>:
    7e00:       11 24           eor     r1, r1
    7e02:       84 b7           in      r24, 0x34       ; 52
    7e04:       14 be           out     0x34, r1        ; 52
    7e06:       81 ff           sbrs    r24, 1
    7e08:       f0 d0           rcall   .+480           ;  0x7fea
    7e0a:       85 e0           ldi     r24, 0x05       ; 5
    7e0c:       80 93 81 00     sts     0x0081, r24
    7e10:       82 e0           ldi     r24, 0x02       ; 2
    7e12:       80 93 c0 00     sts     0x00C0, r24
    7e16:       88 e1           ldi     r24, 0x18       ; 24
    7e18:       80 93 c1 00     sts     0x00C1, r24
    7e1c:       86 e0           ldi     r24, 0x06       ; 6
    7e1e:       80 93 c2 00     sts     0x00C2, r24
    7e22:       80 e1           ldi     r24, 0x10       ; 16
    7e24:       80 93 c4 00     sts     0x00C4, r24
    7e28:       8e e0           ldi     r24, 0x0E       ; 14
    7e2a:       c9 d0           rcall   .+402           ;  0x7fbe
    7e2c:       25 9a           sbi     0x04, 5 ; 4

As you can see, not particularly helpful for reproducing what your sketch did. There are likely to be one of two scenarios:

  • Your sketch was short, in which case you might, after weeks of work, turn the assembler code back into C
  • Your sketch was long, in which case it would be virtually impossible to turn it back into C

In the "short sketch" case, you are better off just rewriting from scratch. That would be faster, almost certainly. In the "long sketch" case - it just isn't worth it. None of the variable names are preserved, and the way the compiler optimizes code, even the structure of the code would be hard to determine.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.