The Best way to program an Arduino Microcontroller from the Linux Command Line Interface

Updating arduino code over the internet hasn’t been easy for me before now. In this post I will show you how to update 200+ different microcontrollers from the Linux Command Line Interface (CLI) using PlatformIO. My specific example (see instagram video below) will be a Teensy 3.2 using a Raspberry Pi, but you can repeat it for many other combinations with a little effort.  This method is specifically for the times when you have no Graphical User Interface (GUI) What-you-see-is-what-you-get (WYSIWYG) into the remote machine such as TeamViewer and all you have a bare bones text interface to the remote device.

Arduino and Raspberry Pi

People often ask me “should I use an Arduino-type microcontroller or should I use a Raspberry Pi for my robot project?” I often say “Both!”

The Arduino is a dedicated piece of hardware with it’s own clock, great for precision timing and simple jobs.  Think of it as the back part of your brain that controls breathing, heart rate, and autonomic functions. It doesn’t have the go juice to run a web connection.

The Raspberry Pi is a more powerful device that juggles many jobs at once.  You can’t demand precise timing for robotics because it is running many threaded tasks together at once.  Think of it as the front part of the brain that deals with language, communication, and interfacing with other people.  It can even run simulations of other machines in it’s head, just as you or I can imagine each other.

Together these two make a tasty robot sandwich.

What about ESP8266?

Sure, you could use a microcontroller that’s powerful enough to run a webserver and move some motors… consider the edge case where you need many Arduinos (or mixed types of boards!) running off one Pi.  Maybe you built your Arduino project and Raspberry Pi is an afterthought.

Ok, Enough Already, Show Me!

Setup the Pi and Arduino hardware

Hook the Pi to a monitor and keyboard, then power it on with a usb micro cable. Connect Raspberry Pi to internet with CAT-5 cable or a USB Wifi dongle. Setup Raspberry Pi with your favorite distro. I chose the default Raspbian installation. After the automatic reboot I did not change the settings. You may find it useful at this time to run the ifconfig command, which will tell you the IP address of the Raspberry Pi if it is on the network. The monitor and keyboard can be removed when setup is done.

Connect Raspberry Pi to Arduino with USB.

Power on Raspberry Pi.  If your Arduino has motors and more, you may need a separate power source for those.

Connect to the Raspberry Pi

In Windows I use the PuTTy client to connect over SSH to the RPi.  You’ll need the IP address of your RPi.  If you do not know the IP address of your RPi, consider using the nmap tool to find it.

nmap -v -sn 172.16.6.1-10

worked well for me.  I knew the machine was in the 172.16.6.* block, and that the block was mostly empty.  The device appeared at 172.16.6.2.  Your IP address block may vary.

When connecting to the Pi you will be asked for a name and password. The default name is “pi” and the default password is “raspberry”.

Install PlatformIO

The magic is PlatformIO, “an open source ecosystem for IoT development”.

Once you are connected and logged in to the Pi, you’ll want to copy the line below and past it into PuTTY. Pasting is most easily done with a right click of the mouse in PuTTy.

sudo python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)"

This one line command uses curl to grab a python script (py) and then run the python interpreter with admin privileges (sudo).  It’s going to be a while.  Maybe use the time to write someone a thank you letter.

Setup PlatformIO for your board(s)

by default when you log into a Raspberry Pi you are in the home directory for user pi (~/).

platformio boards teensy

Asks PlatformIO to list all supported boards in the teensy category. From there I figured out teensy31 was the right board name to install support for my teensy 3.2. You could just as easily ask for plastformio boards arduino.  Now I’ll make a folder to hold my board-specific project.  Again,

mkdir myProject
cd myProject
platformio init --board teensy31
platformio run -t upload

PlatformIO init will download and install the code needed to run the teensy 3.2.  Again, this is a wait.  Call your mother and tell her you’re sorry for that thing you did when you were 9.

PlatformIO run will download and install even more stuff.  Make a meal! clean a thing.  Help a friend with their hackspace project.

Now PlatformIO will probably tell you that there is no code to install so upload isn’t happening.  That’s OK!  Your code should go in ~/myProject/src/.  PlatformIO is smart enough to recognize dependencies and grab them, most of the time.  For example, my LED wall project used the WS2911 library available in Arduino.  PlatfomIO just grabbed it and took care of everything.  All I had to do was hit the reset button on the Teensy at the right time.

Final thoughts

In order to program the Teensy the reset button has to be pushed once.  I imagine there’s a clever way to use a RPi’s GPIO pins to briefly pull the Program pin to ground, triggering a reset.  Then the button would not have to be physically pressed, making it easier to work remotely.  Arduino boards like UNO and Mega do not have this requirement.

Like, share, subscribe

If you found this useful, or know someone who would, please share it.  If you use this knowledge in your project, post about it to our forums so we can share your work with all our friends, too.  Either way… stay awesome!

See also

http://docs.platformio.org/en/latest/installation.html
http://docs.platformio.org/en/latest/quickstart.html