Perl + Arduino (+ ShiftBrite)


Back when I revealed the Twitter Monkey I had a dark secret… I got the Perl script to work, but just barely, and I really didn’t grok exactly how the code worked. I was just thankful MCQN Ltd. published the Alertuino code for me to get started.

I’m happy to say that I’ve made some good progress in getting Perl and the Arduino to talk to each other, and if you keep reading, you’ll see what I’ve got so far.

I’ll be using a ShiftBrite from macetech for this example. You can buy direct for $4.99, get them from Adafruit for $5.00 or use your #freeday money to get one from Sparkfun.


To connect the ShiftBrite, you just run it’s GND to the GND on the Arduino, the V+ to the 5V on the Arduino, and the run DI, LI, CI, EI to digital pins 10, 11, 12, 13 respectively, on the Arduino. Just 6 wires… pretty simple.

Once again, I stand on the shoulders of others… a hacker named Ashley Hughes wrote a post titled ShiftBrites and the Arduino and provided a library named shiftbritehughesyarduino. Grab it and drop it in your Arduino/libraries folder before we get started.

The library makes it pretty easy to send a color to the ShiftBrite using a command like this:


Where the colors are in R,G,B value. Though the scale is not 0-255, or 0-100, but 0-1023. I’m not great at math, so I’m going to use the 0-100 scale and just multiply by 10. So for the line above we’d be sending a RED value of 200, a GREEN value of 400, and a BLUE value of 600.

Here’s the code for the Arduino:

 * ShiftBrite.pde

#include "HughesyShiftBrite.h";

HughesyShiftBrite sb;

