Monday, October 29, 2012

32 Short Explanations About the Arduino (Not Featuring Glen Gould)

There is this tool called the Arduino.  That is how I think of it; as a tool.  I think of it as no more complicated to learn than a bandsaw.  And like a bandsaw, it is useful for some jobs, not so useful for other ones.

The most important thing I try to put across to the non-technical person is that the Arduino is not particularly technical.  It is designed to be easy to learn.  It is self-contained; getting into Arduino means nothing more than getting an Arduino.  You don't discover half-way in that you need some other part or some specialized tool or you need to learn a whole process the existing user base are so used to they don't even think about any more.

Really.  You open the box it came in (mail order, Makers Fair, Radio Shack, etc.), plug a standard USB cable in one end, download the free (multi-platform) software which installs painlessly with a couple clicks, and up pops a window where you type the code.


Add USB cable:

Start the software and load the "Blink" example:

Okay...there are two aspects to this that might seem more "hairy" to someone just getting into it.  The first is that you have to attach the things you want to read or control to the Arduino.  And that means you have to learn just a little about voltages and polarity and the difference between a power line and a data line.

The second is that you do have to program.  If you have never programmed before it probably seems daunting.  Really, though, first-year German is tougher.  Programming in a friendly development environment like Arduino is more like using an English-German phrasebook.  There are plenty of example programs already in the installation, and more if you look around and ask questions.  And you can get a lot of projects working just by finding a sample program and changing a couple of lines.  It may look like hell and win no awards for good programming practices but who cares; the point is that it works.

My own experience is that I knew how to solder and once decades ago had written some really simple stuff in Tandy-BASIC.  And I'm not a very fast learner.  But I got my Arduino working and did a pretty complex task with it in under a week.

The thing I want to  make clear about this "tool" thing is the fact of the much-touted Digital Revolution.  You have heard the phrase "Microcomputers are in everything these days."  Well, they are, and the reason is, it is cheap.  And it is good engineering; using a micro is often simpler to develop, maintain, and expand.

Not all of these advantages translate for the hobby user, but the general idea is still there; it is often faster and cheaper to do it in software.

Say you have a project with a couple of blinking LEDs.  You could use the venerable 555 timer to make a blink circuit:

(Image courtesy of Wikimedia Commons).

 That's two resistors, two capacitors, and the 8-pin DIP to solder up. 

And here's the software equivalent:

 (Actually, only a couple of lines are the blink code; the rest is boiler-plate.)

Even for something as simple as this, although an Arduino board is nuclear overkill, it may still be the optimum solution for the project.  Because you can design for cheaper components, more efficiency, smaller footprint, engineering elegance, etc.  But any real-world project includes the constraints of reliability and fabrication time as well.  And for many of us, weighing twenty minutes soldering up a 555 on a perf against twenty bucks for an Arduino clone that already includes on-board 5V/3.3V regulator...

And when you add that changing the rate or the dwell is a matter of typing a new number (instead of going through R/C calculations, soldering new components, adding a trim-pot to the equation, etc.)...

Because once you add a micro to the project, you have automatically multiple input lines (with debouncing), output lines, as well as the ability to sleep for power saving or reset after time passes with no input, and the ability to create complex sequences of activities and directly link them to each other...

Because a 555 circuit can blink.  The equivalent Arduino circuit can blink a Morse Code message that is several pages long.

 At this point there are probably readers who are muttering about BASIC stamps, baby programming, cargo cult engineering, consumerist culture and gross inefficiency.

The heck with them.  I come from theater.  Our only question is "Does it look good from forty feet away, and will it hold up until closing night?" 

But more seriously, avoid the siren song of perfect optimization.  Optimize as far as is necessary and practical.  And stop. 

And, yes, the Arduino is far from the first, or the only, microcomputer of this kind.  The BASIC stamps from Parallax were an early favorite, and the Raspberry Pi is making waves right now.

Why I like the Arduino is that it really is training wheels, and training wheels of a good kind.  The Arduino hardware is built around the ATmel-made AVR microprocessors.  It is a standardized wrapping of that hardware with basic clock crystal, power regulation, programming ports, and I/O headers.  And it is open hardware as well as being open source.

The Arduino IDE, although far from a robust code development environment, is really just a java wrapper for the compiler, with a few macros to simplify parts of the code.  It isn't, unlike some systems, giving you a misleading picture of what is going on "under the hood," limiting your access, or forcing you to work within a unique and/or proprietary framework.  It is teaching you C.  Phrasebook C, perhaps, but C.  And you can, even from within the artist-friendly IDE, easily pass some bog-standard C down to the metal itself.  Heck -- you can poke direct values to the registers if you want to.

And you aren't constrained to live within the original profile, either in hardware or software.  I've used the Arduino IDE to throw software on to not just minimalist ATmega168 boards, but to the ATtiny85 buried within a BlinkM!  In fact, the Arduino IDE interferes so little with the process of getting working code onto an AVR chip, I find myself turning to it as often as I go direct to the avr-gcc toolchain.

The, perhaps sticking points with some, about the common Arduino hardware/software, is that an expensive FTDI chip is soldered on to every board, and a bootloader takes up a chunk of the available program ROM, both "just" so the board will answer easily to a personal computer on the other end of a USB cable.

