Getting acquainted with Arduino

— 10:54 AM on October 21, 2011

I tend to think of myself as a well-rounded geek. Excluding the keyboard-cowboy day job, my attention is divided between hardware tinkering and putting that hardware to good use via software. Over the years, I've invested most of my energy into commanding data via PHP, with the occasional deviation into Python and C# territory, depending on the project. While it's been an interesting ride, I've reached the point where simply manipulating data is feeling somewhat redundant and, frankly, a tad boring. I need action. I want to write functions that return explosions instead of Booleans. I want my code to reach outside of the box and manipulate physical objects in the cluttered room around me. I want... Arduino?

I've been hearing about this Arduino thing for a while now. I read up on the more intriguing projects that propagate amongst various technology blogs from time to time, but I've never worked up the courage to go hands on. My recent bouts with coding monotony have finally pushed me past the point of no return and into the arms of this mysterious little circuit board.

The Arduino team describes its creation as an "open-source electronics prototyping platform." In plain English, that means they've strapped an inexpensive microcontroller onto a small PCB and added convenient headers that enable easy access to the chip's pins. In addition, they've provided an integrated development environment (IDE) for programming the controller chip and support for an enthusiastic community of hobbyists who know the device inside and out. That's oversimplified, of course, but simple is the name of the game.

Even though the onboard controller chip performs various calculations, the point of Arduino isn't to be another tiny computer board. Instead, it's intended to choreograph the actions of LEDs, sensors, servos, and whatever else you see fit to jam into its input/output headers. To quell any doubts, the Atmel ATMEGA328P chip on my Arduino Uno board is an 8-bit RISC-based device, operating at a break-neck 16MHz, and packing a whopping 32K of Flash and 1K of SRAM. Sandy Bridge? I think not.

Because Arduino is more interested in monitoring simple input values and voltages and supplying an appropriate output value or voltage, it doesn't need a boatload of compute power. This underlying simplicity was one of the major points of attraction for me. For instance, when you slot a video card into your PC, it's communicating with more than a few of the thousand-or-so pins on your CPU. There is so much going on in there that most enthusiasts give up trying to understand exactly how it works—and just care that it does work. With Arduino, things are simple enough that wires are essentially attached directly to individual CPU pins. The visceral thrill of combining this direct connection with a datasheet that describes what each pin does is hard to explain. It makes an electronics newbie like me feel as if I'm hanging out with the hardware hackers of the 1970's, cobbling together an expansion card for the Altair 8800.

From here on, I promise that I won't waste any more time reveling in undeserved delusions of hardware-hacking grandeur. Instead I want to discuss the basics of Arduino and share some pointers with others interested in learning the ropes. While it's a little off the beaten track for us here at TR, the DIY possibilities of this platform are virtually endless, constrained only by imagination, and on occasion by the 8-bit microcontroller.

Because Arduino is open-source hardware, there are many versions of the platform available. You can even buy the board as just a pile of discrete components and solder it together yourself if you really want to. For me, starting out at ground zero with no cache of miscellaneous electronics components at my disposal, a complete Arduino starter kit seemed like the best route to go. The kit that came home with me was the Sparkfun Inventor's Kit for Arduino. This particular kit comes in a small tackle box that includes a pre-assembled Arduino Uno board, a bunch of LEDs, a breadboard, patch wires, resistors, an assortment of various sensors, a USB cable, some other miscellaneous odds and ends, and a nice user's guide with 14 learner's sample projects. These projects focus on the parts included with the kit, and they allow new users to get up to speed building circuits quickly. If you have never worked with the Arduino platform before, I would strongly recommend looking into such a starter kit.

Shortly after opening the box and inspecting its contents, I began assembling the first training project. The inaugural circuit consisted of a single LED and resistor plugged into the breadboard, which was in turn connected to the Arduino board and programmed to blink at a constant interval using the Arduino IDE. The project was extremely simple and straight-forward, but it illustrated the basic concepts of Arduino very well. By the end of the project, I became overly excited and skipping ahead to latter lessons to see how transferring data over a serial link worked and how to utilize sensors and potentiometers. In hindsight, I would recommend following the projects in order for the most part.

The second major ingredient in Arduino's secret sauce is not hardware related, but lives on your PC in the form of its integrated development environment. Arduino uses something akin to C++, but it's been tweaked to include many built-in functions for reading and writing to the Arduino's I/O pins. Coming from a PHP background, the coding process felt familiar enough, and the learning curve to accomplish some basic tasks was minimal. The IDE provides a quick and simple method of compiling, debugging, and uploading your code to the Arduino's Flash memory. Because the program (called a "sketch" in the Arduino vernacular) is saved to non-volatile memory in the controller chip itself, your projects can run untethered from the computer so long as there is a power source available. When connected to a computer, the Arduino gorges itself on the 5V power supplied by the USB port, but battery packs and wall-wart adapters are also viable options.

Within the IDE is a decent library of example sketches that can be directly uploaded to the Arduino. For example, this simple sketch is used to blink an LED:

/* Blink Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain. */

void setup() { // initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);

void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second

In the above example, we first define an output pin on the Arduino board connected to the LED's positive power wire. Pin 13 is configured as an output in this case. Next, we start the main loop. This is an infinite loop, as implied by its name.

In this example, there are only four lines of code in the loop that will repeat over and over until either the power is removed or the program is overwritten with something else. The first line, "digitalWrite(13, HIGH);", tells the Arduino to assert a high electrical voltage for pin 13. Because the LED is connected to this pin, it will illuminate for the duration of the delay() function called right below it. "delay(1000);" is equal to one second (1000ms) here. "digitalWrite(13, LOW);" does just the opposite of its predecessor and tells Arduino to stop a voltage on pin 13. This command causes the LED to turn off for the duration of the delay() statement below it. Since there is no more code after that, the program jumps back to the first line and starts over.

The LED is now blinking on and off at a one-second cadence. By adding more high/low/delay logic and tweaking the delay times, you could make the LED blink at any rate you wish, from a playful Morse Code "SOS" to a seizure-inducing strobe.

The items included in the starter kit are just the beginning. At my local Micro Center, Arduino-related goods are relegated to a small, dark corner near the book section. Even so, you can still find expansion components like GPS modules, barometer modules, humidity sensors, SD card readers, Bluetooth adapters, small LCD screens, and much more. I opted to pick up a small, 2-line-by-16-character LCD display during my last visit. Using the LCD libraries already included with the Arduino IDE, it is relatively easy to display the obligatory "hello world!" or any other message. Not all add-on hardware is supported out of the box, but there is a good amount of support for the most common components.

If you have the time, money, and electronics know-how, Arduino can be put to any number of clever uses. I've seen RC cars, Quadrocopters, kegerators, gaming devices, and many other projects built using this platform. With Ethernet connectivity enabled, things get even more interesting. Using this functionality, one can send trigger signals to an Arduino using anything from incoming tweets to web-based commands sent from a cellphone. PC builders could, perhaps, set up a contraption to monitor the temperature of a certain zone in a computer case and activate a fan or warning indicator if it rises above a defined threshold. Arduino can also be used in conjunction with servo motors to push physical buttons or to create intricate robots. The sky is the limit.

This is admittedly an extremely high-level overview of the Arduino platform. I still have much to learn about it, but I am certainly happy with my investment so far. If there is enough interest in this topic within our community, I'd like to hear some of your thoughts on potential project ideas. If something really good (and viable) gets thrown out there, it could make for an interesting feature article some day.

Tip: You can use the A/Z keys to walk threads.
View options

This discussion is now closed.