Reliable program load to Pro Micro using bat file.

Hi All,

I recently bought some Pro Micros for the first time. (Fantastic! For software testing I needed to enter lots of data into a target computer that is in a VM and 2 remote desktop jumps from my PC. The Pro Micro did the job. Rather strange running a program on my PC talking serial to Pro Micro, sending mouse+keys back up the same wire to the same computer.)

It may have been my computer, but the Arduino IDE usually failed to load the Pro Micro. The automatic way failed, and I could never press the reset at the right time. So I looked at using a bat file and it works perfectly, 99/100 instead of 1/10. (The tutorial part about reloading the boot program says only 8 seconds to do something. I haven’t needed this yet but it’s probably easier with a bat file.)

Save the code below as e.g. Pro_Micro_Build.bat in a normal Arduino sketch directory. It gets the directory and filenames automatically. You have to check and maybe edit four things: board-type, com port, Arduino location, Avrdude location. It compiles the sketch, prompts you to reset the Pro Micro, then uses mode and find to wait for the port to appear. (Clunky but it works.) It uses Avrdude to load the board. (Command lifted from IDE console.) It’s slightly faster if you hold the reset and let go when prompted to reset.

I’ve seen a few posts about difficulties loading the Pro Micro so I hope this helps.

Richard.

@rem ------------------------------------------------------------------------------------------------------------------------
@rem Arduino build from command line.
@rem 24/07/2020 Modified for SparkFun Pro Micro
@rem
@rem This uses arduino_debug.exe to compile the sketch, then avrdude to load it to the Pro Micro.  When prompted, press
@rem the Pro Micro's reset button (that you connected).
@rem
@rem Put this file in the Arduino project folder (with MyProg.ino)
@rem 
@rem The board type is correct for the Pro-Micro but you'll need to edit the port, Arduino.exe location, Avrdude location.
@rem
@rem The port is the bootload port which appears in Device Manager only briefly after you reset the Pro-Micro. Connecting 
@rem reset twice quickly makes that port stay around for a few seconds longer.  Even with that, sometimes Windows is not 
@rem quick enough to show it.
@rem
@rem Arduino build: https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc
@Rem
@rem Pro micro: https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide
@rem
@rem Tested on Windows-XP and Windows-10.
@rem ------------------------------------------------------------------------------------------------------------------------

echo off
setlocal EnableDelayedExpansion
set "startTime=%time: =0%"

@rem ------ edited-settings... --------------------------------------------------------------------

@rem Arduino Board Type
@REM ==================
@rem SparkFun Pro Micro
set brd=SparkFun:avr:promicro:cpu=16MHzatmega32U4

@rem Port name
@rem =========
set prt=COM7

@rem Arduino software executable
@rem
@rem If there are spaces in the directory names, use dir/x in each to get the shortnames so 
@rem the path here doesn't have spaces in it.
@rem
set cmd=C:\Programs\Arduino\arduino_debug.exe

@rem Avrdude location
@rem ================
@rem Windows-XP (use short path to avoid spaces)
set avrdude=C:\DOCUME~1\RDL\LOCALS~1\APPLIC~1\ARDUIN~1\packages\arduino\tools\avrdude\6345AA~1.0-A
@rem Windows-10
set avrdude="C:\Programs\Arduino\hardware\tools\avr"
@rem ------- ... edited-settings ------------------------------------------------------------------



@rem ----------------------------------------------------------------------------------------------
@rem Project folder (and file name)
for /F "delims=\" %%A in ("%0%") do (
  set src=%%~nxA
)
set afl=C:%~sp0%src%.ino
echo %afl%
echo %brd% %prt%

@rem ----------------------------------------------------------------------------------------------
@rem Temporary files
set tmp=%temp%\Arduino\%src%
if not exist %tmp% mkdir %tmp%

@rem ----------------------------------------------------------------------------------------------
@rem Compile

@%cmd% --board %brd% --preserve-temp-files --verify --pref build.path=%tmp% %afl%

if %errorlevel% == 0 goto deliver
  echo "FAIL."
pause
exit /b

@rem ----------------------------------------------------------------------------------------------
:deliver
@rem get the hex file from the temporary folder
copy %tmp%\%src%.ino.hex .\
echo .
echo RESET THE PRO MICRO.
echo .
@rem use count to prevent infinite loop
set /a count=0

@rem ----------------------------------------------------------------------------------------------
@rem wait for the port to appear.
:loop
  mode > ports.lis
@rem mode side-effect makes Ard lamp flash! (what about other serial devices?)
findstr %prt% ports.lis
if %errorlevel% == 0 goto do_load
@rem 0 = not found, 1 = found

set /a count=%count%+1
echo Looking for %prt%  %count%

if %count% leq 100 goto loop


echo Failed to find %prt%
pause
goto done_or_not

:do_load

@rem ----------------------------------------------------------------------------------------------
@rem Load the hex file into the Pro Micro.
%avrdude%\bin\avrdude.exe -C%avrdude%\etc\AVRDUD~1.CON -v -patmega32u4 -cavr109 -P%prt% -b57600 -D -Uflash:w:%src%.ino.hex:i

goto postload
:postload       

set "endTime=%time: =0%"
@rem Get elapsed time:
set "end=!endTime:%time:~8,1%=%%100)*100+1!"  &  set "start=!startTime:%time:~8,1%=%%100)*100+1!"
set /A "elap=((((10!end:%time:~2,1%=%%100)*60+1!%%100)-((((10!start:%time:~2,1%=%%100)*60+1!%%100)"
@rem Convert elapsed time to HH:MM:SS:CC format:
set /A "cc=elap%%100+100,elap/=100,ss=elap%%60+100,elap/=60,mm=elap%%60+100,hh=elap/60+100"
echo Elapsed:  %hh:~1%%time:~2,1%%mm:~1%%time:~2,1%%ss:~1%%time:~8,1%%cc:~1%
@rem
pause done_or_not
exit /b
@rem ------------------------------------------------------------------------------------------------------------------------

This is awesome! Thank you for posting your solution, I am linking it directly in the 32u4 microcontroller board tutorials.

So when I try to run this it displays the error

error: stray '' in program

So I am pretty sure it is in the path name

set avrdude= “C:\Progra~2\Arduino\hardware\tools\avr"

Everything is fine until I put a "" in quotes and it changes it from blue to gray.

There is a forum here on how to change it but this does not work for the Arduino IDE.

https://askubuntu.com/questions/272741/ … -stray-200

Is there another way to declare the pathname?

I am running the Arduino IDE under Windows 7/64. I have a bricked 5V ProMicro, probably a victim of erroneous processor assignment when I tried to download to it. I tried to use @RichardDL’s batch file to unbrick the ProMicro, but without success. I ran it using the blink example, to minimize the chance that my app had somehow been the cause of the bricking. What I saw was

Any advice will be welcome.

That image (a screen grab) is pretty much unreadable on the forum screen. I’m sorry about that. It turns out that if you save the image out as a file, it can be read in photo apps. I thought of piping the output to an ASCII file, but then I wouldn’t know when to hit the time-sensitive reset.

Please see the [troubleshooting section in the hookup guide for help in recovering your Pro Micro.](Qwiic Pro Micro USB-C (ATmega32U4) Hookup Guide - SparkFun Learn)

I found the batch file through the Troubleshooting section, so its value has already been exhausted.

This was super helpful, thanks!

For anyone that gets stuck where I was, after updating the paths I kept running in to the error SparkFun package not found. I had to run arduino_debug.exe on it’s own without params, and repaste the Sparkfun package URL in to preferences for the app to see that I had the board files installed.

Hi! I’ve been banging my head against the wall on this problem for a while now and this post is the closest I think I’ve come to resolving my “bricked” ProMicro issue.

I’ve reached a point whereby I can successfully run Richard’s .bat code, but I run into the same problem every time: if I use any COM port other than 1, the code throws the following error:

avrdude.exe: ser_open(): can't open device "\\.\COM2": The system cannot find the file specified.

So I tried using COM 1 and the code doesn’t appear to wait for the port to appear. The loop is waiting for an error, and if it doesn’t find one, it moves on, correct?

:loop
  mode > ports.lis
@rem mode side-effect makes Ard lamp flash! (what about other serial devices?)
findstr %prt% ports.lis
if %errorlevel% == 0 goto do_load
@rem 0 = not found, 1 = found

When I run the code with COM1, there’s no pause after “RESET THE PRO MICRO”–it just goes right onto the next piece as if it detected the ProMicro. (If I change the loop to default to the error state, it runs through all 100 checks in about 2 seconds before spitting out the “Failed to find COM1” error and closing.)

