Posts Tagged ‘lab’

Physical Computing Lab 1

Thursday, September 4th, 2008

In less I hate the world news, here’s my first real project at ITP. Physical Computing Lab 1 is really more of a “get your feet wet” lab than anything else. The linked lab there is actually more complex than the one we actually were requested to execute.

We’re using the Arduino chipset and codebase for digital control, and the first project was just a “learn about digital design, do a tiny bit of programming”.

We were asked to design a system that, when a switch is activated an LED flashes on and off; when the switch is not activated, the LED stays off.

Most people come out of high school with at least a rudimentary understanding of electricity and thus know how to right a switch-to-light bulb circuit. That’s the first lesson that has to be unlearned for people doing digital work, and that’s probably the important bit (beyond “you can do it”) that this first lab teaches. See, in digital circuit design, inputs and outputs are independent circuits. Instead of rigging the switch to the light circuit, you design a switch circuit connected to the controller and a LED circuit connected to the controller. Their only link is that controller. It looks something like this:

Conceptual Diagram

This means that two separate circuits need to be designed: an LED circuit and a digital switch circuit. The LED circuit is simple, and doesn’t require explanation of the difference between analog and digital switching, so let’s start with that.

An LED circuit is almost precisely identical to the classic light bulb circuit. It takes power from some source, feeds it through an LED, and runs that to ground. You’ve probably seen this before. There, is, however, an important exception here. See, the current that the Arduino puts out is actually too high for a standard LED and would burn it out. This means we want to slow that current down so we use a resistor. 220ohms is about right for our purpose here, so we end up with a circuit that looks a lot like this:

Diagram of the LED circuit

Of course I didn’t have a 220ohm resistor handy, so I did the next best thing: I grabbed a pair of 100ohm resistors and linked them in series. When you line resistors up like this in a circuit, you add their resistance together. It’s quite convenient. So the actual circuit I built looks like this:

Alternate LED circuit diagram

My actual design is very exciting. It looks like this:

Image of my LED circuit

With the LED circuit completed, the next step was to design a switch circuit. The traditional analog switch is simple: a switch with a power source at one end and ground at the other. Unfortunately, digital switches are a bit more complex. See, digital switches are constantly looking for an input. If they sense one then they are “on”, if there is no input then they are “off”. For analog purposes an open switch doesn’t let enough current across to do anything significant, but in a digital switch system an open switch may still allow static electricity or power generated by magnetic interference flow to the sensor, confusing it. This means that all digital switches need to be grounded in order to keep them at zero current when they are open.

It’s the “when they are open” part that gets problematic because we need to ensure that when the switch closes that the current doesn’t flow into the ground, but into the digital sensor. We accomplish this with a huge, ginormous resistor. When the switch is open there is a single loop: sensor to ground. This means that no matter how big the resistor is, the loop stays closed and the sensor is grounded out. When the switch is closed, however, the current from the switch has two potential directions: to the sensor, or to ground. By putting a large resistor on the ground side we ensure that the power flows to the sensor, switching it on.

Another diagram (it’s worth noting that in this particular circuit the system is so low on resistance that you can use just about any resistor you want here):

A diagram of a digital switch

Since I didn’t feel like getting out an actual switch, I built my own… sort of. I simply left an exposed section of wiring at the end of my power lead, and another one between the resistor and the micro controller. The switch is “closed” whenever I touch the exposed wires together, and “open” the rest of the time. It looks like this:

This totally \"awesome\" switch I designed.

Now all we have to do is connect our two separate circuits to the controller. We already know how to do this with the switch since it’s wired to an input, and doing it with the LED is just as simple. Since we want the LED to be an output of the circuit instead of an input, we want to connect the power end of the LED circuit to one of the controller pins.

What we get looks something like this:

Combined circuit diagram

What we have here are two circuits linked by a micro controller. The first circuit is a simple light: when the controller gives the signal, the light turns on. The second circuit is a simple switch: the controller constantly listens to see if there is power flowing across the switch or not. Using my amazing breadboard-less skills, it looks like this:

The modern sculpture of my complete design!

Now that we have our exciting digital system, we need to do something with it. There are all sorts of possibilities now because we have a switch and a light. We could make the light simply turn on when the switch is closed, we could make it flash, we could even make it flash at a rate based upon how long the switch has been closed. For now let’s just do something simple, but at the same time let’s do something that actually requires a micro controller to do. That means no flash rates based on how long the switch has been closed, but it also means no simple “close the switch, turn on the light” since we can do that in pure analog systems. Leaving us with the second option: “when the switch is closed, the light pulses”.

With the circuit designed, the actual command system is entirely software-based. Arduinos use a very simple codebase, so here’s all we have to code up:

void setup()
pinMode(2,OUTPUT); // Set this pin as an output,
// It is where we plug in the LED
pinMode(3,INPUT); // Set this pin as an input,
// It is where we plug in the switch

void loop()
if(digitalRead(3)) // If there is current on the switch's pin
digitalWrite(2,HIGH); // Output current to the LED
delay(1000); // Wait one second
digitalWrite(2,LOW); // Stop outputting current to the LED
delay(1000); // Wait one second
} // Repeat the loop code indefinitely

Load that code into the Arduino and you’re all set to go. With a little work, you too can have an amazing circuit like mine. Here’s a nice 30 second video of it in action: