Tricking Arduino IDE into Flashing Pre-Compiled Binaries

2021.01.25 | Yuki Rea

This is not meant to be an official tutorial, just the story of a quick workaround I had to come up with when nothing else would flash my firmware. It’s a niche problem, though one I needed to solve on the spot.

I was helping a friend build and program a custom macro keypad using QMK firmware on an Atmel ATmega32U4 microcontroller. The chip was on a knock-off Arduino board with what appeared to be a non-standard bootloader. I didn't have a Linux machine on me, and we needed to get the compiled firmware onto the board that same day on Windows.

AVRDUDE, Teensy Loader, QMK Toolbox, and every other flashing utility we tried failed. We kept running into errors, corrupted flashes, or tools failing to recognize the ATmega32U4 entirely. The only thing that worked was opening a sketch in the Arduino IDE and uploading it from there. That proved the board itself was fine, we just needed to get our custom QMK firmware onto it instead of an Arduino sketch.

That sparked an idea: what if I could trick the Arduino IDE into flashing my pre-compiled .hex file for me? I was doubtful, since copying the exact AVRDUDE command from the IDE's console and swapping in my own filename still failed every time. Instead, I had to intercept the file mid-upload.

How I Did It

[1] Find the temporary filename and build directory:

First, I compiled a sketch in the Arduino IDE and checked the console output to see where it drops its compiled .hex file and the exact name it uses.

[2] Stage the custom firmware:

I renamed my compiled QMK .hex file to match the exact filename the Arduino IDE generates and kept it in a separate folder. I learned that setting the IDE's target file to read-only beforehand doesn't work, Arduino throws an error and aborts if it can't overwrite the file during compilation. We have to allow the Arduino IDE to compile and write the file, preform verification checks, then replace the file right before the Arduino IDE starts flashing the file.

[3] Start the upload:

Hit Upload in the Arduino IDE to start compilation of the default Arduino sketch.

[4] Swap the file mid-process:

I watched the console output closely. The instant the IDE finished compiling and wrote its .hex file to the temporary folder, I quickly copied my renamed .hex file over it before the flashing step began. There was only milliseconds of time to get this right, if you need to try this yourself, I suggest creating a script to watch the file for modification and replace it as soon as the file is modified.

[5] Let the IDE flash it:

Because the swap happened right in that narrow window, the Arduino IDE ran its upload sequence and flashed my custom firmware onto the MCU.


To my surprise, it worked once I got the timing correct. Even better, once I got my firmware onto the board, QMK Toolbox started flashing the microcontroller normally, though standalone AVRDUDE commands still wouldn't work without using this Arduino IDE trick.