1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
4
<title>Martin's Atelier: Arduino from the command line</title>
5
<link rel="alternate" type="application/atom+xml" href="../../feed.atom" />
6
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
7
<meta name="robots" content="all" />
8
<meta name="author" content="Martin Oldfield" />
9
<meta name="keywords" content="Arduino command line Makefile" />
10
<meta name="description" content="" />
11
<style type="text/css" media="all">@import "../../std.css";</style>
12
<style type="text/css" media="print">@import "../../print.css";</style>
18
<img id="logo_col" src="../../gears.png"/>
19
<img id="logo_bw" src="../../gears_bw.png"/>
20
<h1>Arduino from the command line</h1>
25
<p>The current version of this software (0.4) won't work with versions of the Arduino software before 0018.</p>
29
<p>The <a href="http://www.arduino.cc/">Arduino</a><span class="link_list"><sup>1</sup></span> 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 <span class="caps">GCC </span>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.</p>
31
<p>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 <span class="caps">IDE</span>: there's nothing wrong with it beyond it not being emacs...</p>
33
<p>It turns out that others have been along this path before: the Arduino website has a hopeful sounding <a href="http://www.arduino.cc/en/Hacking/CommandLine">Arduino from the Command Line</a><span class="link_list"><sup>2</sup></span> article. In turn this points you at the Makefile shipped as part of the Arduino software distribution: hardware/cores/arduino/Makefile.</p>
35
<p>This didn't really fit quite what I wanted to do though, so I wrote my own Makefile. You might wonder why I should embark on such a task. Well:</p>
38
<li>I was keen that all of my objects and random other files were completely separate from the main Arduino stuff in the applet directory.</li>
39
<li>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 <span class="caps">AVR GCC </span>toolchain.</li>
40
<li>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.</li>
43
<p>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.</p>
45
<p>So to the Makefile. Obviously it owes a great debt to the people who wrote the Makefile shipped with the Arduino <span class="caps">IDE </span>and here's the credit list from that file:</p>
47
<pre><code># Arduino 0011 Makefile
48
# Arduino adaptation by mellis, eighthave, oli.keller </code></pre>
50
<p>Thanks then to mellis, eighthavem and oli.keller.</p>
52
<h2>Installation instructions</h2>
54
<p>You'll need to download <a href="http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz">the tarball containing the Makefile,</a><span class="link_list"><sup>3</sup></span>, unpack it, and then copy the Makefile somewhere sensible:</p>
56
<pre><code>$ wget http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz
57
$ tar xzvf arduino-mk_0.4.tar.gz
58
$ cp arduino-mk-0.4/Arduino.mk /path/to/my/arduino/stuff/Arduino.mk </code></pre>
60
<p>The next step is to create a small Makefile for the sketch you actually want to build. The typical Arduino sketch directory looks like this:</p>
64
drwxr-xr-x 4 mjo staff 136 15 Feb 19:53 applet
65
-rw-r--r--@ 1 mjo staff 384 17 Feb 16:11 CLItest.pde</code></pre>
67
<p>To this we'll add a Makefile:</p>
69
<pre><code>ARDUINO_DIR = /Applications/Arduino.app/Contents/Resources/Java
74
ARDUINO_PORT = /dev/cu.usb*
76
ARDUINO_LIBS = LiquidCrystal
78
include /path/to/my/arduino/stuff/Arduino.mk</code></pre>
80
<p>Hopefully these will be self-explanatory but in case they're not:</p>
83
<dt><span class="caps">ARDUINO</span>_DIR</dt>
84
<dd>Where you installed the Arduino software. On the Mac this has to point deep inside the Arduino installation /Applications. </dd>
85
<dt><span class="caps">TARGET</span></dt>
86
<dd>The basename used for the final files. Canonically this would match the .pde file, but you can choose anything! </dd>
87
<dt><span class="caps">ARDUINO</span>_LIBS</dt>
88
<dd>A list of any libraries used by the sketch—we assume these are in $(ARDUINO_DIR)/hardware/libraries. </dd>
89
<dt><span class="caps">MCU</span></dt>
90
<dd>The target processor (atmega168 for the Duemilanove). </dd>
92
<dd>The target's clock speed (16000000 for the Duemilanove). </dd>
93
<dt><span class="caps">ARDUINO</span>_PORT</dt>
94
<dd>The port where the Arduino can be found (only needed when uploading) If this expands to several ports, the first will be used.</dd>
98
<p>A couple of other options might be useful:</p>
101
<dt><span class="caps">AVRDUDE</span>_ARD_PROGRAMMER</dt>
102
<dd>The protocol avrdude speaks—defaults to stk500v1. </dd>
103
<dt><span class="caps">AVRDUDE</span>_ARD_BAUDRATE</dt>
104
<dd>The rate at which we talk to the board—defaults to 19,200.</dd>
108
<p>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:</p>
110
<pre><code>AVR_TOOLS_PATH = /usr/bin
111
AVRDUDE_CONF = /etc/avrdude.conf</code></pre>
115
<p>If you're used to Unix then this is easy:</p>
120
<p>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.</p>
124
-rw-r--r--@ 1 mjo staff 384 17 Feb 16:11 CLItest.pde
125
-rw-r--r--@ 1 mjo staff 202 17 Feb 16:39 Makefile
126
drwxr-xr-x 4 mjo staff 136 15 Feb 19:53 applet
127
drwxr-xr-x 21 mjo staff 714 17 Feb 17:52 build-cli </code></pre>
131
<p>Let's peek inside the build-cli directory:</p>
133
<pre><code>$ ls -l build-cli
135
-rw-r--r-- 1 mjo staff 491 17 Feb 17:52 CLItest.cpp
136
-rw-r--r-- 1 mjo staff 583 17 Feb 17:52 CLItest.d
137
-rwxr-xr-x 1 mjo staff 60971 17 Feb 17:52 CLItest.elf
138
-rw-r--r-- 1 mjo staff 3994 17 Feb 17:52 CLItest.hex
139
-rw-r--r-- 1 mjo staff 5932 17 Feb 17:52 CLItest.o
140
-rw-r--r-- 1 mjo staff 6920 17 Feb 17:52 HardwareSerial.o
141
-rw-r--r-- 1 mjo staff 12708 17 Feb 17:52 Print.o
142
-rw-r--r-- 1 mjo staff 6852 17 Feb 17:52 WInterrupts.o
143
-rw-r--r-- 1 mjo staff 4680 17 Feb 17:52 WMath.o
144
-rw-r--r-- 1 mjo staff 611 17 Feb 17:52 depends.mk
145
-rw-r--r-- 1 mjo staff 5900 17 Feb 17:52 pins_arduino.o
146
-rw-r--r-- 1 mjo staff 8476 17 Feb 17:52 wiring.o
147
-rw-r--r-- 1 mjo staff 7224 17 Feb 17:52 wiring_analog.o
148
-rw-r--r-- 1 mjo staff 8244 17 Feb 17:52 wiring_digital.o
149
-rw-r--r-- 1 mjo staff 6544 17 Feb 17:52 wiring_pulse.o
150
-rw-r--r-- 1 mjo staff 7424 17 Feb 17:52 wiring_serial.o
151
-rw-r--r-- 1 mjo staff 5308 17 Feb 17:52 wiring_shift.o </code></pre>
153
<p>Most of the files in here are object files for the wiring library. What about the others ?</p>
156
<dt><span class="caps">CLI</span>test.cpp</dt>
157
<dd>This is the .pde sketch file with a small main program prepended and a suitable #include prepended. </dd>
158
<dt><span class="caps">CLI</span>test.d</dt>
159
<dd>This tracks the dependencies used by <span class="caps">CLI</span>test.pde </dd>
160
<dt><span class="caps">CLI</span>test.elf</dt>
161
<dd>This is executable produced by the linker </dd>
162
<dt><span class="caps">CLI</span>test.hex</dt>
163
<dd>This is a hex dump of (the code part) of the executable in a format understood by the Arduino's bootloader. </dd>
164
<dt><span class="caps">CLI</span>test.o</dt>
165
<dd>The object file we got by compiling <span class="caps">CLI</span>test.cpp. </dd>
167
<dd>A single file containing all the dependency relations (it's the concatentation of all the .d files).</dd>
171
<p>You may recall the <span class="caps">TARGET </span>variable we specified in the Makefile above: it's that which sets the base name for e.g. <span class="caps">CLI</span>test.elf. However <span class="caps">CLI</span>test.cpp, <span class="caps">CLI</span>test.o and <span class="caps">CLI</span>test.d take their name from the .pde sketch file.</p>
173
<h2>Uploading code</h2>
177
<pre><code>$ make upload</code></pre>
179
<h2>Uploading via <span class="caps">ISP</span></h2>
181
<p>If you're using target hardware which doesn't have a bootloader then you might want to use <span class="caps">ISP </span>to upload the code. Though you'll obviously need some extra hardware to do this.</p>
183
<p>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:</p>
185
<pre><code>ISP_PORT = /dev/ttyACM0
186
ISP_PROG = -c stk500v2</code></pre>
188
<p>Then to upload:</p>
190
<pre><code>$ make ispload</code></pre>
194
<p>You might need to change the fuse settings when programming, though some care needs to be taken here or you might irreversibly change the chip. You can set the fuse settings thus (the values below are the defaults):</p>
196
<pre><code>ISP_LOCK_FUSE_PRE = 0x3f
197
ISP_LOCK_FUSE_POST = 0xcf
203
<h2>Growing the project</h2>
205
<p>There a couple of obvious things to do now. You might want to edit the sketch. That's easy: just edit the .pde file and run make again.</p>
207
<p>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 <strong>should</strong> just work.</p>
211
<h2>Wiring-less development</h2>
213
<p>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.</p>
215
<pre><code>NO_CORE = 1</code></pre>
217
<h2>Bugs and problems</h2>
220
<li>The Makefile isn't very elegant.</li>
221
<li>Handling Arduino libraries isn't very clean. For one thing I'm relying on them being compiled in a compatible way by the Arduino environment which is probably a bit risky. Secondly it's a bit lazy to get the user to specify which libraries are being used manually.</li>
222
<li>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).</li>
223
<li>I fear that the Makefile makes unwarranted assumptions about the hardware: for example, all my Arduino experience has been ATmega 168 related.</li>
224
<li>The Makefile doesn't do some of the things that the Makefile distributed with the Arduino software does e.g. generating <span class="caps">COFF </span>files. I worry that some of these might be important.</li>
225
<li>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.</li>
230
<h3>2010-05-21, version 0.3</h3>
233
<li>Tidied up the licensing, making it clear that it's released under <span class="caps">LGPL</span> 2.1.</li>
234
<li><a href="http://hands.com/~phil/">Philip Hands</a><span class="link_list"><sup>4</sup></span> sent me some code to reset the Arduino by dropping <span class="caps">DTR </span>for 100ms, and I added it.</li>
235
<li>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.</li>
238
<h3>2010-05-24, version 0.4</h3>
241
<li>Tweaked rules for the reset target on Philip Hands' advice. </li>
245
<ul><li>1. http://www.arduino.cc/</li><li>2. http://www.arduino.cc/en/Hacking/CommandLine</li><li>3. http://mjo.tc/atelier/2009/02/acli/arduino-mk_0.4.tar.gz</li><li>4. http://hands.com/~phil/</li></ul>
249
<div class="listcontainer">
250
<h2><a href="../../index/index.html">Martin's Atelier</a></h2>
251
<div id="subscribe"><p><a href="../../feed.atom"><img src="../../atom.png" alt="[ Atom Feed ]" title="Atom Feed" /></a></p></div>
255
<div class="listcontainer">
256
<h3>Related Articles</h3>
259
<li><a href="../03/aarduino.html">The AArduino</a><br/>Martin Oldfield, 16 Jun 2009</li>
261
<li><a href="avrdude-cookbook.html">Avrdude Cookbook</a><br/>Martin Oldfield, 23 Feb 2009</li>
263
<li><a href="olimex-avrdude-mac.html">MacOS X and the Olimex AVR-ISP500</a><br/>Martin Oldfield, 17 Feb 2009</li>
270
<div class="listcontainer">
271
<h3><a href="../../index/st_subject.html">Related Subjects</a></h3>
274
<li><a href="../../index/t_Arduino.html">Arduino</a></li>
276
<li><a href="../../index/t_command line.html">command line</a></li>
278
<li><a href="../../index/t_Makefile.html">Makefile</a></li>
285
<div class="listcontainer">
286
<h3><a href="../../index/index.html">Other Articles</a></h3>
289
<li><a href="../../2010/06/arm-toolchain.html">Getting an ARM toolchain on MacOS 10.6</a><br/>Martin Oldfield, 06 Jun 2010</li>
291
<li><a href="../../2010/05/bus-pirate.html">The Bus Pirate on MacOS</a><br/>Martin Oldfield, 26 May 2010</li>
293
<li><a href="../../2010/04/perl-unicode.html">Unicode games with perl, MySQL, and XML</a><br/>Martin Oldfield, 17 Apr 2010</li>
295
<li><a href="../../2010/01/gps-track-filter.html">Filtering GPS tracks</a><br/>Martin Oldfield, 04 Jan 2010</li>
297
<li><a href="../01/paris_drink.html">Places to drink in Paris</a><br/>Martin Oldfield, 15 Dec 2009</li>
299
<li><a href="../01/paris_food.html">Places to eat in Paris</a><br/>Martin Oldfield, 15 Dec 2009</li>
301
<li><a href="../11/sons-and-daughters.html">Sons and Daughters</a><br/>Martin Oldfield, 06 Dec 2009</li>
303
<li><a href="../12/garmin-gpx.html">Faking geocaches in Garmin GPX files</a><br/>Martin Oldfield, 06 Dec 2009</li>
305
<li><a href="../08/macos-pic.html">Playing with PICs on MacOS X</a><br/>Martin Oldfield, 18 Aug 2009</li>
313
<div class="lastlistcontainer">
314
<h3>Bookmark this article</h3>
317
<li><a href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html" title="Facebook"><img src="../../social/facebook.png" alt="[ facebook ]"/></a></li>
319
<li><a href="http://del.icio.us/post?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Delicious"><img src="../../social/delicious.png" alt="[ delicious ]"/></a></li>
321
<li><a href="http://digg.com/submit?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Digg"><img src="../../social/digg.png" alt="[ digg ]"/></a></li>
323
<li><a href="http://reddit.com/submit?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Reddit"><img src="../../social/reddit.png" alt="[ reddit ]"/></a></li>
325
<li><a href="http://www.stumbleupon.com?url=http%3A%2F%2Fmjo.tc%2Fatelier%2F2009%2F02%2Farduino-cli.html&title=Arduino+from+the+command+line" title="Stumbleupon"><img src="../../social/stumbleupon.png" alt="[ stumbleupon ]"/></a></li>
334
Revised by Martin Oldfield on 07 Jun 2010, original version 17 Feb 2009.<br/>
335
Contact: Martin Oldfield, ex-atelier@mjo.tc<br/>
336
This work is available under the <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>, or the <a href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html">LGPL</a> version 2.1 or later.
342
<script src="http://www.google-analytics.com/urchin.js"
343
type="text/javascript">
345
<script type="text/javascript">
346
_uacct = "UA-567180-1";