{"id":335,"date":"2012-06-12T08:00:47","date_gmt":"2012-06-12T06:00:47","guid":{"rendered":"http:\/\/www.martyndavis.com\/?p=335"},"modified":"2021-07-29T11:23:58","modified_gmt":"2021-07-29T10:23:58","slug":"arduino-from-the-command-line","status":"publish","type":"post","link":"https:\/\/www.martyndavis.com\/?p=335","title":{"rendered":"Arduino from the Command Line"},"content":{"rendered":"<p>The Arduino IDE does a great job of simplifying the creation of programs for Arduino (<a href=\"http:\/\/www.martyndavis.com\/?p=105\">and clones<\/a>) &nbsp;which is great if you don&#8217;t have much programming experience.<\/p>\n<p>But if you&#8217;ve done more than a bit of programming in the past, you&#8217;ll soon find the dinky IDE a little frustrating to use. For example, my preferred text editor, by far, is <a href=\"http:\/\/www.vim.org\/\">vim<\/a>, and I find it quite jarring to be forced to use a basic text editor to build programs. Yes, you can select &#8220;use external editor&#8221; in the settings, but it&#8217;s clumsy. I&#8217;ve built many an Arduino program in vim, then switched to the Arduino IDE to build it. It&#8217;s just not streamlined &#8211; it&#8217;s too slow and clunky.<\/p>\n<p>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&#8217;s a package called <strong>arduino-mk<\/strong>&nbsp;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&#8217;re weird :) and makefiles.<\/p>\n<p>But&#8230; it doesn&#8217;t work out of the box &#8211; here&#8217;s what you need to do to fix it&#8230;<!--more--><\/p>\n<p>Firstly, the only packages you&#8217;ll need are <strong>arduino-core<\/strong>&nbsp;and this new <strong>arduino-mk<\/strong>&nbsp;&#8211; you can forget the <strong>arduino<\/strong>&nbsp;package itself. You&#8217;ll also need the <strong>make<\/strong>&nbsp;package if you haven&#8217;t already got it installed.<\/p>\n<p>So install these using your package manager, or, from the command line with <strong>apt-get.<\/strong><\/p>\n<p>Once you&#8217;ve got those installed, add yourself to the group <strong>dialout<\/strong>&nbsp;with the command<\/p>\n<pre>sudo usermod -a -G dialout <em>yourusername<\/em><\/pre>\n<p>After you&#8217;ve done this, you should log out and back in again as your groups permissions won&#8217;t be updated until then.<\/p>\n<p>Now, in theory, we can create an Arduino program like this simple blink program in whichever directory we like:<\/p>\n<pre>void&nbsp;setup()&nbsp;{\n&nbsp;&nbsp;pinMode(13,&nbsp;OUTPUT);&nbsp;&nbsp; &nbsp;&nbsp;\n}<\/pre>\n<pre>void&nbsp;loop()&nbsp;{\n&nbsp;&nbsp;digitalWrite(13,&nbsp;HIGH);\n&nbsp;&nbsp;delay(1000);\n&nbsp;&nbsp;digitalWrite(13,&nbsp;LOW);\n&nbsp;&nbsp;delay(1000);\n}<\/pre>\n<p>&#8230;save it as, say, <strong>blink.ino<\/strong>, then create a Makefile for it:<\/p>\n<pre>ARDUINO_DIR = \/usr\/share\/arduino\nBOARD_TAG    = uno\nARDUINO_PORT = \/dev\/ttyACM*\nARDUINO_LIBS =\ninclude \/usr\/share\/arduino\/Arduino.mk<\/pre>\n<p>(note, save it as &#8220;Makefile&#8221; with a capital &#8220;M&#8221;)&#8230;and, in theory, from the command line in this directory, (if you have the <strong>make<\/strong>&nbsp;package installed, that is) you can simply issue the command <strong>make<\/strong>&nbsp;which will build the program.<\/p>\n<p>However, when I tried this, I got error messages come up:<\/p>\n<pre>\/usr\/share\/arduino\/Arduino.mk:514: build-cli\/depends.mk: No such file or directory\nmkdir build-cli\necho '#include &lt;Arduino.h&gt;' &gt; build-cli\/blink.cpp\ncat  blink.ino &gt;&gt; build-cli\/blink.cpp\nmake: ard-parse-boards: Command not found\nmake: ard-parse-boards: Command not found\nmake: ard-parse-boards: Command not found\n\/usr\/bin\/avr-g++ -MM -mmcu= -DF_CPU= -DARDUINO=100 -I. -I\/...\ncc1plus: error: missing argument to \"-mmcu=\"\nmake: *** [build-cli\/blink.d] Error 1\nrm build-cli\/blink.cpp<\/pre>\n<p>The important error here is the fact that make can&#8217;t find the program <strong>ard-parse-boards<\/strong>. To fix this, edit the file \/usr\/share\/arduino\/Arduino.mk (you&#8217;ll need to <strong>sudo <\/strong>edit it)&nbsp;and go to line 218, which should read:<\/p>\n<pre>PARSE_BOARD = ard-parse-boards --boards_txt=$(BOARDS_TXT)<\/pre>\n<p>change this line to:<\/p>\n<pre>PARSE_BOARD = <strong><span style=\"color: #ff6600;\">$(ARDUINO_DIR)\/<\/span><\/strong>ard-parse-boards --boards_txt=$(BOARDS_TXT)<\/pre>\n<p>(I&#8217;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 <strong>make<\/strong>&nbsp;command. You should see plenty of compilation output!<\/p>\n<p>Finally, to upload to the board, simply type <strong>make upload<\/strong>. &nbsp;If you get permission errors you most likely haven&#8217;t added yourself to the <strong>dialout<\/strong>&nbsp;group, or haven&#8217;t rebooted after having done so.<\/p>\n<p>If you need more detail (e.g. specifying libraries), this is the page to look at:&nbsp;<a href=\"http:\/\/mjo.tc\/atelier\/2009\/02\/arduino-cli.html\">http:\/\/mjo.tc\/atelier\/2009\/02\/arduino-cli.html<\/a><\/p>\n<p>Hope this helps!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Arduino IDE does a great job of simplifying the creation of programs for Arduino (and clones) &nbsp;which is great if you don&#8217;t have much programming experience. But if you&#8217;ve done more than a bit of programming in the past, you&#8217;ll soon find the dinky IDE a little frustrating to use. For example, my preferred &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.martyndavis.com\/?p=335\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Arduino from the Command Line&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/335"}],"collection":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=335"}],"version-history":[{"count":9,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":819,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/335\/revisions\/819"}],"wp:attachment":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}