void setup() {
  sb = HughesyShiftBrite(10,11,12,13);

void loop() {

  int input =;

  switch (input) {
  case 48:
  case 49:
  case 50:
  case 51:
  case 52:
  case 53:
  case 54:
  case 55:
  case 56:
  case 57:

See all those “case” commands followed by numbers? They are looking for the ASCII code being sent from the serial port. Don’t have an ASCII chart handy? Look at this simple one and you’ll see that the decimal value for 0 is 48, for 1 is 49, etc. So our case statements are looking for anything between 0 and 9, which is a nice scale to use.

OK, if you’ve uploaded the code, you should see the ShiftBrite lit up, since the initialization sent this:


The code on the Arduino is now listening to the serial port waiting for some input. The input should be a number between 0 and 9. We should probably send it some numbers… that’s where Perl comes in:


use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/tty.usbmodem1d21");


for ($i = 0; $i <= 9; $i++) {
	print $i . "\n";


This is the shortest, simplest example I’ve got. You’ll obviously need the Device::SerialPort module installed. If you’ve written anything in Perl (or other languages) this should make some sense. We’re connecting to the serial port (/dev/tty.usbmodem1d21) and sending characters (0 through 9) to it, as well as printing them to the console so you can see them.

When you run the Perl script you should see the ShiftBrite light up and change about once per second, cycling through various levels of blue, green, and red.

Note: We got the port /dev/tty.usbmodem1d21 from the Arduino IDE (though I’ll show you another way to get it) and the sleep command is in there to give things time to initialize. I’ve found that without it, the serial port communication may miss the first commands.

ShiftBrite: Red, Green, Blue

Gotchas: The serial port may change. Mine is ‘/dev/tty.usbmodem1d21′ but it will be different on different machines, and may even change after a reboot. We’ll have a fix for that next time. The other gotcha is that when the Perl script is running, it’s using the serial port, so if you try to upload a new sketch to the Arduino, you will get an error. Since this script only runs for about 13 seconds, you probably won’t hit that problem here… for scripts that loop, you probably will.

I hope that wasn’t too complex. In theory, you could write the Perl part using Ruby, Python, or any scripting language that can do serial communications. In the future I’d like to try to use seriality to see if I can do it via HTML/JavaScript.

In a future installment I’ll have a complete project using all the bits we just covered…. stay tuned!

See Also: Arduino + ShiftBrite Light Organ


Charts & Graphs

I’m in need of graphing some data, and since I looked at the Google Chart Tools long ago but never did anything interesting with it, I figured I’d give it a spin.

Google’s API is pretty simple to use, you feed it a URL, and you get an image in return. Here’s an example expanded out a bit:

Each parameter controls some part of the image you get returned. (Want to see it? View the URL.)

You should get something that looks like this:


Wow… but not really. This is probably one of the simplest examples of a chart, but it serves my purpose.

Of course, since I’m not always content relying on others to host my data, I wanted a way to generate and store the image. You can do that too…

curl -o 20101130.png ',5,5,5&chco=ff0000&chxt=x,y&chxr=1,0,100,5|0,0,24,1&chd=t:3,3,3,3,3,3,3,65,38,46,60,59,56,58,36,21,13,62,2,3,3,3,3,2'

Just use curl to make the request and store the image in a file named 20101130.png (or whatever the date is, or file name you’d prefer.) Obviously you’ll need curl installed. If you need to generate a new chart everyday this is ripe for automating.

I also looked at jQuery Visualize as an option. It’s quite different than the Google Chart API as to how it functions.


With jQuery Visualize you don’t actually create a chart. Well, you do create a table of data, and jQuery Visualize does all the heavy lifting and creates the chart based entirely on your well-formed tabular data.

I sort of like this approach because there’s no external image files to generate, host, or worry about. You just make a table, and include a bit of Javascript. (Of course there are other concerns/issues, but simplicity is pretty high.)

I’m still evaluating what my final choice will be for my current project. If this were 10 years ago, I probably would have used Perl to generate the image files. If it were 5 years ago, I probably would have used Perl to generate some SVG files…

jQuery Visualize uses the HTML 5 Canvas element, so they get some points in the “innovation” column I guess… Also, jQuery Visualize requires less math, and in my world, the less math I have to do, the better!


Arduino: The Novel (nanowrimo)

Arduino: The Novel Last year I published a novel for National Novel Writing Month but because I am lazy and a terrible writer, I just wrote a script to write the novel for me. (The results were posted here: National Novel Writing

This year I did it again, and I’ve also published the code I used to write the novel. (See nanowrirobot on GitHub.)

The script needs a source to do it’s work. I did my original development by just pointing it at /usr/share/dict/words which creates some of the most beautiful gibberish you’ve ever seen. You can also just point it at a text file, and it’ll use the words in that. That’s what I did last time, and this time as well. Since this one is titled “Arduino: The Novel” it had better be about the Arduino, right? I ended up creating a word lists from the HomePage, Introduction, FAQ, and ArduinoBoardUno pages of the Arduino web site, and built a list of just under 1,000 words. The results were astounding…

    Written determined appropriate users Flash read SRAM. Protection a board also conditions or By. Lines making process short features draw preceding. Changes functionality please When power edge Not. What For bootloading we’d Windows inputs provides. Try manufacturers screw tutorials dimension teachers avrdude. Without want Single-Sided By leap released diagram. Which Cross-platform their directly pre-assembled bus 3V3. They inexpensive our Google downloaded by Debian. NG module off This standard Ground former. Additional short suggest do based tool Java. Upper commercial length ie up unofficial off. Current cool locally falling Mega Can supported. Board platforms Frequently conveniently map series Circuit. You’re compiles Auto You’ll general OSX compiler. On-board derivative troubleshooting documentation More Stamp Using.

And that’s not all!

    Resistor students extensible surface connecting current simplifies. Possible until Physically name pin number first. In each RESET-EN having inclusion be permission. Outputs explain taken try language help uploading. Discuss protection Mini have Linux to press. Some Be moved along are press process. AnalogReference start step-by-step USB-to-serial protection CAD code. People chip STK500 emphasize might lights OS. Regulator at overwriting works extending pull-up ideas. Comes supply Each requires chip Open-source flash. Sounds limited call timeout AC-to-DC switches mark. Enhancements step-by-step their off name! Hungarian facilitate. Wall-wart we’d permission Reference tool Additionally passed. Back Eagle preassembled support describe COM distributors. Principles Forum via powered boards page multimedia. Corresponding particular Vin conveniently layer addition overheat.

Wow! Have you ever read a computer generated novel about a microcontroller with such suspense, drama, and intrigue!?

And look! You can download it in a variety of formats:

Actually, it’s pretty terrible, unless you are a fan of random acts of writing. If you want some writing that makes sense, take a look at, where you can find information about novels written by real people.

If you’re more interested in microcontrollers (specifically the Arduino) maybe you want to read about a Trashcan Accelerometer, or Baker Tweet, or a steampunk bandwidth meter


Defending Perl

I stand here today not to condemn Perl, but to defend it… Perl

And when I say “Perl” I want you to feel free to substitute any other language you see fit… Visual Basic, Fortran, Lisp, COBOL, Smalltalk, or even… Java.

I was given a programming assignment this week. Given a list of 2000 names, I was to provide 400 names randomly. (Yes, this was for work, and was a real-world assignment, not just some exercise.) I proceeded to open my ~/scripts folder, which contains well over 1,000 Perl scripts, and grabbed some existing code, and wrote some new code, and ran a few tests, and quickly had the list of 400 names.

I used Perl because it worked for me. I probably could have written in it PHP (as it’s another language I am pretty comfortable with) but since I’m more familiar with Perl, and have done very similar thing before with Perl, and had a gigantic library (nearly 15 years worth) of Perl scripts to pull from… I used Perl.

I’m sure others could tell stories just like this substituting Java, or Python, or Erlang, or whatever they have around, that they are the most familiar with… and that’s fine.

This is about using the appropriate tool for the job, and this case, with the job being performed by me, the tool was Perl.

I wasn’t creating some new framework, or building upon an existing application, or pushing some code to GitHub that I wanted others to collaborate on… it was a quick ‘n dirty task that needed to get done as fast as possible.

Much of the programming world (maybe it’s mostly in the web-programming world) gets hung up on the latest and greatest. There’s a chance that using Ruby someone could have completed this task in half the time… but that person would not have been me, as I’ve spent probably less than an hour using Ruby and I’m not familiar enough with it to bang out the needed code quickly. I’d like to have some time to dig into Ruby (or, whatever, pick a language) and get familiar enough with it to use it effectively, but I don’t know when that will happen.

I’m not even a programmer… I’m a hacker, and I think Perl is a hacker’s language. Make something work. Solve a problem. Do it quickly, and move on.

To some degree you could say I’m trapped in Perl, because it is so easy for me to get what I want done quickly, and yeah, there’s a module for almost everything… Where I mainly feel the pain is when I look for example code and narrow my search to Perl (or PHP) because I’m most familiar with them. I needed some code this week to do some serial port communication, and found only one good example written in Perl. I should probably task myself with re-writing the code in Ruby, or Python, or some other language I’m not as comfortable with as an exercise in forced learning.

And then there’s a part of me that just says “screw it, I’m gonna work with what I know, and get things done, and move on.”


Pretty Print XML with Perl

Let’s say you’ve got a file named “file.xml” and want it pretty printed, all indented nice and everything…

For just such an occasion I have a Perl script named “” and I just run my XML file through it like so: cat file.xml | perl

Here’s the code I use:


use XML::Twig;
use XML::Parser;

my $xml = XML::Twig->new(pretty_print => 'indented');



You can even pass it through right as it comes in over the wire: curl | perl

Here’s an example of data from Foursquare without pretty printing. (I used curl to grab the data. Also, I added in some line breaks, just to make it a little more readable.):

<?xml version="1.0" encoding="UTF-8"?>
<created>Mon, 09 Aug 10 00:50:33 +0000</created>
<name>The Kiltie</name><primarycategory><id>79067</id>
<fullpathname>Food:Ice Cream</fullpathname><nodename>Ice Cream</nodename>
<display>Pete P. @ The Kiltie</display></checkin></checkins>

And here’s the same data, again using curl to grab it, and then passing it through the script:

<?xml version="1.0" encoding="UTF-8"?>
    <created>Mon, 09 Aug 10 00:50:33 +0000</created>
      <name>The Kiltie</name>
        <fullpathname>Food:Ice Cream</fullpathname>
        <nodename>Ice Cream</nodename>
    <display>Pete P. @ The Kiltie</display>

I still find Perl extremely useful for this sort of task… I’m sure there are other command line ways to do this, but this one works for me.

(Hat tip to A Curious Programmer where I picked up this Perl code from…)