Arduino on the command line

I’ve lately been trying to compile Arduino projects from command line. Primarily because the Arduino IDE wasn’t working out of the box on 64-bit Ubuntu (9.04). That problem was eventually solved, but I’ve been meaning to move away from that IDE anyway.

After reading a lot of posts I finally ended up with a working solution.

In addition to arduino (for 32-bit linux), you need to get a couple of packages:

sudo apt-get install avg-gcc
sudo apt-get install avr-libc
sudo apt-get install python-serial

You’ll find hardware/cores/arduino/Makefile under the Arduino folder. This one is meant to serve as a basis for your own projects. I had to make some modifications to it, in addition to what was described in the file. You can get my Makefile, which basically just splits the $AVT_TOOLS_PATH into two separate paths.

A key point that was hard to figure out, was that the Arduino needs to be reset right before a program is uploaded. This is automatically done by the IDE, but not by the Makefile. If this is not in place, you’ll get error messages like:

stk500_recv(): programmer is not responding

or

avrdude: stk500_getsync(): not in sync: resp=0x16
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x34

I made a tiny python script to reset the arduino:

import serial
import time
import sys

if len(sys.argv) != 2:
    print "Please specify a port, e.g. %s /dev/ttyUSB0" % sys.argv[0]
    sys.exit(-1)

ser = serial.Serial(sys.argv[1])
ser.setDTR(1)
time.sleep(0.5)
ser.setDTR(0)
ser.close()

Save this as pulsedtr.py, move it to somewhere in your $PATH and do chmod u+x pulsedtr.py, then place it in the Makefile under upload (Make sure you replace spaces with tabs if you copy from below):

upload: applet/$(TARGET).hex
    pulsedtr.py $(PORT)
    $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)

This was all that was needed for me to be able to make upload. Let me know if you have any problems.

Update: After using the Makefile for a little while, I realized that the dependencies are a bit wrong. This is fixed now.

Update: If you happen to be replacing the Arduino IDE with Vim, I just uploaded an Arduino syntax file.