Arduino from the command line

Retirement

2013-05-19 : I've let this languish for too long, mainly because I don't do much with Arduinos now. Happily Sudar Muthu has taken over maintainance of the code. His github repository is https://github.com/sudar/Arduino-Makefile/1 though by the magic of github, the old URL continues to work.

Thanks to Sudar and everyone else who has helped for keeping this alive.

Update News

2012-09-17 : After letting this languish for many months there’s now version 0.10.2 This:

Github

You can now grab the source for this from github.4 Besides my version there are several others under development which offer better integration with the IDE, support for Microchip’s ChipKIT boards, and other delights.

Introduction

The Arduino5 has done much to popularize microcontrollers for the casual tinkerer. Its success suggests that there’s considerable value in combining a standard microcontroller (the ATmega) and a GCC based toolchain into an easily digesible package. For myself, it’s certainly easier to just install the latest release of the Arduino software than worry about building my own cross-compilers, particularly when it’s all new to me and consequently somewhat confusing.

After working through the toy tutorials though, I found myself wishing that writing code for the Arduino were more like writing other C programs. In my case, that means editing it with emacs then building it with make. I must emphasize that I’m not criticizing the Arduino IDE: there’s nothing wrong with it beyond it not being emacs...

It turns out that others have been along this path before: in the past the Arduino website had a hopeful sounding ‘Arduino from the Command Line’ article, but it’s gone now. There is still some information6 though it’s more limited.

Without an official Makefile, I wrote my own. You might wonder why I should embark on such a task. Well:

I was keen that all of my objects and random other files were completely separate from the main Arduino stuff in the applet directory.

Although I wanted to be able to build Arduino sketches, I also wanted a suitable jumping-off point for code which didn’t use wiring. In other words, to regard the Arduino software as a convenient way to get the AVR GCC toolchain.

Rather than dumping a big Makefile in each sketch directory, I wanted to have a few definitions in the directory which then included a large project-independent file from elsewhere.

Finally, one of the things I enjoy about writing code for microcontrollers is the sense of continuity between the hardware datasheets published by the chip manufacturer and the code I write (by contrast if you’re writing code on Linux there’s a vast gulf between the code executing printf and stuff appearing on the screen). Writing my own Makefile seemed a good way to make sure I understood what was going on.

So to the Makefile. Obviously it owes a great debt to the people who wrote the Makefile shipped with the Arduino IDE and here’s the credit list from that file:

The final step is to create a small Makefile for the sketch you actually want to build. Let’s build the WebServer example8 from the Arduino distribution: it’s a good example because software-wise it’s as complicated as the stardard examples get, but you can just plug the hardware together.

Create a new directory and copy the WebServer.ino file into it.

Note: If you’re using version 1.0 of the Arduino software, you’ll need to make sure that the sketch’s name ends in .ino and not .pde.

A tag identifying which type of Arduino you’re using. This only works in version 0.6 and later.

ARDUINO_PORT

The port where the Arduino can be found (only needed when uploading) If this expands to several ports, the first will be used.

ARDUINO_LIBS

A list of any libraries used by the sketch—we assume these are in $(ARDUINO_DIR)/hardware/libraries.

Until version 0.8 you had to specify a TARGET name which set the basename for the executables. You still can do this, but it’s not necessary: thanks to a patch from Daniele Vergini it now defaults to the name of the current directory.

In the past, the following options were used, and indeed you can still use them. However it’s probably better to use set BOARD_TAG and let the Makefile look up the values in boards.txt:

MCU

The target processor (atmega168 for the Duemilanove).

F_CPU

The target’s clock speed (16000000 for the Duemilanove).

AVRDUDE_ARD_PROGRAMMER

The protocol avrdude speaks—defaults to stk500v1.

AVRDUDE_ARD_BAUDRATE

The rate at which we talk to the board—defaults to 19,200.

BOARD_TAG

Makefiles before version 0.5 had to specify which processor and speed the target used. For standard boards, this information can be found in the boards.txt file, so it seemed sensible to use that instead.

Now, one need only define BOARD_TAG to match the target hardware and it should work. Internally the Makefile invokes ard-parse-boards—a small Perl utility included with the software—which parses board.txt.

If you’re not sure which board tag you need, ard-parse-board will dump a full list:

You can, of course, continue to set F_CPU and MCU directly should you prefer that.

ARDUINO_LIBS

Early (up to and including version 0.4) of this Makefile didn’t really support this (despite claims to the contrary). Happily various kind people sorted out the problem, one of whom patched the Debian and Ubuntu version.

In the official IDE, it’s enough to select the library from a menu: this puts the relevant #include into the Sketch and adds the necessarily linker tweaks too.

In this Makefile, you’ll need to both add the #include yourself and append the directories which contain the library to the ARDUINO_LIBS variable. Often these will both have the same name, though it’s worth noting that the #include refers to a single file, but the ARDUINO_LIBS entry refers to an entire directory of source files.

However, care is needed if the library’s source files aren’t in a single directory. For example, the webserver example uses the Ethernet library9 and we needed to include both Ethernet and Ethernet/utility in ARDUINO_LIBS.

If you omit the .../utility library, you’ll get messy looking link errors from the bowels of the Ethernet library. The SPI and Wire libraries are like this too!

Building

If you’re used to Unix then this is easy:

$ make
...

The output is pretty verbose, but I think it should be obvious if it worked. After building you’ll see a new directory has been created which contains all the object files: build-uno. Since version 0.10, if you rebuild the software with a different BOARD_TAG, you’ll get a different directory name.

Growing the project

There a couple of obvious things to do now. You might want to edit the sketch. That’s easy: just edit the .ino file and run make again.

Alternatively you might want to add some more source files to the project. That’s easy too: the Makefile understands C, C++ and assembler files in the source directory (with .c, .cpp, and .s extensions). Everything should just work.

Wiring-less development

Finally you might want to develop code which isn’t linked against the Wiring library. There’s some scope for this: just set NO_CORE in the Makefile e.g.

NO_CORE = 1

Bugs and problems

The Makefile isn’t very elegant.

When compiling the sketch file, the compiler actually sees the .cpp file derived from it. Accordingly the line numbers of any errors will be wrong (but not by that much).

The Makefile doesn’t do some of the things that the Makefile distributed with the Arduino software does e.g. generating COFF files. I worry that some of these might be important.

This hasn’t been used very much yet, even by me. I’m writing this now as much for my benefit as anyone else’s, though I’d be delighted to know if anyone else finds it useful.

Changelog

Tidied up the licensing, making it clear that it’s released under LGPL 2.1.

Philip Hands11 sent me some code to reset the Arduino by dropping DTR for 100ms, and I added it.

Tweaked the Makefile to handle version 0018 of the Arduino software which now includes main.cpp. Accordingly we don’t need to—and indeed must not—add main.cxx to the .pde sketch file. The paths seem to have changed a bit too.

Imported changes from Debian/Ubuntu, which incorporate a patch from Stefan Tomanek so that libraries would be compiled too.

Note: Many other people sent me similar patches, but I didn’t get around to using them. In the end, I took the patch from Debian and Ubuntu: there seems merit in not forking the code and using a tested version. So, thanks and apologies to Nick Andrew, Leandro Coletto Biazon, Thibaud Chupin, Craig Hollabaugh, Johannes H. Jensen, Fabien Le Lez, Craig Leres, and Mark Sproul.