Downloading existing code

Good morning all,
I have been tasked with reverse engineering an semi automated hole punching device that my company has been using the past 4 years or so. It consists of an UNO r3 arduino with an adafruit v2.3 motor shield. I am running a single stepper motor, a single servo motor, and a basic 2 x 16 rgb display. 4 on/off buttons control the unit. one for stepper advances to the right, one for stepper advances to the left. one for operating the servo, and one for disengaging the stepper which allows it to manually be moved into the starting position.
Unfortunately, the original designer left next to no information about the sketch used. It is also unfortunate that I have absolutely no experience with writing sketches.
What I would like to do ( if possible) is download the existing code from the working unit and upload it into the new unit. Everything from the older unit has been copied to create the new one.
I fully plan to dive into the sketch writing but simply do not have the time. If there is any information about how to download the sketch I would be extremely greatful beyond belief.
When powered up the Blink sketch runs as it should.

Charliet13

I have never done it, but I believe that the Avrdude utility can be used to pull the code off one Arduino and then put it on another.

It comes with the IDE which I assume you used to install blink.

Why reverse engineer the old one? Do they want to change its functions?
Starting from the beginning using basic stepper tutorials, example code looks ok to me.

You should be able to copy the code from one arduino to another using avrdude on the command line, but there is no function within the IDE to do this. DO NOT do this on the working unit until you have successfully accomplished it on a couple of test UNO's, a mistake could erase the original code.

The UNO does not contain the sketch, only the compiled machine language code, so reverse engineering it back to a readable sketch is very difficult.

You can't pull the source code from the Arduino. You can pull the executable from the Arduino.

That will give you a binary or hex file that you can disassemble. After that, you're basically on your own to understand it; there might (!) be an utility to convert it back to C but it will definitely not look like a sketch.

