Arduino from the Command Line

The Arduino IDE does a great job of simplifying the creation of programs for Arduino (and clones)  which is great if you don’t have much programming experience.

But if you’ve done more than a bit of programming in the past, you’ll soon find the dinky IDE a little frustrating to use. For example, my preferred text editor, by far, is vim, and I find it quite jarring to be forced to use a basic text editor to build programs. Yes, you can select “use external editor” in the settings, but it’s clumsy. I’ve built many an Arduino program in vim, then switched to the Arduino IDE to build it. It’s just not streamlined – it’s too slow and clunky.

It was therefore with delight I saw in the kubuntu 12.04 repository (I recently re-installed everything on my desktop after falling back in love with KDE) that there’s a package called arduino-mk which promises to provide the ability to build arduino programs directly from the command line. Which means you can use vim (or emacs if you’re weird :) and makefiles.

But… it doesn’t work out of the box – here’s what you need to do to fix it…

Firstly, the only packages you’ll need are arduino-core and this new arduino-mk – you can forget the arduino package itself. You’ll also need the make package if you haven’t already got it installed.

So install these using your package manager, or, from the command line with apt-get.

Once you’ve got those installed, add yourself to the group dialout with the command

sudo usermod -a -G dialout yourusername

After you’ve done this, you should log out and back in again as your groups permissions won’t be updated until then.

Now, in theory, we can create an Arduino program like this simple blink program in whichever directory we like:

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

…save it as, say, blink.ino, then create a Makefile for it:

ARDUINO_DIR = /usr/share/arduino
BOARD_TAG    = uno
ARDUINO_PORT = /dev/ttyACM*
ARDUINO_LIBS =
include /usr/share/arduino/Arduino.mk

(note, save it as “Makefile” with a capital “M”)…and, in theory, from the command line in this directory, (if you have the make package installed, that is) you can simply issue the command make which will build the program.

However, when I tried this, I got error messages come up:

/usr/share/arduino/Arduino.mk:514: build-cli/depends.mk: No such file or directory
mkdir build-cli
echo '#include <Arduino.h>' > build-cli/blink.cpp
cat  blink.ino >> build-cli/blink.cpp
make: ard-parse-boards: Command not found
make: ard-parse-boards: Command not found
make: ard-parse-boards: Command not found
/usr/bin/avr-g++ -MM -mmcu= -DF_CPU= -DARDUINO=100 -I. -I/...
cc1plus: error: missing argument to "-mmcu="
make: *** [build-cli/blink.d] Error 1
rm build-cli/blink.cpp

The important error here is the fact that make can’t find the program ard-parse-boards. To fix this, edit the file /usr/share/arduino/Arduino.mk (you’ll need to sudo edit it) and go to line 218, which should read:

PARSE_BOARD = ard-parse-boards --boards_txt=$(BOARDS_TXT)

change this line to:

PARSE_BOARD = $(ARDUINO_DIR)/ard-parse-boards --boards_txt=$(BOARDS_TXT)

(I’ve put the new part in red). Save that, and now go back to your test directory with the blink.ino and Makefile in it, and issue another make command. You should see plenty of compilation output!

Finally, to upload to the board, simply type make upload.  If you get permission errors you most likely haven’t added yourself to the dialout group, or haven’t rebooted after having done so.

If you need more detail (e.g. specifying libraries), this is the page to look at: http://mjo.tc/atelier/2009/02/arduino-cli.html

Hope this helps!

 

Comments 5