Thursday, July 19, 2012

The many ways of programming Arduino

Arduino is arguably the most popular portable hardware development platform of the year. It's never been easier to create your own little device that scratches an itch. (Perhaps literally.) From controlling the temperature of a beer keg to unlocking your front door after a correct knock sequence, Arduino makes it possible, and best of all, cheap.

At its core, the Arduino platform offers reading analog data (0–5V) such as a temperature reading, reading and writing digital data (0 or 5V), pulse-width modulation for motors and such, and memory for your program. In essence, a typical Arduino program reads a pin (whether digital or analog), compares it to some value or a range, and writes some values to some other pins. Then repeat.

For example, suppose you have a mini-fridge that cools to 40°F, but you need to maintain 50°F, and the temperature dial doesn't go that high. (Problem courtesy of Jeremy.) Arduino to the rescue. Use the Arduino to combine a temperature sensor with a powerswitch. At a high level, your program would read the temperature from an analog pin (or a digital pin for a digital temperature sensor), then decide: if current temperature is below 48°, cut the AC power to the fridge by sending 0V to the pin that controls your powerswitch. Else, if the temperature is above 52°, re-enable the power by sending 5V. (Or whatever the manual for the powerswitch says to do.) Voila.

The programming tools I describe below support these basic operations like reading and writing pins. These operations appear either as functions or graphical "blocks" within the tool/language. So once you come up with the basic logic of what you want the Arduino to do, you can pretty much use any of these tools. The differences in the tools revolve around the programming methodology, strengths/weaknesses, and cost.
  1. Using Wiring, the official language of Arduino. It's basically C++. You can write this code in the official Arduino IDE.
  2. Using the QP Framework + QM modeler. Here, you design your software using UML Statecharts, then autogenerate code from that. It is free for non-commercial use, and surprisingly reasonably-priced for commercial use.
  3. Using the combination of LabVIEW and LabVIEW Interface for Arduino (LIFA). LabVIEW is a rather expensive ($1,200–4,500 for a single perpetual license) graphical development environment, and LIFA is a free add-on. The students in my Hardware/Systems course this summer used this programming method to develop fun Arduino projects.
  4. Using Simulink, a very expensive ($3,150, or $5,250 with MATLAB) graphical development environment.
Wiring is the most straightforward way to program your Arduino, if you have at least some experience with imperative programming. It's also the most "portable" way in that you can publish your code, and others can use it without any investment in tools.

The QP/QM method is my personal favorite. I am a big fan of software design, and I love having the implementation naturally flow out of the design. QP/QM, as I wrote earlier, makes it easy to develop complex yet maintainable and reliable software. Its reasonable price also makes it easy to move your hobby into commercial production. But, in my humble opinion QP/QM has a higher learning curve than the other methods I describe.

LabVIEW is a good introduction to programming. Its graphical ways are less intimidating than text-based programming. You have two screens, a front panel and a block diagram. The front panel has UI elements, and the block diagram expresses business logic. The business logic involves blocks, and wires that connect them. Data flows from one block to another. Blocks are either UI elements (source or sink) or programming constructs (such as arithmetic or Arduino commands). This approach makes two very important things easy:
  1. A versatile graphical user interface. More generally, it ingrains the separation of GUI and business logic.
  2. Understanding types, and type safety. (Thanks to wires and blocks of different colors.)
For a non-programmer, or a casual programmer, I believe LabVIEW offers the best mix of power and friendliness. My students this year, having no prior programming/robotics experience, developed some pretty impressive programs. The downside of LabVIEW, as I already mentioned, is pretty significant: the Arduino must be tethered to the PC at all times, since that's the only way to run LabVIEW programs on the Arduino.

I created a one-page LabVIEW Quick Start to help my students and anyone else who's exploring this route.

Finally, Simulink. I have not yet used this method of programming an Arduino. Simulink offers a similar graphical environment, with blocks and wires, except there is no user interface—only the equivalent of LabVIEW's "block diagram" for business logic. This is offset by the advantage that Simulink is able to deploy native code to the Arduino, so you can disconnect from the PC and run your Arduino on battery or AC power.

That's the overview of all the ways I know to program an Arduino device. If after reading the above you're still unsure which method to choose, here's a brief summary:
  • Are you already a programmer, or do you want to become one? If yes, start with Wiring and migrate to QP/QM once you feel comfortable.
  • Is a graphical user interface important for you? If so, use LabVIEW.
  • Do you need your Arduino to run standalone on battery or AC power? Use anything but LabVIEW.
  • Do you have money to burn? I mean, are you doing math more complex than I can think of a use for—on your Arduino? Use Simulink. (In all seriousness, I am not a Simulink user. If you know where Simulink outshines the alternatives, please comment here.)
Have fun!

4 comments:

  1. Thanks, I'll start with Wiring. Now I need time and something to build.

    ReplyDelete
  2. Also, did you find any websites to be especially helpful?

    ReplyDelete
  3. Arduino's wiki/tutorials are helpful. My biggest challenges revolve around circuits, and articles like this are a godsend: http://arduino.cc/it/Tutorial/Button

    ReplyDelete
  4. Mark, here's a first project idea for you. (It was developed by a two-person team in this year's HW/Sys course.)

    Assemble an array of 11 LEDs, with the center LED being a different color. Your program shall quickly cycle through all LEDs, lighting up exactly one LED at a time. Your goal is to press a pushbutton exactly while the center LED is lit.

    If you succeed, you proceed to the next level where the LEDs cycle a bit faster. If you push the button at the wrong time, either nothing happens or some counter is decremented, moving you closer to a disgraceful "game over".

    Fun for the whole family!

    ReplyDelete