A desktop or laptop computer has a number of standard interfaces to support interaction with the user. Input is typically via keyboard, mouse / trackpad, microphone and webcam; output via screen and speakers. These interfaces work quite well for able-bodied people engaged in clerical work, but they are relatively limited if you want to use the computer in some kind of nonstandard interaction, installation or performance, or if you wish to engage other sensory modalities or bodily movements.

One way to extend the interactive possibilities of your computer is to attach a microcontroller. This allows you to gather information from a wide range of switches and sensors and to control lights, motors and other kinds of external hardware. In this tutorial we will introduce the Phidgets InterfaceKit; the following tutorial is devoted to Arduino. In both cases, we will be using the microcontroller as a peripheral device, something which will remain connected to your desktop or laptop via a USB cable. When you push a button or turn a knob connected to the microcontroller, information is relayed to Max, running on your main computer. You can also send signals from Max to the microcontroller, which are then used to switch lights or motors on or off, or to control other kinds of hardware.

The PhidgetInterfaceKit 2/2/2 is a tiny board that plugs directly into a USB port on your main computer and provides 2 analog inputs, 2 digital inputs and 2 digital outputs. The instructions that we give here can easily be modified for use with other InterfaceKits, some of which will be used in later tutorials.

The first step is to install the Phidgets software for Max 6 (Mac OS X | Windows). When that is complete, you can connect an LED to digital output 0 (green wire), a normally-open switch to digital input 0 (yellow wire), and a potentiometer to analog input 0 (one of the red, white and black cables). The potentiometer that we are using in the tutorial is the Phidgets Rotation Sensor. Both the switch and the LED also have to be grounded (connected to the black wire). The connector diagram for the 2/2/2 is shown in the next picture.

The complete circuit should look something like the following. Note that both the LED and the switch have one lead connected to ground. For the switch, it doesn’t matter which lead goes to the yellow wire and which goes to the black wire. The LED will not work if you plug it in backwards, however. The LED lead that is connected to the black wire is the cathode. It is usually shorter than the other lead (the anode) and there is a flat edge on the LED body  above it. When the switch, potentiometer and LED are connected to the 2/2/2, you can plug it into a USB port on your computer.

The patch for working with the 2/2/2 is shown below. The raw code link is here.

The first thing I like to do when working with Phidgets is to send a ‘getSerial’ message to the device. It should respond by sending a five- or six-digit number to the number object on the rightmost outlet of the route object. (The serial number of the 2/2/2 I used for this tutorial was 268367 but yours will be different).  I then send a ‘getStatus’ message. If the device responds with a 1, it is ready to go. Click the ‘start’ message before continuing.

We can send a digital output (i.e., an ‘on’ or ‘off’ signal) with a ‘setoutput’ message. To turn on digital output 0 (the green wire), send the message ‘setoutput 1 0’. To turn off digital output 0 send the message ‘setoutput 0 0’. As you click these two messages, your LED should turn off and on. You are now using a Max patch running on your main computer to control the state of a hardware device in the world. In this case it is a physical LED, but it could be anything. Note that these two messages also send the value 0 (off) to digital output 1. A better way of communicating with the digital outputs is to use the toggle, pak and prepend objects as shown in the patch. Then you can change one digital output without affecting the state of the other one. Try checking and unchecking the leftmost toggle. Your physical LED should turn on and off as you do so.

If you press the physical switch connected to digital input 0 (the yellow wire), the leftmost led object in the Max patch should light up. When you release the switch, it should turn off. The signal is being relayed from the 2/2/2 to the main computer. A route object splits up the digital inputs, analog inputs and status messages. The digital inputs are then unpacked and sent to led objects. You now have a way of sending the state of physical switches from the world to a Max patch running on your computer.

Finally, if you turn the potentiometer (rotation sensor) attached to analog input 0, the leftmost dial object in the patch should turn with it. The angle of the potentiometer shaft is encoded as a number between 0 and 1000. This arrives at the dial via route, unpack and flonum objects. You now have a way of sending a continuous stream of values from a sensor in the world to a Max patch on your computer. Future tutorials will build on our ability to exchange digital and analog information between Max and the outside world.

Here are some useful resources