Posted on 3 Comments

#16: Compiling Arduino sketches with Pocket C.H.I.P.

Do you ever find yourself inside your ceiling or under the floor working on your home automation system, and need to SSH to a remote machine or re-flash an Arduino?

Sure, you could drag your laptop under there with you, but I don’t want to have my expensive laptop lying around in the dirt or insulation.

I may have found the answer.

C.H.I.P. and Pocket C.H.I.P. will be available soon from getchip.com.

I used “arduino-mk” running on the Pocket C.H.I.P to compile and upload an Arduino sketch: github.com/sudar/Arduino-Makefile

The following sequence got everything working for me. You may want to do things a little differently, for example by managing the python-serial extension using Pip or storing your sketches in a different location. That’s totally up to you, just make sure you alter the paths shown below as necessary.

Start by making sure your package list is up to date, and that you have the latest packaged Arduino and Python serial extension installed on your C.H.I.P.:

sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install python-serial arduino arduino-mk

Create a directory inside your home directory to keep all your Arduino sketches organised:

mkdir ~/sketchbook

Now go into your sketchbook and copy an example project out of arduino-mk, so that it includes the example Makefile:

cd ~/sketchbook
cp -a /usr/share/doc/arduino-mk/examples/Blink .

Note the space and the dot at the end of that line!

Now go into the Blink project and see what files it contains:

cd Blink
ls -l

You’ll see that there’s both the sketch itself and a Make file:

-rw-r--r-- 1 chip chip 509 Jul 12 2014 Blink.ino
-rw-r--r-- 1 chip chip 61 Jul 12 2014 Makefile

There’s nothing unusual about the Blink sketch. It’s just standard Arduino code that you could compile with the regular IDE. The magic is invoked through the Make file.

Open the Make file in your favorite text editor, and you’ll see that it looks like this:

BOARD_TAG = uno
ARDUINO_LIBS =
include ../../Arduino.mk

The “BOARD_TAG” value specifies that you want to compile it for an Uno or compatible board. You can change this to match other boards found in the Arduino board hardware definition file, which is located at /usr/share/arduino/hardware/arduino/boards.txt. In that file you’ll find other handy board types that I’ll list at the end. For now, we’ll assume that you’re using an Uno and leave that unchanged.

The “ARDUINO_LIBS” value allows you to specify a path to your own custom libraries in case you need them linked during compilation. We’ll ignore that for now, so leave that line unchanged.

The “include” line is a problem. It assumes the project is still located in the shared location where it was installed by the package, but we’ve made a copy in our local sketchbook. To fix that, change the “include” line to read:

include /usr/share/arduino/Arduino.mk

There are many other options you can add to the Make file to override the default settings within Arduino. For example, you can define the path to a specific version of AVRDUDE if you want to bypass the version that’s bundled with the Arduino environment.

But for a minimal example, you don’t need to change anything else.

Now you can compile the sketch, and upload it to your Arduino. To compile the sketch without attempting to upload it, just type:

make

That’s it! Can’t be much simpler. If there are any compilation errors, you’ll see them in the terminal.

To upload (and automatically compile if necessary) make sure your Arduino is plugged into your Pocket C.H.I.P. using a USB cable, and type:

make upload

You’re all done! Your sketch should now have been uploaded to your Arduino.

There are other commands available as well. Check out the Arduino-MK documentation for more information:

github.com/sudar/Arduino-Makefile

Alternative board types

These are all the board tags list in the standard /usr/share/arduino/hardware/arduino/boards.txt file:

uno: Arduino Uno
atmega328: Arduino Duemilanove w/ ATmega328
diecimila: Arduino Diecimila or Duemilanove w/ ATmega168
nano328: Arduino Nano w/ ATmega328
nano: Arduino Nano w/ ATmega168
mega2560: Arduino Mega 2560 or Mega ADK
mega: Arduino Mega (ATmega1280)
leonardo: Arduino Leonardo
esplora: Arduino Esplora
micro: Arduino Micro
mini328: Arduino Mini w/ ATmega328
mini: Arduino Mini w/ ATmega168
ethernet: Arduino Ethernet
fio: Arduino Fio
bt328: Arduino BT w/ ATmega328
bt: Arduino BT w/ ATmega168
LilyPadUSB: LilyPad Arduino USB
lilypad328: LilyPad Arduino w/ ATmega328
lilypad: LilyPad Arduino w/ ATmega168
pro5v328: Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
pro5v: Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
pro328: Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
pro: Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168
atmega168: Arduino NG or older w/ ATmega168
atmega8: Arduino NG or older w/ ATmega8
robotControl: Arduino Robot Control
robotMotor: Arduino Robot Motor

You can also use custom board profiles, but that’s beyond the scope of this post! Most common Arduino compatible boards match something on this list. For example, the Freetronics EtherMega operates just like an Arduino Mega with a 2560 processor, so you’d select “mega2560” as the BOARD_TAG in the Make file.

Update 6 Jan 2017: Quick Setup Script

My friend Alec has written a script to simplify the process of installing the packages I used in the video and also to fix a few things he doesn’t like about the default setup. His script installs the necessary packages, blocks root access to the terminal and SSH, locks the root account, changes the default password, changes the hostname, and forces public key authentication via SSH. Definitely worth a look:

gist.github.com/alecthegeek/434325b6ea261ba84499f4966795073e

3 thoughts on “#16: Compiling Arduino sketches with Pocket C.H.I.P.

  1. Amazing. I can remember getting very excited reading the review in BYTE about a new “affordable” UNIX workstation back in about 1983 that only cost about as much as my parents house!

    And it would have been less powerful….

  2. So I got my PocketCHIP. Didn’t like the lack of security so I’ve created a setup script that also installs the packages above.

    https://gist.github.com/alecthegeek/434325b6ea261ba84499f4966795073e

    However people should copy and modify it to suite. I wrote to reflect my own needs

    1. Nice work, Alec! Thanks for posting this. I’ll update the page to add a link to it, so people who don’t read the comments will see it.

Leave a Reply

Your email address will not be published. Required fields are marked *