exhibit research | technology


Basic Stamp, Basic Stuff
A brief introduction to physical computing


Joseph Stein is Embedded Interaction Designer at the American Museum of Natural History. A sort of master chef of multimedia, he uses ingredients such as sensors, switches, and microcontrollers to cook up exhibits that feast the eyes, ears and brain, while remaining every bit as nutritious as more mundane fare. Here he gives a quick introduction to prototyping circuits, using some simple electronics including the Basic Stamp microcontroller, to interface computers with the real world.

This all may appear a bit daunting to the uninitiated, but fear not. You need not follow along to the letter; I publish this mainly to show how simple it is to get started in "physical computing" -- giving computers more types of appendages to sense, and affect, the outside world. The Basic Stamp, sold by Parallax, is taught to schoolchildren and requires only the most rudimentary knowledge of electronics. It is a great way to get started in robotics or simply experimenting with lights and sensors. See also the theory page for some good introductory physical computing links.


Q: Now, explain how this breadboard works.



JS: The breadboard is very clever. These middle lines here are connected horizontally, then there's a break in the middle. So you can see how the Stamp is put in, all of its pins have their own row. Along the sides, these rows are connected, but vertically. The holes along the red line are all connected, and same with the blue. Same on the other side.

The first thing you should do when you're putting together a breadboard is to connect voltage and ground on both sides -- ground to ground, and 5 volts and 5 volts.

Q: Those are on the same place on both sides.

Yeah. People have different ways of organising it -- some people ignore the colours, and just say that everything on the outside is going to be 5 volts, everything on the inside is ground. For me it's just as easy to follow the colour code that they have set out for you. I don't mind adhering to that.

Take a look at the schematic in the Stamp documentation. The TX and RX pins are send and recieve, then Attention and Ground, and these are all primarily for transmitting the program to the Stamp. But you can actually use those pins, so don't forget that if you ever get crammed for pins.

The fourth pin needs to be grounded, so we'll ground it. The second one down on the right side is also Ground, so we'll connect that to ground. The top one on the right is Power, so you can plug your DC voltage directly into the Stamp and use the on-board regulator you would plug your power in there. In our case we're going through a power regulator to keep the Stamp cool, and make sure we have a steady power source, so we're using the direct power pin, fourth down on the right.

There are 5 volts out coming from pin 21, fourth down on the right. This powers the Stamp. Now we're basically ready to go.



You can prepare a DB9 serial connector to download the Stamp programs. This is what it looks like. On all of these, the pins are numbered. This one on the far right is pin 5, then there's Attention, Transmit, Receive. The two leftmost ones on the bottom are always connected, otherwise the Stamp program won't download. We can plug this in, knowing that pin 5 is always Ground.

Now we can put in a few things. The rest is up to our discretion. Let's put in a light. I've just chosen a pin here; this is pin 11, and we've stuck a resistor in there, and now we're going to ground it. On LEDs, the short end always goes to Ground.

Let's try this out with the program; it's a good first step. Plug in your serial cable, power up the Stamp, and we'll try just turning on pin 11.

In the Stamp software, there are preferences to set the Stamp model you're using. We'll just do a quick little program:

     main:
          high 11
          pause 100
          low 11
          pause 100
     goto main

As you're probably guessing, we should get a nice little blinking LED. That seems to be working, so we can start building on this circuit some more.

We can leave everything in place. Let's add a variable resistor -- this is another key component. This one will be a photoresistor. And we'll also add a switch. You always have to have a resistor in your circuit, with a switch.



Q: Is there a reason you put it there, in particular?

No. In fact, I always find it more intuitive putting the resistor to ground, though it's not necessary. You can actually use an LED as a resistor, but most of them only take a couple of volts, so you'll need to add a resistor to that too. We can make our switch light up an LED. Remember again that the long side of the LED goes to power. This is a momentary switch, and it's normally off, which means that right now it's off, and it's only on when you press it on -- it doesn't stay on. You can see, even before we program anything, that the switch turns on the light.

Now, in the program, we should be able to say, in from pin 7, and make a variable called "switch."

     switch var byte

     main:
          high 11
          pause 100
          low 11
          pause 100
          switch = IN7
          debug dec, cr
     goto main

We've labeled our variable, to make it byte-sized. "Dec" just turns it into a decimal, and we've added a carriage return. When we run it, we see a constant stream of zeroes. Then you see ones when we have it pressed.



The next thing to do is to put together the potentiometer, or variable resistor. We're just going to use pin 8 as our input. This is a small capacitor, 0.1microfarrad. One pin goes go ground, and the other, the plus side, is power.



Now, you can put any variable resistor into this little circuit, and you'll get numbers out. This time we're putting in a photoresistor. You'll notice that both ends of it go to ground, and that means you have to have power going through it somehow, and that actually comes from the Stamp. So you make that pin "high" in the program -- the pin you're actually detecting on.

I'm going to turn off the "debug" on the switch. We use "rctime" to get sort of a fake analog-to-digital for the Stamp. We're basically counting how fast it takes for the capacitor to charge up. We choose the pin, in this case pin 6, it looks like. And then choose a variable name. We'll call it "photores". Label it up at the top, and we'll make this a "word," which makes it thousands of possibilities instead of just 256. Now let's "debug."

     switch var byte
     photores var word

     main:
          high 11
          high 6
          pause 100
          low 11
          pause 100
          switch = IN7
          'debug dec, cr
          rctime 6, 1, photores
          pause 10
          debug dec photores, cr
     goto main

So now, when you pass your hand over it, you get all kinds of numbers. Of course, this is completely dependent on the lighting situation.

Let's now get that input into the computer. You can use one pin each for transmit and receive. The receive pin should have a resistor, because the Macintosh serial actually has a little bit of extra voltage on it. It should be fine without the resistor, but it's better if you have it. Then there's ground, and you'll need one more pin for that.

Serial commands are incredibly easy. It's just one line of code. There's "serin," you choose the pin number, and then a strange number which corresponds to the baud rate, a comma, then your variable name. Basically, what you're doing is saying, "Take the serial in from this pin, at this baud rate, and dump that data into this variable.

But there's a problem with "serin." There are times when a Stamp will actually hang on "serin," and it won't proceed to the next line of code if it's not getting anything, or not getting the right thing, depending on what you've set the receiving variable to do.

"Serout," on the other hand, seems to be just happy. That's the corresponding command, where you're sending data out to the computer. That never hangs up, it just spits out stuff, no problem. Whenever you're first trying things out, it's always good to do "serout" first. And you can send many things at once, just put a comma after each variable.

So let's do that with our program. We're receiving on pin 8, so we're sending out on 9

     switch var byte
     photores var word

     main:
          high 11
          high 6
          pause 100
          low 11
          pause 100
          switch = IN7
          'debug dec, cr
          rctime 6, 1, photores
          pause 10
          debug dec photores, cr
          serout 9, 16624, [switch]
     goto main

In Macromedia Director, we use Geoff Smith's SerialXtra to get data into the computer from the Stamp. He gives you all the code you need, and you can just change it to fit your needs.

Kevin Walker