Tom Egan

The Many Ways to Run Code on an Arduino*

Back in 2005 there was just a single way to program an Arduino; the Arduino IDE. Today in 2019 the concept of “what is an Arduino&dquo; has changed from a microcontroller produced by a single vendor to arguably any board that can be programmed using the Arduino IDE. And in this time, a variety of methods for programming Arduinos* beyond the Arduino IDE have been released. The goal of this post is to list and compare some methods in use at Bucknell.

Arduino IDE

The Arduino IDE is the original method for programming Arduinos, in fact for most, it defines what an Arduino is:

* if a microcontroller can be programmed with the Arduino IDE it is an Arduino
The Arduino IDE is free software for PCs running Linux, Mac or Windows. Freshly installed it supports only Arduino brand boards along with a few common add-ons. Adding support for more boards and add-ons is as simple as instructing the IDE where to look for board support files then using the built in board manager or library manager to install new board support packages and libraries respectively. Programs are written in the Arduino IDE using C++ and can be split over multiple files though typically only one file with a handful of functions is used. The Arduino IDE is targeted at those learning to program for a microcontroller and while it comes with a built in serial monitor for communicating with microcontroller boards connected with a USB cable it lacks features that experienced programmers may be expecting such as a debugger or integration with git. The Arduino IDE also has a few quirks such as naming C++ files with the .ino file extension, a user interface which can be difficult to read, files hiding in odd places, and a requirement that programs to be placed in a specific location on your computer. It is also built with Java in order to support running on many OSes but therefore comes with all the baggage of Java.

PlatformIO

PlatformIO is billed as “an open source ecosystem for IoT development” and is targeted to the more experienced developer. PlatformIO is delivered either as a “core” toolchain only environment similar to the traditional Unix build toolchain, or as an extension to turn the Atom or Visual Studio Code editors into an IDE. While PlatformIO does build on many of the same open source projects as the Arduino IDE, a board or add on supported by the Arduino IDE may not be supported by PlatformIO and vice versa. One of the nice features of PlatformIO is for many boards the user has a choice of frameworks (predominantly C/C++) e.g Arduino, Espressif32. Some boards even have debugging support. PlatformIO also provides tools/commands for creating projects, managing dependencies, and automated build processes a.k.a Continuous Integration (CI).

CircuitPython

Unlike the previous toolkits, CircuitPython by Adafruit does not enable the user to program the microcontroller directly but rather it runs a minimalist python interpreter on the microcontroller which executes a user created python script. Because the python interpreter, even the lightweight MicroPython takes some amount of storage space and has some run time overhead ie the interpreter requires memory and processing power for itself, only a limited number of boards are capable of running CircuitPython. The big advantage of CircuitPython is however the more accessible programming environment. By composing programs in python, users do not have to worry as much about memory management, error handling and arcane C syntax errors.

Matlab

Matlab by Mathworks is a multifaceted product with many many capabilities. As such it is possible to program for an Arduino in a number of ways however the most common method in use at Bucknell is installing the Matlab Support Package for Arduino Hardware and using the Arduino object to communicate with a microcontroller installed with a firmware application provided as part of the support package. In essence this works like CircuitPython with an interpreter running on the microcontroller and the user supplied code written in a more accessible programming language than C. The big advantage of this approach is that it is very easy to pull data from the microcontroller and graph it or place it in a graphical user interface. The disadvantages are of course that Matlab is a proprietary software product that users must have a paid license to use and only a limited number of boards are supported. Additionally, the Arduino must be connected to the PC running Matlab either via USB or with select boards over a wireless connection because Matlab only executes microcontroller specifics tasks on the Arduino while most computation occurs on the PC and thus the two devices must talk continuously to stay in sync.

Simulink

Simulink by Mathworks differs from the previously discussed solutions in that it is a graphical programming environment as opposed to being text oriented. Simulink also provides two methods for running code. Like PlatformIO it can compile code to run on a microcontroller like the Arduino IDE and PlatformIO. It can also work in a hybrid configuration called “External Mode” discussed in a previous post. Like Matlab, in External Mode some code runs on the microcontroller while code that does not need to execute on the microcontroller executes on the PC. Also like Matlab the advantage is a possibly more approachable programming environment and the disadvantages are that Simulink is a proprietary software product that users must have a paid license to use and only a limited number of boards are supported.

Custom Built Interpreter

While there is no official support for doing so it is also possible to create a custom built interpreter using something like PlatformIO and then invoke commands exposed by the interpreter over a serial connection to the microcontroller from a Python program running on a Raspberry Pi or PC. This is a technique that we have only recently been exploring due to the unusual constraints of a particular project. But looking at the project we have realized that this gives us many of the benefits of using something like Matlab without some of the IT difficulties. The advantages are of course being able to use any board supported by PlatformIO yet moving most of the development into Python. Disadvantages are of course being tethered to a second computer and that the interpreter is home grown and unsupported commercially. However, I put together an IP free version which anyone could use as a starting point for their own solution.