Compiling and uploading arduino programs without the IDE

The Arduino IDE might be good for beginners, but it’s not my usual text editor, so I don’t like it. That being said, what do we need to do in order to compile stuff as usual C programs, and upload them to the board? Using a bit of google-fu we get to several arduino makefiles. I found that this one looked alright but ultimately settled or a fork of that repo which had a bunch of bugfixes and new features like autodetecting versions and so on.

I’m running Ubuntu 11.04 and the Arduino packages aren’t up to date, so I just downloaded and decompressed the main folder to a directory which we’ll call /arduino/arduino from now on. I then downloaded the makefile repo from github which you can find here.

I put the makefile repo alongside the arduino folder in /arduino/arduino-mk.

Let’s get the blink program working. Create a folder in /arduino/blink with a blink.ino inside.

void setup() {
    pinMode(13, OUTPUT);
}

void loop() {
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
}

I’m using a duemilanove with a 328 so that’s what I need to put in my makefile. If you got a new Uno just sed -i "s/atmega328/uno/" makefile. Also keep an eye on the port your board is using but `/dev/ttyUSB0 should be ok if you’re not on a wierd system (ie: not Linux). My makefile:

BOARD_TAG = atmega328
ARDUINO_PORT = /dev/ttyUSB0
ARDUINO_DIR = /arduino/arduino
ARDMK_DIR = /arduino/arduino-mk
include /arduino/arduino-mk/arduino-mk/Arduino.mk

Now you can run make to be sure everything is okay. When I ran make upload I got an error saying:

make: /home/vasco/Prog/arduino/arduino-1.0.1/hardware/tools/bin/avrdude: Command not found
make: *** [raw_upload] Error 127

By looking into the code in Arduino.mk, it looks like we could solve it by telling the each and every makefile where AVR_TOOLS_DIR was. We can also just put the files where the makefile expects them to be. So we’ll do that instead.

cp /arduino/arduino/hardware/tools/avrdude /arduino/arduino/hardware/tools/avr/bin/avrdude
cp /arduino/arduino/hardware/tools/avrdude.conf /arduino/arduino/hardware/tools/avr/etc/avrdude.conf

Now you’re a make upload away from seeing something like this:

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

And your Arduino should be blinking away. For programs which use other libraries, you now need to include them as you normally do in C. The makefile is doing its magic by linking the Arduino basic ones so if for example you were to use the Serial library to do something useful like counting to 1000 (note the include at the top):

#include <SoftwareSerial.h>
void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i<1000; i++) {
     Serial.println(i);
     delay(10);
  }
}

If you need help visualizing, your tree should look something like this:

arduino
├── arduino
├── arduino-mk
│   └── arduino-mk
│       └── Arduino.mk
└── projs
    ├── blink
    │   ├── makefile
    │   └── blink.ino
    └── serial

UPDATE: After all this, someone showed me inotool which has a bunch of cool features and is simpler to set up and use. Check it out.