Downloading/Reading sketches from the Arduino?

I have created an RGB LED light program a while ago and I lost the sketch, is there a way to read the ATMega328 on my Arduino and extract the code so I can upload it to another Arduino?

I am downloading AVR Studio from Amtel thinking it will detect the Arduino and allow me to get into the chip, any suggestions?

At best, you can get the hex/binary - but not the source code...

extract the code so I can upload it to another Arduino?

Have you lost the original source code? The answer to this sort of question is normally a slightly qualified no.

so I can upload it to another Arduino?

if you press the [compile-upload] button with the shift pressed you get information how a sketch get uploaded (commands etc). If you are able to extract the hexcode from one Arduino this verbose output might give some clues how to upload the hexfile. Check AVRDUDE: 1 Introduction


update:
Just read the page myself and found on AVRDUDE: 2.2 Programmers accepting extended parameters the option -U memtype:op:filename[:format] (options are case sensitive!!)

The [b]op [/b]field specifies what operation to perform:
r   read the specified device memory and write to the specified file
w   read the specified file and write it to the specified device memory

So one can read and write from a device ( I expect reading will produce a 32K image, even if the original sketch was less)

and

The [b]format [/b]field is optional and contains the format of the file to read or write. Possible values are:
...
r    raw binary; little-endian byte order, in the case of the flash ROM data
...

So it must be possible to download a raw binary from one device and upload it to another one. You must also have the port right and other flags, so read it more carefully than I did. As Cr0sh said you wil not get the sourcecode but this thread http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241117308 is about disassembling the generated hexcode so you may get a faint echo of the original source

PLease let us know if you succeeded as this is a way to quick multiply a busload of Arduinos with the same sketch. Very interesting.

Rob


update
Avrdude can be found at ~\arduino-0021\hardware\tools\avr\doc\avrdude

Too interesting, I tried myself the first step: I changed dir to C:\Program Files (x86)\arduino-0021\hardware\tools\avr\bin>
and called:

avrdude -C "C:\Program Files (x86)\arduino-0021\hardware\tools\avr\etc\avrdude.conf" -v -v -v -v -p atmega328p -c stk500 -U flash:r:"c:/arduino.bin":r -P\\.\COM5 -b57600

And yes!, a nice c:\arduino\bin appeared. 32 K in size. So the download works! yippy! Now try to upload the arduino.bin file again.

avrdude -C "C:\Program Files (x86)\arduino-0021\hardware\tools\avr\etc\avrdude.conf" -v -v -v -v -p atmega328p -c stk500 -U flash:r:"c:/arduino.bin":w -P\\.\COM5 -b57600

The application still worked - could see that with the serial monitor! However my IDE now gets a failure on avrdude when I try to upload a new sketch. Think I need to restart my PC :frowning: because restaring the IDE did not solve it. I'll be back :slight_smile:


rebooting didn't help either. :frowning: => will be a reinstall of the IDE.

Sofar no guarantee ...


The application still worked - could see that with the serial monitor! However my IDE now gets a failure on avrdude when I try to upload a new sketch. Think I need to restart my PC because restaring the IDE did not solve it. I'll be back

That's because you likely blew away your bootloader uploading the binary - so you'll need to find a bootloader binary for your board, and upload that again so you can upload sketches with the Arduino IDE (if I am understanding you correctly?)...

(moved to make the thread more readable)
It works again without the reinstall. Did a check on the directory

C:\Program Files (x86)\arduino-0021\hardware\tools\avr\bin>

and somehow a file avrdude was made of 0 bytes (besides the avrdude.exe) , somewhere I screwed up I did some avrdude calls that failed. Maybe that created the new file. As the IDE calls avrdude without extension it tried to start the wrong file and failed. After removeing these zero bytes fiel the IDE worked again. Yes!

@Cr0sh, wrt the bootloader, think it is in the 32K file(?). as all seems to work again (pffffh) :slight_smile:

Next step is try to disassemble the 32K file

Rob

Yeah I lost the original code, it got mixed up in my dir with a bunch of versions of the file... I mean it's not a huge loss because it's simple RGB LED code but I had the colors perfect for what I was doing...

Thanks Rob, I will try extracting and transfering to another Arduino and see how far I get... If I can get it on another Arduino that would be great.

Thanks everyone, I will post my results after the weekend.

Kyle

About disassembly: Tried various formats of the download with avr-objdump but no luck sofar. (end of experiment)