How to get sketch off Arduino

Nigel asks:

I have an Arduino that has a sketch that has been uploaded to it and it performs its task fine.

The original sketch saved on the PC was then lost.

Is it possible for me to extract the sketch from the Arduino or copy it onto another similar Arduino?

I don’t need the code back I just need to be able to clone the Arduino.

I have an Arduino Uno R3.

Hey Nigel,

Thanks for your question.

You can get a hex dump of the program but not the original source code.
You can then clone the hex dump onto other units.

To do this you’ll need to use avrdude.

Extract sketch from Arduino (as hex dump)

Windows Example

avrdude -C "C:\Program Files (x86)\arduino-00XXX\hardware\tools\avr\etc\avrdude.conf" -v -v -v -v -p atmega328p -c stk500 -U flash:r:"c:/arduino.hex":r -P\\.\COM5 -b57600
Naturally swap ut the arduino-00XXX for your Arduino folder and COM5 for your com port.

If you’re on Windows, ATMEL’s AVR Studio apparently has a nice gui for doing this (I haven’t used it yet).

Mac Example

~/arduino-00XXX/hardware/tools/avrdude \ -C~/arduino-00XXX/hardware/tools/avrdude.conf -v -v -v -v \ -pm328p -cstk500v2 -P/dev/ttyUSB1 -D -Uflash:r:/tmp/arduino.hex:i

#To upload the hex dump to another Arduino use:**

Windows Example

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

Mac Example
avrdude -U flash:w:[put-hex-file-path-here]:i -C avrdude.conf -v -p atmega328 -b 115200 -c stk500v2 -P [put-device-path-here]

Cheers,

Marcus

Also, once a hex file is read from an Arduino, there are experts who can write a matching sketch. This is sometimes needed; like when a change has to be made to how the sketch works. It can be reasonably costly though, and can scale with the size of the hex file. Most of the time it is easier to write a sketch from scratch in order to change it.