60

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?

2

7 Answers 7

43

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.

1
26

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 in /var/folders by default, and on a Windows machine they are in …\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 they 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.

4
  • 4
    This is the best solution IMHO. Very creative, too...
    – dda
    Jul 3, 2015 at 5:10
  • 1
    You may find this in /tmp/ directories on Linux (and even Mac). c.f. arduino.cc/en/Hacking/BuildProcess
    – pd12
    Oct 10, 2016 at 13:32
  • Comment echo from the accepted answer: Reading back the raw hex: forum.arduino.cc/index.php?topic=6150.0 and Automatic Disassembly: forum.arduino.cc/index.php/topic,46843.0.html
    – SDsolar
    Apr 13, 2017 at 6:57
  • I assumed you checked the trash. I use Linux mint and my last program was here: "/tmp/arduino_build_699749/preproc/ctags_target_for_gcc_minus_e.cpp" That was the original source file I compiled a few days back. Note I have not compiled anything since. I found the original /tmp/arduino_build_699749/sketch/myfile.ino.ccp. This probably will not help you but it probably help others. Remember go after it before compiling anything else.
    – Gil
    Oct 29, 2021 at 1:23
6

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.

3
6

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!

6

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.

3

Download:

avrdude \
    -c <programmer type> \
    -P <connection port> \
    -b <override RS-232 baudrate> \
    -p <partno, AVR device> \
    -U <memtype>:r:file_name[:format]

Upload:

Use w (write) instead of r (read) for the -U option.

More details at manual - avrdude Option-Descriptions.


Example:

Download UNO v3 (ATmega328) flash ROM (bootloader) given port /dev/ttyACM0 as Intel HEX:

avrdude -c arduino -P /dev/ttyACM0 -b 115200 -p m328p -U flash:r:SaveFlash.hex:i

If used directly from from the arduino IDE archive also add:

-C path-to/arduino-V/hardware/tools/avr/etc/avrdude.conf
0

Another approach might be to treat the program onboard the Arduino as a black-box exercise. That is, run the program and take careful notes of what happens (via Serial Monitor or whatever). If, in fact there is any output that you can observe, make it a task to create your own new code to emulate what the program DOES. This won't be productive unless the original programmer liberally included Serial.print() statements throughout the code, something I STRONGLY RECOMMEND as a programming habit to develop. Doing so, facilitates just the exercise I am suggesting. (Ask me why I developed that habit for everything I have coded for the past 30 years.)

Unfortunately, most people sprinkle a few comments here and there, which is helpful 5 years later if you have the source code. Personally, I comment EVERY LINE of source code. But comments in the source don't help later in this sort of situation, as the compiler doesn't put them in the machine code it generates. (Perhaps that might be a valuable option for the compiler. Yes, it takes bytes, but how valuable might it be years later for an important project?)

My two cents worth, which probably is worth the price the reader of this post paid to see it!

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