Thanks for the timely response. Unfortunately, its looking like my best bet is to dig in and learn this stuff. Its a very simple operation where one button moves the stepper a set distance ( .10") CW, another does the same thing CCW. A third button engages the servo in a CW/CCW motion as long as button is depressed. A forth button releases the stepper so it can be manually moved to an arbitrary starting position. The video screen logs the number of moves +1, +2, +3, etc and -3, -2, -1. Shouldn't be too hard to figure out, right? Thanks again.

It's likely more effective in the long run to recreate the code, so that when some new functionality is required, you'll be able to add it.

But if you are pressed for time and simply want to replicate the existing device, you can do so as noted above. Just be sure that you heed david_2018's warnings.

1 Like

Split the project into small parts. Reading a button is one, making a stepper move one step is another. Operating the display is another. Piece by piece You advance towards the wanted system and the functions You told look suitable for a beginner.

No changes at all. I begged em to just let me design a new one but you know "If it aint broke dont fix it". It's basically ( very basic) a hole punching fixture. stepper moves a platform CW/CCW. whem in position an arbor press is manually used to punch a .040" hole in thin walled heat shrink. If needed, the servo gives it a cutting motion. In theory the stepper motor is all that really has to do it's thing. None of the techs ever look at the video counter anyway. The servo is "iffy" in my opinion, too. Is there a specific stepper motor sketch you could recommend to get me started? The main problem I have is a total lack of programming knowledge and understanding. It shames me to say it, but there it is.

Buy one of the $40 Arduino starter kits on amazon/eBay that include a stepper motor/driver and start working through the tutorials.

Blink some LED’s with pushbuttons and work your way through it until you end up being able to control a servo motor and then a stepper motor.

Learning something new is like the old proverb “how do you eat an elephant?” Simple. One bite at a time.

Everybody is born as a newbie, a beginner! No shame in that! It's much better You told that instead if swinging terminology and words You don't know the meaning of.
Do some tries, tasting some example code available in the IDE, topics like "stepper basics" etc.

First, we’re going to do a dummy upload in the Arduino IDE in order to get it to help us generate the avrdude command used to read the program from the Arduino board:

  1. Plug your Arduino board that contains the program you want to read into your computer.
  2. Select your board from the Arduino IDE’s Tools > Board menu.
  3. Select your board’s port from the Tools > Port menu.
  4. VERY IMPORTANT: Unplug your board from your computer.
  5. File > Preferences
  6. Check the box next to "Show verbose output during > upload".
  7. Click the OK button.
  8. Sketch > Upload
  9. Wait for the upload to fail.
  10. Scroll up the black console window at the bottom of the Arduino IDE window until you see the avrdude command that was generated for the upload. It will look something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
  11. Select the full text of the upload command.
  12. Press Ctrl+C. This will copy the upload command to the clipboard.
  13. Next, you need to modify the upload command to read the hex file out of your Arduino board:
  14. Start a text editor program.
  15. In the text editor window, press Ctrl+V. This will paste the command into the text editor.
  16. The end of the command will look something like this:
    -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
    That is the part of the command that tells it to write.
    Replace that part of the command with the command that tells AVRDUDE to read:
    -Uflash:r:readfile.hex:i
    
    That will cause the read file to be named "readfile.hex", which will be saved to whichever folder you run the command from. So now the full command looks something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:r:readfile.hex:i
    
  17. If the paths in the command contain spaces, wrap the paths in quotes.
  18. Plug your Arduino board into your computer.
  19. Copy and paste the command from the text editor to the command line
  20. If you are using to an ATmega32U4-based board (e.g., Leonardo, Micro, Yun), you’ll need to press and release the reset button on the board immediately after running the command. If you are using a Pro Micro, use a wire to momentarily connect the RST and GND pins. If you are using a Yun, press and release the "32U4 RST" button quickly twice. Wait about a second after resetting the board before running the command.
  21. Run the command.
  22. Wait for the command to finish successfully.

Writing the file to a board

You can follow a similar procedure to write the file to another Arduino board. Remember that this file was compiled specifically for the Arduino board you read it from. You can’t use it with an Arduino board that has a different configuration. For example, if you read it from an Uno, it is compiled for an ATmega328P running at 16 MHz, and won't work correctly on a board with a different microcontroller or clock speed.

Generate model command

First, we’re going to do a dummy upload in the Arduino IDE in order to get it to help us generate the avrdude command used to write the program to the Arduino board.

Note: It doesn't matter whether the upload will succeed or fail. You get the command either way.

  1. Plug the Arduino board you want to write to into your computer.
  2. Select your board from the Arduino IDE’s Tools > Board menu.
  3. Select your board’s port from the Tools > Port menu.
  4. Select File > Preferences from the Arduino IDE's menus.
  5. Check the box next to "Show verbose output during: [] upload".
  6. Click the OK button.
  7. Select Sketch > Upload from the Arduino IDE's menus.
  8. Wait for the upload to finish/fail.
  9. Scroll up the black console window at the bottom of the Arduino IDE window until you see the avrdude command that was generated for the upload. It will look something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
  10. Select the full text of the upload command.
  11. Press Ctrl+C. This will copy the upload command to the clipboard.

Customize command

Next, you need to modify the upload command to write the hex file you read from your other Arduino board:

  1. Start a text editor program.
  2. In the text editor window, press Ctrl+V. This will paste the command into the text editor. The end of the command will look something like this:
    -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_91864/sketch_jan22b.ino.hex:i
    
    That is the part of the command that tells it to write.
  3. Replace the filename in that part of the command with the name of the file you read:
    -Uflash:w:readfile.hex:i
    
    So now the full command looks something like this:
    E:\Arduino\hardware\tools\avr\avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM17 -b115200 -D -Uflash:w:readfile.hex:i
    
    If the paths in the command contain spaces, wrap the paths in quotes.

Run command

Now the write command is all prepared. It's time to run it!

  1. Plug the Arduino board you want to write to into your computer.
  2. Select the full text of the write command in your text editor.
  3. Press Ctrl+C. This will copy the write command to the clipboard.
  4. Open a command line terminal. On Windows, you can use the Run dialog, PowerShell, or cmd.
  5. In the command line, press Ctrl+V. This will paste the avrdude command.
  6. If you are uploading to an ATmega32U4-based board (e.g., Leonardo, Arduino Micro), you’ll need to press and release the reset button on the board before running the command. If you are using a Pro Micro, use a wire to momentarily connect the RST and GND pins. If you are using a Yun, press and release the "32U4 RST" button quickly twice. Wait about one second before running the command.
  7. Press Enter to run the command.
  8. Wait for the writing process to finish successfully.

The adafruit motor shield library has examples for driving a stepper motor, that will give you a working example.

It is not that difficult to transfer the code from one arduino to another, provided the original programmer did not get paranoid and disable reading from the flash memory. If you set the Arduino IDE preferences to show verbose output during compile and upload, then upload any code to an UNO, immediately after the text that shows how much memory the sketch and global variables use will be the command for avrdude to upload the sketch hex file. Copy that line, and it can be modified to read from flash memory and save to a file, instead of writing the sketch file to flash memory. Once you have that, you then modify the command again to write your saved file to the 2nd UNO. DO NOT, and I repeat, DO NOT try this with your working unit until you have done it successfully between a couple of test UNO boards - making a mistake can erase the original code.

I see pert has already posted the instructions while I was typing this.

Thanks for the info. I believe that's going to have to happen eventually. I'm wondering how long it would actually take to eat an elephant on bit at a time and I'm hopeful this comes together quicker.

Thanks

Many thanks to you and pert. Im going to give this a go. I'm ordering the "test boards" this afternoon.

Thanks again guys. And thanks for making me feel welcome!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.