====== Mass programming a lot of Arduino boards ======
This short Howto deals with programming a sketch on a lot of boards in the shortest possible time. You need a modern Linux workstation, like i.e. [[http://www.debian.org/|Debian]], [[http://www.ubuntu.com/|Ubuntu]], [[http://www.suse.com|SuSE]], our [[:robox-live:|robox-live]] or just any udev-enabled Linux distribution.
===== How it works =====
We use a custom udev-rule to automatically upload a prepared hex file using avrdude.
===== Step 1: Get the hex file =====
Only .hex files can be directly uploaded with avrdude. The easiest way to get the .hex file of your sketch is enabling verbose output in the preferences of your Arduino IDE and saving the file away for later use. If you enable verbose output, you will see some lines similar to the ones below:
/home/boxtec/apps/arduino-1.0.1/hardware/tools/avr/bin/avr-ar rcs
/tmp/build5730367966331024242.tmp/core.a /tmp/build5730367966331024242.tmp/wiring_analog.c.o
...and so on...
/home/boxtec/apps/arduino-1.0.1/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom
/tmp/build5730367966331024242.tmp/Blink.cpp.elf /tmp/build5730367966331024242.tmp/Blink.cpp.hex
Binary sketch size: 1,084 bytes (of a 32,256 byte maximum)
In the last line above you can see the name and path of the .hex file we are looking for:
**/tmp/build5730367966331024242.tmp/Blink.cpp.hex**
Now save this file to a convenient place, i.e. in our user home:
cp /tmp/build5730367966331024242.tmp/Blink.cpp.hex ~/
===== Step 2: Script for autoprogramming =====
The exact usage of avrdude is beyond the scope of this document, please check this [[http://www.ladyada.net/learn/avr/avrdude.html|fine howto]] from Lady Ada for more info on avrdude.
The script that automatically uploads the .hex file which we copied to our home in Step 1 is below, save it to i.e. **/usr/local/sbin/massprogram_arduino.sh** with the following contents:
#!/bin/bash
# CHANGE THIS LINE TO YOUR ARDUINO BASE DIRECTORY:
ARDUINO_BASE=/home/obiwan/apps/arduino-1.0.1
# CHANGE TO PATH TO YOUR .HEX FILE:
HEX_FILE=/home/boxtec/Blink.cpp.hex
# Log connection of the board
/bin/echo "Programming new Arduino on /dev/$1, udev-action: $ACTION" | /usr/bin/logger
# Beep once to indicate start of writing
/bin/echo -e "\007" > /dev/console
# run avrdude from Arduino directoy and save output for later:
OUT=$($ARDUINO_BASE/hardware/tools/avrdude -v -C$ARDUINO_BASE//hardware/tools/avrdude.conf \
-pm328p -carduino -P$1 -Uflash:w:$HEX_FILE 2>&1)
if [[ $? > 0 ]]; then
/bin/echo "Error occured programming board:" | /usr/bin/logger
/bin/echo $OUT | /usr/bin/logger
/bin/echo -e "\007" > /dev/console
sleep 0.5
/bin/echo -e "\007" > /dev/console
sleep 0.5
/bin/echo -e "\007" > /dev/console
sleep 0.5
/bin/echo -e "\007" > /dev/console
sleep 0.5
/bin/echo -e "\007" > /dev/console
else
# Beep twice to indicate that board is ready for disconnection
/bin/echo "Board programmed, ready for disconnect." | /usr/bin/logger
/bin/echo -e "\007" > /dev/console
sleep 0.2
/bin/echo -e "\007" > /dev/console
fi
Now make the script executable with the following command:
sudo chmod 750 /usr/local/sbin/massprogram_arduino.sh
You should now check if the script is working, connect i.e. an Arduino Uno and try running it with:
sudo /usr/local/sbin/massprogram_arduino.sh /dev/ttyACM0
Check your log in **/var/log/messages** and your Arduino to see if your code was actually uploaded as desired. If yes, you're ready for the next step.
===== Step 3: Create udev-rule =====
Udev rules are executed for matching target devices whenever such a device is connected (or disconnected). Our udev-rule shall invoke our massprogramming_arduino.sh script every time it sees an Arduino Uno somewhere on /dev/ttyACM*.
As root create **/etc/udev/rules.d/58-massprogram_arduino.rules** with the following contents:
SUBSYSTEMS=="usb", ATTRS{idVendor}=="2341,", ATTRS{idProduct}=="0001"
KERNEL=="ttyACM*", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="0001", RUN+="/usr/local/sbin/massprogram_arduino.sh /dev/%k"
The above rules are for an Arduino UNO, naturally you can easily adapt this for your our own board with the help of //dmesg// and //lsusb//.
===== Step 4: Test, save time, have more fun =====
You are now ready to give it a try, connect an Arduino UNO to USB and hear the beeps, 1 denotes the start, 2 the successful end. If you hear five longer beeps, something went wrong and the board wasn't successfully programmed.
It takes less than 4 seconds (**!**) of USB connection to program a board completely in our test setup.