But you don't have to accept that as a default or as a limitation.  Go via an FTDI cable or AVR-ISP.  Delete the bootloader if it really bothers you.  And use one of the many, many "-duino" variants that are built without the FTDI or USB or voltage regulator or even clock crystal.

Here's a typical Arduino clone (a Freeduino, in fact:

Here's a minimalist Arduino built on Adafruit perma-proto board (includes an 8-channel Darlington; that's the second DIP):

Here's a much messier minimalist Arduino in the process of being assembled with the parts to drive a 7-segment Vacuum Fluorescent Display:

And, yes, you can actually run quite a bit of Arduino code on the ATtiny series of microprocessors from the same manufacturer; here's a target board for ATtiny 13/45/85;

But if that isn't small enough for you, then go surface mount; the Arduino Mini or Nano, or an Arduino-compatible like the Fio or the Ardupilot, or a Teensy or Bumble-B:

Okay, so we have an Arduino.  How do we make it talk?  How does one, practically, connect to the outside world?

It depends on what "outside world" you are talking about. 

There is within the Arduino community a variety of "Shields."  These are add-on boards that stack on top to provide such things as LCD displays, tiny keyboards, sound cards, MIDI jacks, etc.  I mention these because they are extremely easy to use; just, literally, stick it on top.  Most of them are well-supported with code libraries so using them is pretty much plug-and-play.

I don't have any pictures because all my shields are hand-grown.  I'll show some of those in a moment, but I didn't want to scare the reader.

Because for a lot of applications, it can be as easy as sticking wires into the holes.

Here's the guts of my "Square Candies that Look 'Round":

The idea was to have a couple of small (fake) candies sitting on top of a box.  There is a Sharp IR sensor in the front of the box.  When any object (like a passing child) got within about half a meter of the box, the candies would spin around to show the faces I had painted on one side.

There is no soldering involved here.  There are no additional components.  Just the sensor, and the two servo-motors.

To make this thing, I took two miniature servo-motors out of the bag they shipped in, cut holes in the top of a cardboard box, and stuck them in place with hot glue.  Then I took the $12 Sharp IR sensor out of the bag it came in, cut a square hole in the front of the box and tacked it in place with hot glue.

Then I took all the wires and jammed them into some of the holes on the header of that standard Arduino you see there.

Servos have three wires.  Two have to go to power; those went to the clearly marked 5V and Ground holes.  The last is the data line, and it requires PWM.  Don't even worry about that that means; just know that only a few of the I/O lines of the Arduino support PWM natively, so look it up (or look at the silkscreen on the Arduino).

Similar for the sensor; the sense lead had to go to an analog input.

I wrote down which pins I was using.  Plugged in the USB cable and opened the Arduino IDE.  Opened a servo example and changed the pin number to match the pin I'd plugged mine into.

And, yes, there was a little more to the code than that, but getting the I/O to work -- getting a reading from the sensor and getting the servos to turn to the desired position -- was pretty much just opening an example and changing a few numbers around until it looked right.

Added a little black tape to keep the wires from pulling out, and the project was done.

Here's a slightly more complex-looking one:

This is my oft-used MIDI Button.  No internal battery; I usually run it off any USB power supply (a computer or a phone charger).  The jack on the right there can take a standard wall-wart power supply or a battery in a box...anything from about 6 volts to 12 volts DC.  There is a MIDI output jack on the far side, a test button and activity light on the top, and the screw terminal on the back is how I connect remote buttons to it.

Here's a glimpse inside:

All the wires from the project box are connected to short bits of pin strip, which are plugged into the standard Arduino headers in turn.

It isn't neat, but it gets the job done.

(If you look close, you can see there's a loose XBee node on a breakout board floating around inside.  That's using the 3.3 regulated power the Arduino can also supply on the header, and makes the MIDI Button able to receive a signal from my Wireless Staples Easy Button.)

There's a grand total of three discrete components in the whole box.  Each leg of the bi-color LED has a ballast resistor soldered to it, and there's a single resistor on the MIDI port as well.  Other than that, the entire thing...from closing a switch fifty feet away to a standard MIDI note event being managed by sticking bare wires into the slots on the Arduino's headers.

I did mention shields.  Here's a double-decker sitting on top of my Freeduino:

That is another good example of nuclear overkill.  I mostly did it this way because I already had the proto-shield wired up, and I only had about six hours to work.  The proto-shield has standard headers that fit on to the Arduino headers.  It also has a smallish strip-board style space in which to solder parts.

This shield is set up with two parts; a second header for a small RF receiver board, and a ULN2803 Darlington array.  The board drove three sets of high-power LEDs in various patterns under radio control.

This one, at last, has a bit of real soldering in it.  But the electronics layer is still dirt-simple; the Darlington array handles all the buffering itself, meaning all you have to do is attach outputs from the Arduino to the input leads, a power source to the "supply" lead of the Darlington array, and your 60ma loads to the output leads.

Because the whole idea is to leverage something that is simple to hook up and reliable enough to work for "long enough" and get you back to making art.  Or cute little robots for the stage.

No comments:

Post a Comment