Applying micro controllers to the real world often involves sensing motion, position or the proximity of objects. A very simple sensor is the optical switch consisting of an IR LED emitter and an IR-sensitive phototransistor. These can be arranged to detect directly transmitted light (photo interrupter) or the reflection of light (proximity detector) and they can be packaged into a self-contained sensor or used as separate emitter / receivers. The photo below shows an interrupter type and a proximity detector type that may be purchased for less than a buck a piece.
The interrupter is often used to sense rotary motion. With one sensor, the rotational speed of a shaft can be calculated; using two sensors, the speed and direction of rotation can be determined. The interrupter may also be used to determine position of items moving in a linear path. The illustration below shows an interrupter used as a limit switch to detect when the slide has reached the end of its travel.
The proximity type can detect the presence of an object, such as a finger. Imagine one of these behind an IR-transparent opaque panel. Move your finger near a spot to turn a device on, and near another spot to change modes, all with no controls visible.
With a little ingenuity, these sensors can be applied to many different applications!
The sensors are cheap so they must be difficult to use, right? Actually, they couldn't be much easier to use! One resistor to adjust the LED current and a second to load the transistor are all that's needed. Fairchild Semiconductor's AN-3005 - Design Fundamentals for Phototransistor Circuits
shows two methods of connecting these sensors. I've used the more common collector resistor arrangement in the circuit shown below with aVishay TCRT5000 reflective proximity detector
purchased from eBay for a couple bucks for ten. These are offered by many vendors on eBay for around the same price.
The schematic shows the circuit I used. A 100 ohm resistor provides about 26 mA to the LED. A 5k resistor is the collector load, which the above link recommends as a good value to be sure the phototransistor operates in the switching mode. The values here should work well with most sensors of either the transmissive or reflective type - they all seem very similar in the requirement s for the transistor and LED.
The circuit is shown on a breadboard above.
To test this simple circuit, I used the logic tool function of the PICkit 2. High states are indicated by a 1, low states by a 0. Pin 6 of the logic analyzer was connected to the optical switch circuit. With noting near the sensor, the collector resistor pulls the output high and a high state / 1 is shown. With my finger within about half-an-inch (12 mm), IR reflected by my finger turns the phototransistor on, pulling the output to near ground, resulting in a low state / 0 being shown on the logic tool.
This switches have IR-filters, so they don't respond to daylight much (direct sunlight may swamp them out). They are sensitive to IR light from any source. A convenient sources is an IR remote control. Switching to the PICkit logic analyzer mode and pointing a handy IR remote at the sensor resulted in the following trace showing the remote control pulses.
I'm going to use one of these proximity sensors to detect if the sliding door is open for my fan controller. The edge of the sliding glass door will be in front of the sensor if the door is not open far enough to operate the fan. The reflected IR from the door will activate the phototransistor, resulting in a low output.
Not bad for 25 cents worth of parts. Optical sensors with a transistor output will all operate similarly to this. Some sensors are logic level output, in which case the circuit is even easier, eliminated the collector resistor.