Update News

2012-02-12 : After letting this languish for over six months, there's now version 0.81 which:

In principle version 0.8 should work with older releases of the Arduino IDE but I've not checked it. If you do upgrade to 1.0 though, you'll need to rename your .pde files to .ino.

Introduction

The Arduino2 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 information3 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:

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:

# Arduino 0011 Makefile
# Arduino adaptation by mellis, eighthave, oli.keller

Thanks then to mellis, eighthavem and oli.keller.

Installation instructions

If you're using Debian or Ubuntu, then just grab the arduino-core package.

You'll need to download the tarball containing the Makefile,4, unpack it, and then copy the Makefile somewhere sensible:

$ wget http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.8.tar.gz
$ tar xzvf arduino-mk_0.8.tar.gz
$ cp arduino-mk-0.8/Arduino.mk /path/to/my/arduino/stuff/Arduino.mk
$ cp arduino-mk-0.8/ard-parse-boards /usr/local/bin

The next step is to create a small Makefile for the sketch you actually want to build. Let's build the WebServer example5 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. Now this we'll add a Makefile:

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.

ARDUINO_DIR = /Applications/Arduino.app/Contents/Resources/Java

BOARD_TAG    = uno
ARDUINO_PORT = /dev/cu.usb*

ARDUINO_LIBS = Ethernet Ethernet/utility SPI

include /path/to/my/arduino/stuff/Arduino.mk

Hopefully these will be self-explanatory but in case they're not:

ARDUINO_DIR
Where you installed the Arduino software. On the Mac this has to point deep inside the Arduino installation /Applications.
ARDUINO_LIBS
A list of any libraries used by the sketch—we assume these are in $(ARDUINO_DIR)/hardware/libraries.
BOARD_TAG
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.

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.

If you're using the toolchain provided by the system rather than bundled with the Arduino software (as I think is the case on Linux) then you'll have to add some more paths:

AVR_TOOLS_PATH   = /usr/bin
AVRDUDE_CONF     = /etc/avrdude.conf

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:

$ ard-parse-boards --boards						
Tag          Board Name							
atmega168    Arduino NG or older w/ ATmega168				
atmega328    Arduino Duemilanove or Nano w/ ATmega328			
atmega8      Arduino NG or older w/ ATmega8				
bt           Arduino BT w/ ATmega168					
bt328        Arduino BT w/ ATmega328					
diecimila    Arduino Diecimila, Duemilanove, or Nano w/ ATmega168	
fio          Arduino Fio						
lilypad      LilyPad Arduino w/ ATmega168				
lilypad328   LilyPad Arduino w/ ATmega328				
mega         Arduino Mega (ATmega1280)					
mega2560     Arduino Mega 2560						
mini         Arduino Mini						
pro          Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168		
pro328       Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328		
pro5v        Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168		
pro5v328     Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328		
uno          Arduino Uno						

If you don't set it, BOARD_TAG defaults to uno.

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 library6 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-cli.

$ $ ls -lR
total 16
-rw-r--r--   1 mjo  staff   263 12 Feb 11:06 Makefile
-rw-r--r--   1 mjo  staff  2308 12 Feb 10:57 WebServer.ino
drwxr-xr-x  28 mjo  staff   952 12 Feb 11:07 build-cli

build-cli

Let's peek inside the build-cli directory:

$ ls -l build-cli
total 2136	 						
-rw-r--r--  1 mjo  staff    2292 12 Feb 11:07 CDC.o		
-rw-r--r--  1 mjo  staff    2292 12 Feb 11:07 HID.o		
-rw-r--r--  1 mjo  staff   23452 12 Feb 11:07 HardwareSerial.o	
-rw-r--r--  1 mjo  staff   16008 12 Feb 11:07 IPAddress.o	
-rw-r--r--  1 mjo  staff   40012 12 Feb 11:07 Print.o		
-rw-r--r--  1 mjo  staff   21068 12 Feb 11:07 Stream.o		
-rw-r--r--  1 mjo  staff   16580 12 Feb 11:07 Tone.o		
-rw-r--r--  1 mjo  staff    2300 12 Feb 11:07 USBCore.o		
-rw-r--r--  1 mjo  staff    6048 12 Feb 11:06 WInterrupts.o	
-rw-r--r--  1 mjo  staff    7068 12 Feb 11:07 WMath.o		
-rw-r--r--  1 mjo  staff   79196 12 Feb 11:07 WString.o		
-rw-r--r--  1 mjo  staff    2329 12 Feb 10:57 WebServer.cpp	
-rw-r--r--  1 mjo  staff    1920 12 Feb 11:06 WebServer.d	
-rw-r--r--  1 mjo  staff   11324 12 Feb 11:06 WebServer.o	
-rwxr-xr-x  1 mjo  staff  193852 12 Feb 11:07 WebServer.elf
-rw-r--r--  1 mjo  staff   28572 12 Feb 11:07 WebServer.hex	
-rw-r--r--  1 mjo  staff    1920 12 Feb 11:08 depends.mk	
-rw-r--r--  1 mjo  staff  541002 12 Feb 11:07 libcore.a		
drwxr-xr-x  4 mjo  staff     136 12 Feb 10:57 libs		
-rw-r--r--  1 mjo  staff    3616 12 Feb 11:07 main.o		
<-rw-r--r--  1 mjo  staff    5544 12 Feb 11:07 new.o		
-rw-r--r--  1 mjo  staff    9780 12 Feb 11:06 wiring.o		
-rw-r--r--  1 mjo  staff    7024 12 Feb 11:06 wiring_analog.o	
-rw-r--r--  1 mjo  staff    9704 12 Feb 11:06 wiring_digital.o	
-rw-r--r--  1 mjo  staff    7056 12 Feb 11:06 wiring_pulse.o	
-rw-r--r--  1 mjo  staff    5736 12 Feb 11:06 wiring_shift.o	
								