I’m worried that I may have actually bricked my ProMicros…can anyone help me?

I ran into this problem with Linux and thought this script might help others. Simpler than Richard’s Windows example because it uses an existing HEX file (blink), but otherwise it works in the same way. With the bricked Pro Micro plugged in, the script is run. It loops waiting for the device /dev/ttyACM0 to appear, which happens when the Pro Micro’s RST pin is grounded twice in quick succession. As soon as the port is available, avrdude is run to install blink. After a successful install, the Arduino IDE can be used again.

#!/usr/bin/bash

# Linux Script for unbricking a Sparkfun Pro Micro
# Successfully downloading a program unbricks the bootloader but
# this is time sensitive. Grounding the reset pin twice in quick 
# succession wakes the bootloader up & opens the USB port for 8 seconds,
# which is usually not enough time to manually select the port
# and start an upload with the Arduino IDE.

# Needs a precompiled program such as Blink in HEX form, to find the hex:
# switch on verbose compile in Arduino IDE & check output to see
# where the HEX file is put, usually in /tmp.   
# Then copy the HEX file to a known place, see INOHEX below

# The script loops until PORT is detected and then immediately
# runs avrdude. The script grabs the port well before 8 seconds expires.

# After a program has been successfully installed the Arduino IDE
# should recognise the Pro Micro normally. 

INOHEX="$HOME/bin/proMicroBlink.ino.hex"
PORT="/dev/ttyACM0"

# Loop until port exists
while ! [ -e $PORT ]
do
	echo Reset Pro Micro twice or ctrl-c to exit
	sleep 0.33
done

# Got the port, do the download!
avrdude -v -patmega32u4 -cavr109 -P $PORT -b57600 -D -U flash:w:$INOHEX:i

Tested on Ubuntu 22.04 with a 5V Pro Micro bricked by downloading a sketch in 3.3V mode.

Dave

RichardDL:
Hi All,

I recently bought some Pro Micros for the first time. (Fantastic! For software testing I needed to enter lots of data into a target computer that is in a VM and 2 remote desktop jumps from my PC. The Pro Micro did the job. Rather strange running a program on my PC talking serial to Pro Micro, sending mouse+keys back up the same wire to the same computer.)

It may have been my computer, but the Arduino IDE usually failed to load the Pro Micro. The automatic way failed, and I could never press the reset at the right time. So I looked at using a bat file and it works perfectly, 99/100 instead of 1/10. (The tutorial part about reloading the boot program says only 8 seconds to do something. I haven’t needed this yet but it’s probably easier with a bat file.)

Save the code below as e.g. Pro_Micro_Build.bat in a normal Arduino sketch directory. It gets the directory and filenames automatically. You have to check and maybe edit four things: board-type, com port, Arduino location, Avrdude location. It compiles the sketch, prompts you to reset the Pro Micro, then uses mode and find to wait for the port to appear. (Clunky but it works.) It uses Avrdude to load the board. (Command lifted from IDE console.) It’s slightly faster if you hold the reset and let go when prompted to reset.

I’ve seen a few posts about difficulties loading the Pro Micro so I hope this helps.

Richard.

@rem ------------------------------------------------------------------------------------------------------------------------

@rem Arduino build from command line.
@rem 24/07/2020 Modified for SparkFun Pro Micro
@rem
@rem This uses arduino_debug.exe to compile the sketch, then avrdude to load it to the Pro Micro. When prompted, press
@rem the Pro Micro’s reset button (that you connected).
@rem
@rem Put this file in the Arduino project folder (with MyProg.ino)
@rem
@rem The board type is correct for the Pro-Micro but you’ll need to edit the port, Arduino.exe location, Avrdude location.
@rem
@rem The port is the bootload port which appears in Device Manager only briefly after you reset the Pro-Micro. Connecting
@rem reset twice quickly makes that port stay around for a few seconds longer. Even with that, sometimes Windows is not
@rem quick enough to show it.
@rem
@rem Arduino build: Arduino/build/shared/manpage.adoc at master · arduino/Arduino · GitHub
@Rem
@rem Pro micro: Pro Micro & Fio V3 Hookup Guide - SparkFun Learn
@rem
@rem Tested on Windows-XP and Windows-10.
@rem ------------------------------------------------------------------------------------------------------------------------

echo off
setlocal EnableDelayedExpansion
set “startTime=%time: =0%”

@rem ------ edited-settings… --------------------------------------------------------------------

@rem Arduino Board Type
@REM ==================
@rem SparkFun Pro Micro
set brd=SparkFun:avr:promicro:cpu=16MHzatmega32U4

@rem Port name
@rem =========
set prt=COM7

@rem Arduino software executable
@rem
@rem If there are spaces in the directory names, use dir/x in each to get the shortnames so
@rem the path here doesn’t have spaces in it.
@rem
set cmd=C:\Programs\Arduino\arduino_debug.exe

@rem Avrdude location
@rem ================
@rem Windows-XP (use short path to avoid spaces)
set avrdude=C:\DOCUME~1\RDL\LOCALS~1\APPLIC~1\ARDUIN~1\packages\arduino\tools\avrdude\6345AA~1.0-A
@rem Windows-10
set avrdude=“C:\Programs\Arduino\hardware\tools\avr”
@rem ------- … edited-settings ------------------------------------------------------------------

@rem ----------------------------------------------------------------------------------------------
@rem Project folder (and file name)
for /F “delims=" %%A in (”%0%") do (
set src=%%~nxA
)
set afl=C:%~sp0%src%.ino
echo %afl%
echo %brd% %prt%

@rem ----------------------------------------------------------------------------------------------
@rem Temporary files
set tmp=%temp%\Arduino%src%
if not exist %tmp% mkdir %tmp%

@rem ----------------------------------------------------------------------------------------------
@rem Compile

@%cmd% --board %brd% --preserve-temp-files --verify --pref build.path=%tmp% %afl%

if %errorlevel% == 0 goto deliver
echo “FAIL.”
pause
exit /b

@rem ----------------------------------------------------------------------------------------------
:deliver
@rem get the hex file from the temporary folder
copy %tmp%%src%.ino.hex .
echo .
echo RESET THE PRO MICRO.
echo .
@rem use count to prevent infinite loop
set /a count=0

@rem ----------------------------------------------------------------------------------------------
@rem wait for the port to appear.
:loop
mode > ports.lis
@rem mode side-effect makes Ard lamp flash! (what about other serial devices?)
findstr %prt% ports.lis
if %errorlevel% == 0 goto do_load
@rem 0 = not found, 1 = found

set /a count=%count%+1
echo Looking for %prt% %count%

if %count% leq 100 goto loop

echo Failed to find %prt%
pause
goto done_or_not

:do_load

@rem ----------------------------------------------------------------------------------------------
@rem Load the hex file into the Pro Micro.
%avrdude%\bin\avrdude.exe -C%avrdude%\etc\AVRDUD~1.CON -v -patmega32u4 -cavr109 -P%prt% -b57600 -D -Uflash:w:%src%.ino.hex:i

goto postload
:postload

set “endTime=%time: =0%”
@rem Get elapsed time:
set “end=!endTime:%time:~8,1%=%%100)*100+1!” & set “start=!startTime:%time:~8,1%=%%100)*100+1!”
set /A “elap=((((10!end:%time:~2,1%=%%100)*60+1!%%100)-((((10!start:%time:~2,1%=%%100)*60+1!%%100)”
@rem Convert elapsed time to HH:MM:SS:CC format:
set /A “cc=elap%%100+100,elap/=100,ss=elap%%60+100,elap/=60,mm=elap%%60+100,hh=elap/60+100”
echo Elapsed: %hh:~1%%time:~2,1%%mm:~1%%time:~2,1%%ss:~1%%time:~8,1%%cc:~1%
@rem
pause done_or_not
exit /b
@rem ------------------------------------------------------------------------------------------------------------------------

I registered for the forum just to say thanks. So, thanks :slight_smile:

I downloaded, configured and ran the batch file, but also ran into the problem that the “SparkFun” package couldn’t be found. Seeing that my directory in the hardware folder was “sparkfun” I changed it to lower case here

set brd=sparkfun:avr:promicro:cpu=16MHzatmega32U4

and it worked! Thanks, Richard, for sharing this with us.

I’m not having any luck getting this to even try to run under W11.
I pasted the text into a file named Go.bat and copied that from the desktop to /user/dvh, then I opened a command window in /user/dvh and it seems that nothing happens. Probably W11 “helping” me… What now?