Use an IR LED and phototransistor pair to create a light beam switch. Point the components at each other to turn the switch on, then break the beam to turn the switch off. Use to detect when of your machine passes by a particular point. Or, bounce the light from the diode off a part to reflect back onto the detector. If the part is there, light will reach the detector and the signal can be passed to your Stamp. An IR LED/detector pair is exactly how your TV remote works. You can control your TV from across the room because the diode is pulsed briefly at a much higher current which gives off much more light.
The ME2011 robot store sells an IR LED/phototransistor pair, or you can purchase elsewhere.
IR LED: Radio Shack 276-143 ($1.69) or Jameco 106526 ($0.45). Connect like an ordinary LED using a 330 ohm series resistor to the +5 supply (or to a Stamp pin if you want to switch the source on and off). Current draw is about 11 mA with a 330 ohm resistor. Current runs from anode to cathode. Flat on the case marks the cathode.
IR Phototransistor Radio Shack 276-145 ($0.99) or Jameco 112168 ($0.45). A phototransistor is just like a regular transistor except the base lead is disabled or absent and light activates base current. The flat on the case marks the collector, the other lead is the emitter. Connect the collector to one end of a 10K resistor and connect the other end of the resistor to a +5V supply (you can use the +5 pin on the Stamp). Connect the emitter to ground. To view output, measure the voltage at the collector with your DVM. The voltage should start out at +5V. When pointing the IR diode at the phototransistor, the voltage should drop down to near zero. To interface with the Stamp, make a second connection from the collector to a Stamp pin (for the example below use Pin 4).
Telling them apart The IR LED and the IR phototransistor look alike. If you don't know which is which, you can try them in the test circuit described above, and swap the pair and flip the orientation of each until it works. There are only four possible combinations so it should not take very long. The cathode of the LED (the side that connects to ground) has a flat on the rim of the case. If you look inside through the clear plastic case you will see the tiny chip in the middle. The chip for the LED is square while the chip for the phototransistor is rectangular. This holds true only for some brands of devices.
Connections Use the figure below as a guide for connecting the sensor/detector pair. To test, point them directly at each other separated by 2-4 inches.
Test program Load this program which you can use to test the operation of the IR sensor
loop: debug dec in4 pause 250 goto loop
This is the simplest program you can use to test the operation of your IR sensor. Watch the numbers on the screen change between 1 and 0 as you block and unblock the sensor. If they don't change, measure the voltage at pin 4 while you block and unblock the sensor. If the voltage does not change, disconnect the wire going to pin 4 and measure the voltage on the wire (not on the pin). If it doesn't change, there is a wiring error or the sensor or emitter is bad. Note that the IRLED and phototransistor should be no more than two or three inches apart for this test.
Usage example Load this program into your Stamp:
switch: if in4 = 0 then turnon low 0 goto switch turnon: high 0 goto switch
Wire an ordinary red LED in series with a 330 ohm resistor to Pin 0. Wire the IR diode and phototransistor as described above. Hold the diode 0.25 to 3 inches from the phototransistor. The LED should light. Put your hand or a card between the diode and phototransistor and watch the LED turn off. For a slightly longer distance, drive the LED with more current. (A 100 ohm resistor will result in 38 mA, but be sure to hook it up to a separate +5 supply rather than a Stamp pin. Or, calculate the appropriate resistor and connect to a +12 supply.) Circuit is sensitive to ambient lighting. Can put each component in a dark tube to mask. Warning, if the circuit works perfectly in your room, it may or may not work in another location because the room lighting is different.