./build-cli/libs:						
total 0								
drwxr-xr-x  9 mjo  staff  306 12 Feb 11:07 Ethernet		
drwxr-xr-x  3 mjo  staff  102 12 Feb 11:07 SPI			
								
./build-cli/libs/Ethernet:					
total 392							
-rw-r--r--  1 mjo  staff  24836 12 Feb 11:07 Dhcp.o		
-rw-r--r--  1 mjo  staff  23112 12 Feb 11:07 Dns.o		
-rw-r--r--  1 mjo  staff  33008 12 Feb 11:07 Ethernet.o		
-rw-r--r--  1 mjo  staff  42000 12 Feb 11:07 EthernetClient.o	
-rw-r--r--  1 mjo  staff  19420 12 Feb 11:07 EthernetServer.o	
-rw-r--r--  1 mjo  staff  41244 12 Feb 11:07 EthernetUdp.o	
drwxr-xr-x  4 mjo  staff    136 12 Feb 11:07 utility		
								
./build-cli/libs/Ethernet/utility:				
total 152							
-rw-r--r--  1 mjo  staff  40480 12 Feb 11:07 socket.o		
-rw-r--r--  1 mjo  staff  34840 12 Feb 11:07 w5100.o		
								
./build-cli/libs/SPI:						
total 16							
-rw-r--r--  1 mjo  staff  6812 12 Feb 11:07 SPI.o		

Most of the files in here are object files for the wiring library. What about the others ?

WebServer.cpp
This is the .pde sketch file with a small main program prepended and a suitable #include prepended.
WebServer.d
This tracks the dependencies used by WebServer.pde
WebServer.elf
This is executable produced by the linker
WebServer.hex
This is a hex dump of (the code part) of the executable in a format understood by the Arduino's bootloader.
WebServer.o
The object file we got by compiling WebServer.cpp.
depends.mk
A single file containing all the dependency relations (it's the concatentation of all the .d files).
libcore.a
Rather than link all the system supplied objects directly, we build them into this library first, then link against it.

Uploading code

This is easy:

$ make upload

Uploading via ISP

If you're using target hardware which doesn't have a bootloader then you might want to use ISP to upload the code. Though you'll obviously need some extra hardware to do this.

Assuming that avrdude supports your programmer though, you'll only need to make a few changes to the Makefile to tell avrdude where it can find the programmer and how to talk to it:

ISP_PORT         = /dev/ttyACM0
ISP_PROG         = -c stk500v2

Then to upload:

$ make ispload

Fuses

You might need to change the fuse settings when programming, though some care needs to be taken here or you might irreversibly damage the chip.

Normally the fuse settings are chosen from the boards.txt file to match the value of BOARD_TAG (assuming you're running version 0.6 or higher), but you can set them yourself:

ISP_LOCK_FUSE_PRE  = 0x3f
ISP_LOCK_FUSE_POST = 0xcf
ISP_HIGH_FUSE      = 0xdf
ISP_LOW_FUSE       = 0xff
ISP_EXT_FUSE       = 0x01

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

Changelog

2010-05-21, version 0.37

2010-05-24, version 0.49

2011-06-23, version 0.510

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.

2011-06-23, version 0.611

Unreleased, version 0.713

2012-02-12, version 0.814

Similar work

It's not a derivative of this, but Alan Burlison has written a similar thing.15

Alan's Makefile was used in a Pragmatic Programmer's article.16