Stepping up the Tech

After my last few posts, some of you out there in Internet-land are probably thinking, “Well that looks like fun! But I don’t have a Commodore 64. Is there any way I can make lights blink from my computer?” Sure you can! All you need is an Arduino!

The Arduino is a ridiculously popular option for “physical computing” projects. Hardly a day goes by without an Arduino-based project showing up on HackADay or the MAKE Blog. As a platform, it’s got a lot going for it: It’s cheap, it’s small, and it’s dead simple to use. But what exactly is it?

This Arduino circuit attaches to a PC via USB. With the help of a Python script on the host computer, it displays information about that computer’s activity. The brightness of the two green LEDs indicate CPU utilization (one for each core), and the red LED indicates the amount of disk activity.

An Arduino is a credit-card-sized board built around an Atmel microcontroller chip. It’s basically a one-chip computer, with its own RAM and some Flash memory for storing programs. Unlike a microprocessor, which has a bunch of pins dedicated to an address and data bus for accessing external memory, the pins on the microcontroller are meant as digital and analog inputs and outputs. With the C64, I needed to use the (built-in) CIA chip to do digital input and output, and I needed to add an ADC chip on the user port to do analog input. With the arduino, those abilities are built in.

The best part is that it has a USB interface, so you can just plug it into your computer and control it from there. It’s a pretty slick setup: There’s an Arduino development environment that runs on a PC. The programs (they call ’em “sketches”) are written in Arduino’s programming language, which is C++ with a few limitations and a bit of preprocessing. Compiling and downloading your code to the Arduino is literally a one-button process.

I’ve been skimming through some of the examples in the Getting Started with Arduino book to, um, get started. The last big example in that short booklet demonstrates two-way communication between the sketch running on the Arduino and a program running on the PC. The communication happens over a virtual serial interface running on top of the USB connection – when I plug in one of my two Arduino boards, it shows up as COM3:.

Annoyingly, the other one shows up as COM6:. I’m going to have to figure out what’s up with that sooner or later…

The sample program polls the MAKE blog and counts occurrences of the words “Peace”, “Love”, and “Arduino”. The sketch maps the word counts to 3 different colored LEDs, mixing their colors together based on something going on out there on the Web. It’s a cute idea, but it didn’t really get me excited. The MAKE blog’s RSS feed doesn’t change that often, so you’ll only notice the color changing over the course of hours or days. Plus, the PC side of the interaction was written in Processing, a Java-derivative language that I wasn’t particularly eager to learn.

To spice things up a bit, I decided to convert the project into an activity monitor for my computer. I wanted two lights to show usage for the two CPU cores, and the third to display disk usage. I also decided to write the PC part of the code in Python, a language I’ve been meaning to spend more time with for quite a while.

My Python code needed to do two things: 1) Collect data, and 2) spit it out a serial port. Conveniently, I found 3rd-party libraries to help out with both of those tasks. The psutil library collects statistics about CPU activity and I/O operations (and a bunch of other stuff besides), and the pySerial library handles the serial port (duh!). I very quickly put together a short Python script that did the job (source code below the fold).

I kept the output format of the original demo program, which is just the brightnesses for 3 LEDs expressed as hexadecimal numbers. The only tricky part was mapping the CPU usage percentage to a brightness value. A linear interpolation just didn’t seem very expressive. Using the square of the percentage gave a more informative result.

On the Arduino side, I started with the demo code. I rewrote it a bit for fun, but the only major change I made was to gradually change the LED brightness when a new set of values was received from the PC.

The circuit itself is a pretty trivial adaptation of some of the things I’ve been doing these last few weeks. There are LEDs hooked up to 3 of the Arduino’s digital pins, and an on/off button hooked up to another. The Arduino pins can be used as either inputs or outputs, just by setting the mode via software.

What’s striking about this is how easy it all was! If you’d asked me a year ago, I would’ve said something like this would be complicated to set up. The hardware and software just all seemed to come together very simply. It’s a great feeling when you get results from a little project like this. It makes you wonder what more you can accomplish with these tools!