Wednesday, January 11, 2012

Getting to Hello World with Arduino

I am not a hardware person. This admission might come as a surprise to my software buddies, who I am always boring with long rants about how things really work under the hood. My hardware buddies would all laugh at the very idea that I might ever claim to be a hardware person. The fact is, I haven't handled discrete electronic parts with intent to build since my undergraduate computer engineering classes circa 1975. That was back when a resistor was the size of a refrigerator. I confess too that I've always been a little jealous of a former colleague and office mate (Hi, Dan!) who was always doing really cool stuff with tiny microcontrollers.

I decided it was time to do something about my ignorance, under the auspices of my latest project, code-named Amigo. I ordered a SparkFun Inventor's Kit for Arduino!

SparkFun Inventor's Kit

Arduino is an open-source/open-hardware project to create a platform on which to teach electronics and embedded development and to use as a basis for simple (or not so simple) multidisciplinary applications of small microcontrollers. Several versions of the Arduino board are available with variations in the microcontroller and available peripherals. Arduino includes a graphical integrated development environment [1] that supports a C-like language [2] and includes a library of functions [3] to control the various peripherals on the board. It has been widely applied to all sorts of cool stuff from controlling sensor networks in real-life laboratory experiments to animating Christmas lights. Arduino has become so popular that an broad ecosystem of hardware and software has become available based around the various versions of the board.

(SparkFun is an online electronics retailer based just up the road in Boulder Colorado. It seems to be staffed with people who love doing interesting things with electronics. Their web site is worth a look. I am merely a satisfied continuing customer.)

The Inventor's Kit is a plastic box full of embedded fun that includes an Arduino Uno (revision 3) board with an Atmel ATmega328P eight-bit RISC microcontroller [4] (I'm already getting excited here), a small breadboard for prototyping circuits, a USB cable that connects the board to your PC or MAC for both control and power, and a selection of electronic components ranging from LEDs to a solenoid and about which I know nothing.

SparkFun Inventor's Kit: Contents

Fortunately it also includes an instruction booklet. The booklet has a series of fourteen projects of increasing complexity that use the components in the kit.

SparkFun Inventor's Guide: Instruction Manual

I immediately set off to get CIRC-01 "Blink LED" working. The Kit comes with a series of overlays that you lay right on top of the little prototyping breadboard that tell you what to plug where, and the instruction booklet tells you why. It also includes the code to get the project working. The intent here is to teach you the basics about how the microcontroller and each individual component works so you can later unleash your inner mad scientist and use Arduino for your own nefarious purposes.

CIRC-01: Getting Started - Blinking LED

In no more than a couple of hours, I managed to (1) assemble the little SparkFun prototyping fixture, (2) install the software off the web onto my Mac, (3) hook the fixture to my Mac via the USB cable and get it to power up, (4) poke a hole in my thumb and get blood all over everything, (5) wire up the circuit diagram, (6) type in the handful of lines of source code from the booklet, and (7) do a pretty good imitation of a fat old guy who wrote some software to blink an LED.

Here's the entire setup. That's the Arduino IDE on the Mac display on the left, and the tiny SparkFun fixture with the Arduino board on the right, with a USB cable providing both power to the fixture and communication with the IDE on the Mac. Yes, that's a magnifying class at the lower right. Did I mention I'm old?

Arduino and SparkFun Inventor's Kit

Here's a screen snapshot of the IDE. (You can click on any of these images to get access to a larger version.)

Arduino IDE on the Mac

And here's a closeup of the board with the LED blinking. Huzzah!

CIRC-01

Trust me, if I can do this, anyone can do this. If you've ever wanted to learn some basic electronics and microcontroller programming, or if you have a hankering to put your latest evil experiment under digital control, this could be the platform for you. (You might skip the step requiring medical attention though.)

Update (2012-01-12)

[1] Not satisfied with the documentation nor the Wikipedia page on Arduino, I naturally downloaded the tarball of the open source code from the web site and sucked it into an Eclipse project. The IDE and related host-based tools are written in Java.

[2] It's actually C++, but the run-time environment presents a much simplified API to the developer compared to other C++ environments with which you may be familiar. It might be too simple for a really complex application. But if you're that motivated you will, like me, want to dig under the hood anyway. Once you do, you'll see a lot of stuff familiar to anyone accustomed to doing embedded development. The lowest level routines to manipulate the hardware reminds me a lot of code in bootloaders that I've had to hack.

[3] The API you'll use to do the projects in the Kit is written in a way such that you could be excused if you thought it was Java. I admire the design because it is obvious that a lot of thought has been put into make it as simple as possible for someone who perhaps has only done some Java development to write useful embedded applications. You have programmatic access to the serial port on the microcontroller that connects to your desktop via USB. There is a console tool included in the IDE that gives you access to it, so you can for example print stuff to help you debug.

[4] Data types: char, byte and boolean are eight bits; int and word are sixteen bits; long, float and double are thirty-two bits. The basic register set of the processor is thirty-two eight-bit registers. If you're anything like me, you want to know this.

1 comment:

Craig Ruff said...

I've had fun using the AVR chips for some embedded projects. Helps protect my aging brain from the brain damage caused by trying to understand the instruction set of the smaller PIC variants.