Your Mac can already talk to all kinds of electronic devices, including digital cameras, camcorders, printers, external disk drives, mobile phones, and other computers. But there are plenty of other gizmos that your Mac can’t communicate with: motors, robots, external LCD displays, and sensors of all sorts.
For your Mac to talk to one of these items, it needs something to act as a translator.
That’s what microcontrollers do. Connect one of these programmable computers-on-a-board to your Mac’s USB port, and you can then hook an external device to the board and program it to tell that device what to do.
To show you what’s possible with microcontrollers and the Mac, I came up with a demonstration project that I call BreakTimer.
This system enables your Mac to detect whether you’re sitting in your chair and, if you’ve been sitting too long, to tell you it’s time to get up and take a break.
Assemble the componentsThere are three components to any microcontroller system for the Mac: the microcontroller board, its OS X software, and the hardware you’re connecting to the microcontroller.
The most popular Mac-compatible microcontroller board is the Arduino. It’s open source (you can get the schematics and build your own), it’s very afford-able ($35 for a fully assembled board), and it works wonderfully with Mac OS X.
The board itself is available from several sources; make sure you get the newer Diecimila model.
Once you have the board, you need the software that goes with it: go to this site, click on the Mac OS X link, extract the file that downloads, and then drag the resulting folder to your Applications folder. Next, install the board’s drivers. The most up-to-date driver software is also available; just make sure to get the right version for your particular Mac.
Connect the hardwareNow, if you’re trying my BreakTimer project, you’re ready to connect the seat sensor to your Arduino board. I’ve suggested some suppliers and part numbers, but you can source the hardware anywhere you like as long as it matches my description. Also, if you’re not already familiar with the process of wire wrapping, check out an online tutorial (alternate) before you start.
Once you’ve assembled your materials, cut two pieces of wrapping wire long enough to reach from your Mac to your chair. Using the wire stripper that comes with the wire-wrapping tool, strip about an inch from one end of each wire. Then, with the wire-wrapping tool, wrap one wire around each of the pins on the sensor.
Next, strip an inch from the other end of each wire. Using the wire-wrapping tool, wrap one of them to one lead of the resistor; wrap as high up on the lead as you can. With your cutters, detach a single pin from a row of male header pins. Wrap the end of the other wire to the short end of that pin.
Hardware Connections: This is the hardware you’ll be working with–the pressure sensor and a resistor connected to the Arduino board.Now it’s time to hook it all together: Plug the end of the resistor that’s connected to the sensor pin into the hole on the Arduino board labeled Analog In 0. Put the male header pin that’s connected to the other sensor pin into the hole marked 5V. And the end of the resistor that isn’t connected to anything goes into one of the two connectors marked Gnd. (Your board should look like the one shown to the right.) Make sure that the pieces of hardware in Gnd and 5V aren’t touching, or you might get a short circuit.
Finally, connect the Arduino board to your Mac through any standard USB A-to-B cable; avoid using a USB hub.
Program the softwareWith the hardware connected, launch the Arduino application. Open the Tools menu and select your board from the Board submenu. Then open the Serial Port submenu and look for an entry that reads something like /dev/tty.usbserial-A4001LBP. If there’s just one such entry, select it. If there’s more than one, quit the Arduino program and unplug any nonessential USB gear. When you restart the application, you should see just one entry beginning with /dev/tty.usbserial.
Now you’re ready to program the Arduino. The board uses a relatively straightforward programming language; if you’ve done any programming or scripting at all, you should be able to figure it out. (You’ll find a good reference guide.)
For this demonstration, I wrote a program (a sketch, in Arduino argot) that uses the sensor to detect how long you’ve been sitting down.
Begin by choosing File: New in the Arduino software. A window should appear with a tab at the top. Click on the right-facing arrow in the upper right corner of that window, type a name (in my example, BreakTimer ), and click on OK. Copy this code, and paste it into the code window.
Next, select File -> Upload To I/O Board. Some lights on the Arduino board (labeled TX and RX) should flash. When the upload is complete, the out-put pane of the Arduino program should show something along these lines: “Binary sketch size: 3046 bytes (of a 14336 byte maximum).”
Now it’s time to test your sensor: If you’re attempting my BreakTimer project, click on Arduino’s Serial Monitor icon and apply pressure to the sensor. A letter D should appear in the Arduino status window. When you release the pressure, you should see a U. Apply pressure for 15 minutes (you can adjust that to a shorter interval for testing purposes), and you should see a B. Click on the Serial Monitor icon again to stop monitoring these messages.
ASProxy at Work: Using Massimo Banzi’s free ASProxy, you can trigger simple AppleScript actions based on Arduino input.Now that the sensor is successfully communicating with your Mac through the Arduino board, you need to tell your Mac how to respond. One way is to use Massimo Banzi’s free ASProxy, which monitors your serial port for single-character messages and triggers AppleScript actions based on those messages (see the screenshot on the right for an example).
Or you can download the AppleScript add-in SerialPort X. After copying it into your user folder/Library/Scripting Additions, you can use it to write an AppleScript in Script Editor that monitors your serial port for input and triggers specific actions. Just make sure to save the script as an application and select Stay Open. (See a downloadable example of such a script for BreakTimer.)
The final wordThis project is just one demonstration of the many things you can do with a microcontroller connected to your Mac.
The Arduino board can talk to all kinds of sensors and other devices available from places such as SparkFun Electronics and Trossen Robotics. For more ideas, check out the Arduino site, particularly the Playground and Exhibition areas.
[Brian Jepson is the executive editor for Make magazine’s book series and the editor of Making Things Talk (Make Books, 2007).]