How-to read and write a HEX file from Arduino/ATMEGA328P

When things go wrong with an Arduino sketch

Maybe you recognize this one: When working on hobby projects, and most likely multiple ones, it can take a long time for a project reaches its final stage. And well some projects don’t seem to end at all.

One of the projects I’m working on is a frequency counter. And this is just one of those projects where I work on for a couple of weeks, and then it ends up collecting dust. And after a long period of time I start working on it again.

The downside is that after a long periode of time (in this case¬† a couple of years) you can’t remember everything what is done during the project. Especially when it comes down to software. I’m not a full-time developer so setting up a git hub repository is not a “natural thing”.

And that caused an interesting situation: I finally came around to develop PCB’s for the frequency counter. And of course the first version of the PCB had some faults in it. So I started to fix those faults, and made a second version of the PCB main board.

Once I soldered the second version I uploaded the sketch to the ATMEGA-328P which I’m using to drive the display and some other things.

After days of troubleshooting I finally figured out it was the software which caused problems. The sketches I had didn’t work. So I only got a ATMEGA-328P which I took from the bread-board, and that ATMEGA-328P is working fine.

I didn’t want to reverse engineer the complex sketch, so I figured: If I can take the binary code from the original ATMEGA-328P and then upload the binary file to a new ATMEGA-328P I can at least go further with test the hardware.


Getting the HEX file from a existing ATMEGA-328

Since I use a Imac (running Mojave) the command to read the hex file from an ATMEGA-328p:

/Applications/ -C ./Java/hardware/tools/avr/etc/avrdude.conf -v -v -v -v -pm328p -c arduino -P/dev/cu.usbmodem14401 -D -Uflash:r:/~/freq_cnt.hex:i

To write the hex file:

/Applications/ -C/Applications/ -v -patmega328p -carduino -P/dev/cu.usbmodem14401 -b115200 -D -Uflash:w:/~/freq_cnt.hex:i

When using the commands above, make sure to change the USB device ( the part after the “-P” flag. And also the place to save or read the HEX file from.

Leave a Reply

Your email address will not be published. Required fields are marked *