Make: More Electronics (2014)
Chapter 31. Experiment 31: Electronic Optics
Two basic types of sensors are triggered by variations in light. There is the active type and the passive type.
The Chronophotonic Lamp Controller that I described in Experiment 7 used a phototransistor, which is a passive sensor. It just sits and waits, measuring light from outside sources and changing its effective internal resistance accordingly.
Another common passive light-sensing component is a PIR motion sensor, where the P in the acronym stands for “passive” and the IR stands for “infrared.” These are commonly used to switch on lights or trigger alarm systems when they sense body heat from a person moving around.
PIR motion sensors are useful, but the complete units that you find in hardware stores don’t leave much room for experimentation. If you want to play with this kind of sensor, it would be better to buy one on a breakout board, as shown in Figure 31-1. Hobby-electronics sources, such as Sparkfun, sell them for less than $10 at the time of writing, and they are designed for direct connection with microcontrollers. For a truly excellent tutorial on using them, visit http://www.ladyada.net. I won’t be dealing with PIRs here, as I think active sensors have more interesting possibilities.
Figure 31-1. A passive infrared (PIR) sensor mounted on a breakout board to facilitate experimentation.
Active Light Sensors
Instead of just sitting and watching the ambient light around it, an active sensor emits some light of its own—almost always infrared, although a few ultraviolet sensors exist. Active sensors are found in copy machines, where they detect paper jams; in industrial automation, where they sense the progress of a product through a manufacturing process; or in robotics, where they verify the positions of moving parts.
Usually the light beam is generated by an infrared LED that operates in a narrow frequency band and may be modulated to distinguish it clearly from other sources. Some circuitry behind a nearby detector (usually, a phototransistor) is tuned to the same frequency.
The general term for a combination of light emitter and light sensor is an “emitter-receiver” combination, and there are two variants:
The reflective type of emitter-receiver:
§ The LED and the phototransistor are mounted side by side, pointing in roughly the same direction. An example is shown in Figure 31-2.
Figure 31-2. This reflective emitter-receiver consists of an infrared phototransistor mounted beside a matching infrared emitter. Light from the emitter bounces back from a reflective object positioned a short distance away, and the phototransistor measures any variation caused when something moves to interrupt the beam.
§ You have to position a reflective surface, such as a piece of silvered mylar or a white object, to bounce the light from the LED back to the phototransistor.
§ The range is usually very limited. Many reflective sensors expect the reflecting surface to be about half an inch away. There are exceptions to this rule, but they are likely to cost more.
The transmissive type of emitter-receiver:
§ The LED and the phototransistor face each other across the gap in a single U-shaped mount, as shown in Figure 31-3.
Figure 31-3. In this Everlight ITR9606-F transmissive sensor, light is transmitted across the gap to a phototransistor facing it in the other half of the component. The schematic symbol for a diode is just visible, molded into the righthand section of the plastic. An infrared LED is mounted in this part.
§ If an object breaks the light beam, the phototransistor changes its output.
§ In many components of this type, the air gap is about a quarter inch wide.
§ A transmissive type of emitter-receiver is less versatile than a reflective type but very easy and convenient to install.
§ This type of sensor is sometimes referred to as an “optical switch” or an “opto-interrupter.”
In the upcoming experiment, I’m going to show you how to use a transmissive sensor, such as the Everlight ITR9606-F. After you test it, I’ll suggest some applications. But first—
Warning: Slow Sensor Death!
Active IR sensors are “always-on” devices. So long as the system is powered up, the infrared LED is emitting light.
This entails some power consumption (usually around 10mA to 20mA), but that isn’t the major issue. The sad fact is that infrared LEDs tend to deteriorate with usage. Some datasheets may warn you that the light intensity will diminish by 50 percent over a period of about five years. Other datasheets say nothing about this, but it is still likely to happen. The deterioration may be heat-related, or may be linked with the particular chemical process that is used in manufacturing. My reading on the subject suggests that deterioration is still not thoroughly understood, but everyone agrees that it happens.
If you use an active IR sensor, try to adjust your circuit with a large margin of error so that it will continue to work for as long as possible while the light from the LED diminishes with time. Also try to minimize the current that passes through the LED.
The infrared emitter in a transmissive sensor is an LED that requires a relatively low forward voltage—typically around 1.2V, and not greater than 1.5V. You’ll need to add an external series resistor to protect the LED, because no resistor is included inside the component. The resistor value must be chosen according to the voltage of your power supply.
§ Check the current consumption of the infrared LED, as well as its forward voltage. Adjust the series resistor so that the current is close to the value described as “typical” in a datasheet. You don’t need (and don’t want) the current to be up near the “absolute maximum.”
The infrared receiver is usually a phototransistor with an open-collector configuration—just like a Hall-effect switch. As before, you are expected to use a pullup resistor with the open collector. The only question is, what value of pullup resistor is appropriate? The datasheet doesn’t always say, but it will give a maximum value for current that can be allowed to sink into the open collector. A “maximum” value may be 20mA, but a “typical” value may be much lower. Consequently, you should not expect to drive a (normal, visible-light) LED directly from this type of sensor.
Infrared Sensor Test
Now you have the orientation, let’s test this thing! If you are using the ITR9606-F, the pinouts are shown in Figure 31-4. But how do you know which way around the sensor is? The Everlight ITR9606-F makes it easy for you by embossing the symbol of a diode in the plastic. You can see it on the right in Figure 31-3 if you look carefully.
Many other sensors have similar specifications, and their datasheets will show you the functions of the leads. Some of them are the same as on the ITR9606-F, but in others you’ll find one pair of leads is reversed.
The experiment here should work with any transmissive optical sensor, provided you proceed methodically, get the pin functions right, and avoid passing excessive current through the component.
Figure 31-4. Pin functions of the Everlight ITR9606-F. Other sensors with similar specifications may have the same pin functions, or one pair of pins may be reversed. Check datasheets for details.
In Figure 31-5 I’m suggesting that you test the sensor with a quad two-input OR logic chip. The reasons for this will become clear as we go along. A picture of the breadboarded circuit is in Figure 31-6.
Figure 31-5. Schematic for testing a transmissive optical sensor in conjunction with an OR logic chip.
Figure 31-6. Breadboarded version of the test circuit for a transmissive optical sensor. The U-shaped sensor is at the top, just above the trimmer potentiometers.
Note that I’m using a 5VDC regulated power supply, because the OR chip requires this.
When you wire the circuit, leave the positive power supply to the chip unconnected initially, and also leave the wire to the collector of the phototransistor unconnected. The first step is just to power the infrared LED.
Infrared LED Test
Remember, you aren’t going to see any light emitted from the LED inside the sensor, because it’s outside the visible spectrum. You must rely on your meter to tell you that it’s working.
In Figure 31-5, adjust the 1K trimmer to its maximum resistance. (Check it with your meter before you plug it into the board, if you are in doubt.) Now measure the voltage between points A and B. It should be between 1VDC and 1.2VDC.
You also need to measure the current that the infrared LED is drawing. Remove the jumper that connects point A in the circuit and the positive bus. Set your meter to measure mA and use the meter to connect point A to the positive bus. As you turn the trimmer, you will see the meter reading change. You want a reading of about 10mA. The LED is rated for more, but because of a future application that I have in mind, I don’t want this circuit to draw too much power.
Remove the trimmer from the circuit and measure the resistance between the wiper and the top end. In my test I found that it was about 350Ω. So, the total series resistance for the infrared LED was 100 + 350 = 450Ω, which is relatively high for an LED powered by 5VDC, but my circuit worked with this value.
§ You can now remove the 1K trimmer and the 100Ω resistor and substitute a series resistor of 450Ω, or whatever value gave you the 10mA current flow.
Now that the LED is taken care of, connect the collector of the phototransistor and measure the voltage between point C and negative ground. This voltage is supplied through the 1K resistor and the 2K trimmer, which together form the pullup resistor for the open collector.
While you are measuring it, insert a piece of card in the sensor to block the infrared light beam, and then remove the piece of card. By adjusting the trimmer, you should be able to achieve a high voltage that exceeds 4.5VDC when the card is removed, and a low voltage that is less than 0.5VDC when the card is inserted. I’m betting that a total resistance of about 2K will be appropriate.
§ You can now remove the 2K trimmer and the 1K resistor and substitute an appropriate pullup resistor that creates the desired voltage range at point C in the circuit.
Because a 74HC00 likes to have a high logic input greater than 3.5VDC and a low logic input less than 1VDC, the output from the sensor should be acceptable.
I’ve seen some transmissive optical sensors that recommend a pullup resistance on the phototransistor as low as 100Ω. If you happen to be using one of these, verify the current that is sinking into the sensor. The way to do that is to set your meter to measure milliamps, disconnect the emitter of the transistor from the circuit, and insert your meter between the emitter and negative ground. You shouldn’t need a current higher than 4mA.
Now connect point C with the top-left OR gate in the logic chip, as shown. The other input of this OR gate, at D, is tied to negative ground for the time being.
When you move a piece of card into the sensor, you should see the LED lighting up without any hesitation or flickering.
Because the input impedance of a 74HC00 series logic chip is so high, you should find that it doesn’t reduce the open-collector voltage significantly.
While you are testing the circuit, experiment with different objects blocking the infrared LED in the sensor. You’ll probably find that a thick piece of cardboard will provide a higher open-collector voltage than a piece of white paper. Bear this in mind if you use a sensor for a practical application.
You could take this experiment a step further by adding another optical sensor. Disconnect the grounding wire from the second input to the OR gate at point D, and connect this input to the output from the second sensor. Now you should get a high output from the OR gate when you block the infrared light beam in either of the sensors. Naturally you could use an OR gate that has more inputs if you want to check whether any of a set of sensors is triggered.
You could substitute an XOR gate for the OR gate. Now you’ll get a result when only one sensor is triggered. Logic gates enable you to customize your circuit depending on your needs.
Alternatively, a transmissive optical sensor is well suited to drive a comparator, in the same way as we used a phototransistor with a comparator in the Chronophotonic Lamp Switcher (see Chapter 7). The comparator will tolerate a wider range of voltages than a logic chip and will allow you to set a threshold voltage that gives you a large margin of error.
Note that when you were experimenting with a Hall-effect sensor, I suggested using a NAND gate to couple the outputs of multiple sensors. That was because the Hall output from the open collector goes low when the sensor is activated. The optical sensor behaves in the opposite way, because blocking the infrared beam causes the effective resistance of the phototransistor to increase, which makes the open-collector output appear to go high when the sensor is activated.
If you have a circuit where you want to invert the behavior of the optical sensor, you can change it to give you an emitter-follower output:
1. Disconnect the pullup resistor to the collector of the phototransistor. Disconnect the ground wire to the emitter of the phototransistor, and substitute the resistor between the emitter and ground.
2. Connect the collector of the phototransistor directly to the positive side of the power supply.
3. Move the connection to the OR chip from point C to the emitter of the phototransistor.
Now the output from the sensor will be normally high and will go low when the infrared light beam is blocked.
Quick Facts About Transmissive Optical Sensors
§ Typically the internal infrared LED requires a voltage no greater than 1.5V (preferably around 1.2V). It may consume 10mA to 20mA.
§ In an open-collector circuit, the phototransistor needs a pullup resistor between its collector and the positive side of the power supply. The value varies widely for different types of optical sensor. The collector voltage will be low when the sensor is unobstructed, and high when the sensor is obstructed.
§ An emitter-follower circuit inverts the behavior of the sensor. A resistor is placed between the emitter of the phototransistor and the negative ground side of the power supply. An emitter connection should be high when the sensor is unobstructed and low when the sensor is obstructed.
§ Transmissive optical sensors are not designed to drive significant loads. The output should go to a high-impedance component, such as a comparator or a logic gate.
§ Remember that the output of an infrared LED will diminish over time. Try to design your circuit so that it will still work with a reduced light intensity. A comparator will tolerate a wider range of input voltages than a logic gate.
§ Because infrared light is not visible, you won’t see anything while the component is active. Don’t leave the component switched on by mistake!
What are we going to use this thing for? Well, how about a coin detector in the Hot Slot game? You’ll need sixteen sensors, but they’re quite cheap, and by the time you read this, they may be even cheaper.
To do this, I need to address two issues: how to wire the sensors, and how to mount them in a box suitable for coin insertion. I’ll deal with the wiring first.
Proof of Concept
You’ll remember that in the original version of the game, a multiplexer provided power through one of its sixteen outputs (see Chapter 21). If you need to refresh your memory, check Figure 21-7 and Figure 21-8.
The optical sensor requires so little current, I think the multiplexer can supply both its infrared LED and its phototransistor. This would be a nice arrangement, because all the other sensors can be left in an “off” state, consuming no power and conserving their infrared LEDs.
Another reason I like this arrangement is that instead of having just one LED that lights up when someone wins, each sensor can drive its own individual “win-notification” LED (if I add some kind of amplifier for it). When someone hits the hot slot, the win-notification LED will come on right beside that slot.
The only thing I’m wondering about is the exact sequence of events. It’s a little tricky, so I’m going to show what happens to just one sensor.
In Figure 31-7, a sensor is doing nothing yet, because it is receiving no power through the multiplexer. Its infrared LED is dark. Its phototransistor is not powered, either.
Figure 31-7. Step 1 in the powering of a coin sensor. See text for details.
Power is connected to an amplifier for the win-notification LED. The amplifier is our old friend a ULN2003 Darlington, which will work by sinking current through the win-notification LED—but only when a signal from the coin sensor tells the Darlington to do this. There’s no signal yet, because the sensor has no power. Consequently, the win-notification LED is dark.
Now take a look at Figure 31-8. The multiplexer has just chosen this sensor to be the Hot Slot. It is powering the sensor’s infrared LED and its phototransistor. The infrared LED immediately lowers the effective internal resistance of the phototransistor, which sinks current through a pullup resistor. Because the phototransistor is sinking current, hardly any of it gets to the amplifier. Consequently, the signal input of the amplifier has a low voltage, and the win-notification LED stays dark.
Figure 31-8. Step 2 in the powering of a coin sensor. See text for details.
Finally, in Figure 31-9, someone inserts a coin in the sensor. The phototransistor cannot see the infrared light anymore, so its effective internal resistance rises. Now the voltage on the signal input to the amplifier rises, so the amplifier switches on the win-notification LED.
So—do you think this will work? I wasn’t entirely sure when I first drew the circuit. I wondered if there was just a chance that when the multiplexer applies power, the sensor would take a moment to respond, and during that moment, the phototransistor wouldn’t sink any current, so the power would sneak around to the amplifier, and the win-notification LED would light up, revealing that this is the Hot Slot.
Figure 31-9. Step 3 in the powering of a coin sensor. See text for details.
However, when I wired the circuit, I found that the sensor responds so quickly, any current to the amplifier is so brief, the win-notification LED does not make a visible response. So, yes, the circuit does work. But sometimes you have to try these things to be sure. At least, I have to.
That’s it for the proof of concept. Now it’s just a matter of getting the resistor values right and fitting everything onto a breadboard.
Actually, I can’t fit everything onto a breadboard. But I can do eight sensors, which is the most the Darlington array can handle anyway. The breadboard layout can then be duplicated on another breadboard to make a full sixteen slots for the Hot Slot game.
Figure 31-10 shows three sensors with a Darlington array. Because I don’t have unlimited space, and because each sensor is wired identically, I’m leaving it to you to add five more sensors to the ones shown in the figure, using exactly the same hookups.
Figure 31-10. Three of the sixteen sensors and one of the two Darlington arrays that would be needed to implement optical coin sensing for the Hot Slot game.
§ The inputs numbered 0, 1, and 2 on the right-hand side correspond with the outputs from the multiplexer in the original Hot Slot schematic. See Figure 21-7.
§ Each sensor has been rotated 180 degrees compared with the way I had them in the test schematic in Figure 31-5. I turned them around to minimize the wire crossovers when driving the Darlington array. Make sure you mount your sensors with the infrared LED on the right, not the left.
§ I have increased the series resistor for each infrared LED in the sensors to 680Ω, and each pullup resistor for the open-collector output is now 3.3K. This is because the circuit is going to be driven by a multiplexer using a 9VDC power supply. We’re not using 74HC00 logic chips, so 5VDC is not necessary—and might be inadequate, because the multiplexer imposes some internal resistance, which will drop the voltage a little.
§ The Darlington chip sinks current (does not source it) so each win-notification LED is connected to the positive side of the power supply and sinks current into the Darlington.
§ Although the power for each sensor comes from the multiplexer in Figure 21-7, the Darlington arrays are powered directly from the main power supply. This way, they won’t add to the load on the multiplexer.
§ Each Darlington pair has a lower input impedance than the OR chip that was used in the previous test. Consequently a Darlington will pull down the output voltage of an active sensor slightly. I tried various values for the pullup resistor on the sensor and found that 3.3K was the best compromise. You can adjust this upward if you don’t get correct on-off behavior from the win-notification LED.
§ I’m suggesting a series resistor of 330Ω for each win-notification LED. The ones that I used are designed for a forward voltage of 2VDC. If you use different LEDs, you may need to adjust the series resistor. Check the voltage across one of the LEDs when it is on, and check the current that it is drawing.
§ A Darlington pair can sink 100mA without any trouble, so you don’t need to worry about overloading it.
Figure 31-11 shows a breadboard layout that you can use for each of the sensors, so that it only occupies four rows, including the win-notification LED, which has to be right beside the sensor. The gray strips are the conductors inside the breadboard. The sensor pins are shown as black circles with white centers.
§ The diagonal line linking two of the sensor pins is a jumper that must be inserted in the breadboard before the sensor is inserted above it.
Figure 31-11. A possible breadboard layout for one of the sensors, occupying a minimal number of rows.
Because the components are packed tightly together, you have to be very careful about placing them. If you put a resistor or a jumper wire one space away from its proper location, you can reverse the voltage to a sensor and burn it out. In my own circuit, I burned out two sensors while testing it. Impatience, of course, is always the problem. Well—it’s my problem.
In Figure 31-12 you will find a photograph of my breadboarded circuit with eight sensors. This circuit would be duplicated to take care of the remaining eight sensors.
Figure 31-12. Breadboarded circuit featuring eight of the sixteen coin sensors for the Hot Slot game.
In the photograph, the white jumper wires coming in from the right are from the multiplexer outputs from the Hot Slot circuit that you can look up in Figure 21-7. Because I wanted to use bright LEDs in this circuit, they’re water clear and 5mm diameter. You can see them lined up on the left.
What do you think? Is it worth the extra trouble to create a neater user interface so that coins can be dropped effortlessly into place instead of being forced between some tight metal contacts? Personally I think so, and when you see my idea for a really elegant enclosure (coming right up), maybe you’ll be convinced, too.
The Slot Box
The most obvious way to build this circuit permanently would be to solder all the components onto perforated board and position the board underneath slots that you cut in the lid of a box. I’m not too excited by this idea, as it won’t control the coins properly.
I think a better plan is to sandwich the sensors between vertical layers of wood or opaque plastic that are glued together.
Figure 31-13 shows the first stage in this process, with four sensors and a quarter-inch-thick piece of wood or plastic that has semicircular coin receivers.
Figure 31-13. The first step in assembling an enclosure for the sensors.
Figure 31-14 shows how the sensors sit in notches at the bottoms of the semicircular slots.
Figure 31-14. The sensors rest in notches at the bottoms of the coin slots.
Now in Figure 31-15, a 3/4” spacer is added, with LEDs that have been mounted in holes drilled vertically from top to bottom.
Figure 31-15. A spacer will hold the sensors in place and prevent any leakage of infrared light from one sensor to another.
After adding four spacers and four coin-slot segments, all that remains is to add one more spacer on the front and then enclose the whole assembly in a box that will house the electronics in the bottom.
Figure 31-16. One more spacer will be needed at the front, after which the assembly can be enclosed in a box.
Cutting the pieces for this enclosure is not a very challenging task. The plans are shown in Figure 31-17. The colors have no special meaning and were added just to clarify the distinction between the sections.
Figure 31-17. How to cut the sections that can be assembled to enclose the sixteen sensors, coin slots, and win-notification LEDs.
The spacers are of wood or plastic, 3/4” thick. If you use wood, it should be a hardwood, because there is only 3/16” above each notch, and softwood will provide insufficient strength.
The coin receivers are made by cutting four 1” diameter circles in two pieces of wood or plastic measuring 6-1/2” x 2” x 1/4”. The thickness must be 1/4”, because it has to match the notch width in the sensors. You may have difficulty finding hardwood in this thickness, in which case a good-quality 1/4” plywood will do. I would use a Forstner bit to cut the circles, but a hole saw may work if you are careful. If you can use plastic such as ABS, that’s better.
After drilling the circles, cut each piece in half along its long dimension. The notches at the bottom of each semicircle can be made with a flat metal file with an abrasive edge, or a very small square metal file.
Don’t forget to drill vertical holes in the spacers to accommodate the LEDs before you start to assemble the pieces. You will need to extend the LED leads with pieces of 24-gauge wire so that they poke out at the bottom of each hole. If you use 5mm LEDs that don’t have a flange at the bottom, they will sit neatly in a hole that is 13/64” diameter. Add a smear of epoxy to hold each one in place.
Use a little epoxy, also, to stabilize each sensor in its notch. Don’t get any epoxy on the sensor leads, though. Also, be extremely careful to place all the sensors the same way around. You might draw a little picture to remind yourself which way around they are, because after you assemble the pieces of the enclosure, you won’t be able to tell.
The slots are sized so that quarters, pennies, nickels, or dimes will work to interrupt the infrared light in each sensor. Coins much larger than a quarter will not fit in a slot, because a quarter is just a fraction under 1” in diameter. If you live in a country where there are bigger coins, I leave it to you to make holes of the right size to accommodate them.
If you turn the assembly upside down, you’ll see that the leads from the sensors are still accessible, so you can build the whole slot-and-spacer assembly before you do any wiring.
The chips in the circuit can be mounted on a separate piece of perforated board, connected with the sensors with ribbon cable. You can then add four sides of thin plywood or plastic to create a box that holds the circuit board and the 16 slots.
This project is high on my “must build!” list, and I regret that I don’t have a photograph of a finished version. I’d like to start fabricating it right now, but my primary goal is to finish writing this book so that you can consider building the projects yourself.
In a way, it’s good that I don’t have my own fabricated enclosure to show you. This way, if you build your own, you won’t be influenced (for better or worse) by someone else’s work.