Experiment 6: Easy On, Easy Off - Make: More Electronics (2014)

Make: More Electronics (2014)

Chapter 6. Experiment 6: Easy On, Easy Off

You saw in the last two experiments that a phototransistor changes its output gradually in proportion with the amount of light falling on it. This is a useful capability—although not as useful as it could be. For practical purposes, we often want a light-sensitive gadget that has two precisely defined states: “on” and “off.” An intrusion alarm, for instance, triggered by someone interrupting a light beam, has to give a clear signal. It cannot function gradually or intermittently.

Is there a way to convert a gradual output from a phototransistor into a clearly defined signal? Most definitely. A comparator is the tool for the job.

Making Comparisons

Put together the schematic in Figure 6-1. The 500K potentiometer is a trimmer that you plug into your breadboard. The phototransistor is in series with a 3.3K resistor as before, except that this time the output from the emitter connects through a 100K resistor to an input on an LM339 chip. This chip contains a comparator—in fact, it contains four comparators, although we will only use one of them right now. During this demo, the unused comparators can be left unconnected.

The initial stage of a circuit that uses a comparator to switch an LED in response to light falling on a phototransistor.

Figure 6-1. The initial stage of a circuit that uses a comparator to switch an LED in response to light falling on a phototransistor.

Set the potentiometer around the middle of its range. Begin with the phototransistor covered, so that no light falls on it. Now allow light to intrude, and you should see the LED light up. Dim the light again, and the LED goes off.

The 500K potentiometer sets a reference voltage for the comparator. When the wiper of the potentiometer is in the middle of its range, the reference voltage will be about 2.5V, because the potentiometer is functioning as a voltage divider between the positive and negative-ground sides of the power supply.

In dim light, the voltage from the emitter of the phototransistor is lower than 2.5V, so the comparator doesn’t respond. As the light brightens, the voltage on the emitter of the phototransistor rises above 2.5V (do you remember why?). The comparator detects the difference and changes its output. (The 100K resistor will be necessary when other components are added in the next step. Because the input impedance of the comparator is extremely high, the resistor hardly affects the input voltage sensed by the chip.)

Now maintain a constant moderate light on the phototransistor while you adjust the potentiometer. The LED goes off and on because you are varying the reference voltage that the comparator is using.

Quick Facts About Comparators

§ The comparator compares a variable voltage on one input with a fixed, reference voltage on the other input.

§ We can use a potentiometer to set the reference voltage.

So far, so good. But we have a problem if the light falling on the phototransistor changes very slightly around the point where the LED turns on or off. To see this, start with the phototransistor in shadow, and gradually increase the light until the LED comes on. Now decrease the light just a tiny fraction, and the LED should flicker.

This is shown graphically in Figure 6-2. The flickering is known as “hunting,” as the comparator is hunting to and fro, unable to make up its mind whether its output should be in the on state or the off state.

When a comparator receives a slowly changing input (upper graph), its output tends to oscillate unpredictably (lower graph) between “on” and “off.”

Figure 6-2. When a comparator receives a slowly changing input (upper graph), its output tends to oscillate unpredictably (lower graph) between “on” and “off.”

How can we prevent this? The answer is to use a very powerful technique known as positive feedback.

Feedback

Figure 6-3 shows the same circuit as before, but with an additional potentiometer on the right. Figure 6-4 shows a breadboarded version of the circuit.

The basic comparator circuit has been modified to clean up its output, by the addition of positive feedback. Previous wiring is gray.

Figure 6-3. The basic comparator circuit has been modified to clean up its output, by the addition of positive feedback. Previous wiring is gray.

The positive-feedback comparator schematic breadboarded, with two trimmer potentiometers, a phototransistor at bottom-left, and an LED to display the output.

Figure 6-4. The positive-feedback comparator schematic breadboarded, with two trimmer potentiometers, a phototransistor at bottom-left, and an LED to display the output.

The basic idea of this circuit is shown in Figure 6-5. Notice that pin 2 of the comparator chip is connected back to pin 5. Pin 2 is the output (remember, it controls the LED). Pin 5 is the variable input—the phototransistor is connected with it through the 100K resistor. So the second potentiometer in Figure 6-3 is taking some of the voltage from the output and feeding it back to the input. This is the positive feedback.

The basic concept of positive feedback.

Figure 6-5. The basic concept of positive feedback.

With both of the trimmers near the middle of their scales, if you make small adjustments to the light falling on the phototransistor (as you did before), you should find that the LED doesn’t flicker anymore. It is either “on” or “off.”

Positive feedback works like this:

§ When the output becomes more positive, it circulates back through the feedback loop, which adds it to the input.

§ The input voltage goes up, which boosts the output.

§ The higher output circulates back and reinforces the input some more.

This all happens very, very quickly, so that the LED lights up and stays lit. Now if the light falling on the LED gradually diminishes, at first nothing happens, because there’s still enough feedback to sustain the input. But as the light fades, this happens:

§ The lower input creates a lower output.

§ Feedback from the output doesn’t reinforce the input so much anymore.

§ Deprived of positive feedback, the input drops suddenly, and the comparator’s output goes low.

This all happens so quickly, the LED blinks off instead of flickering or gradually fading out.

Hysteresis

Turn the righthand trimmer to reduce its resistance in the circuit. This will increase the positive feedback so that it becomes more easily seen.

Now adjust the light falling on the phototransistor very, very gradually. If you have it under a desk lamp, hold your hand close to the lamp, so that you cast a shadow with a very soft, blurry edge.

You should find that when the LED lights up, you can reduce the light a little, and the LED will remain illuminated. You can think of the comparator becoming “sticky,” because it tends to stick in the “on” state.

When it finally goes out, slowly increase the light, and now the comparator will tend to stick in the “off” state. Figure 6-6 illustrates this.

With positive feedback, the output from a comparator tends to stick in its high or low state. The “sticky” zone is the hysteresis region.

Figure 6-6. With positive feedback, the output from a comparator tends to stick in its high or low state. The “sticky” zone is the hysteresis region.

This phenomenon is known as hysteresis, and it’s very useful. Suppose you are using a phototransistor to switch on a lamp at sunset. The light fluctuates a little as the clouds pass in front of the setting sun. Do you want the lamp to turn on and off with every little variation in the light? No, once the lamp comes on, you want it to stay on, regardless of small variations.

Suppose you have a thermostat that controls a heater. You want the heater to come on when the room temperature drops to around, say, 70 degrees Fahrenheit. Once the heater is on, you don’t want it to switch off just because someone walks in front of the thermostat and creates a brief current of slightly warmer air. You want the heater to ignore small fluctuations until the temperature reaches, say, 72 degrees. Then you want the heater to turn off and stay off until the temperature drops back down to 70. In this case, the hysteresis region extends between 70 and 72 degrees.

The amount of hysteresis can be adjusted by increasing or decreasing the amount of positive feedback to a comparator. With a lower resistance allowing more feedback, the comparator will ignore larger fluctuations in its input, to create a simplified output. Figure 6-7 illustrates this.

Increasing the positive feedback in a comparator circuit creates more hysteresis, causing the comparator to ignore substantial variations in an erratic signal.

Figure 6-7. Increasing the positive feedback in a comparator circuit creates more hysteresis, causing the comparator to ignore substantial variations in an erratic signal.

The lower half of the figure shows the output that we want from the comparator, ignoring all those little wiggles in the input. Basically, the comparator will ignore any changes in the gray area, and will only react when the signal rises through the gray area and emerges into the “must switch on” region, or falls through the gray area and emerges into the “must switch off” region.

You should know that hysteresis is normally shown using a graph that looks like the one in Figure 6-8. This is the graph that you tend to find in most electronics books, but it’s a little difficult to understand. The righthand part of the curve shows the output from the comparator (measured on the vertical axis) when the input voltage is smoothly and gradually increasing (measured on the horizontal axis, from left to right). The comparator waits a bit before it allows its output to come on. Then if the input voltage smoothly and gradually starts to go down, the curve to the left shows that the comparator waits a bit before it allows its output to go off.

The classic way to depict hysteresis. See text for details.

Figure 6-8. The classic way to depict hysteresis. See text for details.

The Symbol

Now for some details about the comparator. First, its symbol is shown in Figure 6-9. The comparator requires its own power supply, like a logic chip. I’ve shown this with the positive and negative signs, but in a schematic, often the power supply to a comparator is omitted. Everyone knows it has to be there, so people who draw schematics may not bother to include it.

Schematic symbol for a comparator. The power supply is always necessary, but is not always shown in a circuit.

Figure 6-9. Schematic symbol for a comparator. The power supply is always necessary, but is not always shown in a circuit.

The reference voltage in your circuit was actually being applied through the “inverting” input of the comparator. You used the “noninverting” input for the variable voltage from the phototransistor. I’ll explain why the inputs have these names a bit later. In the schematic symbol, the two inputs have their own plus and minus signs—which are confusing, because they do not mean that you are supposed to apply positive or negative voltage to them.

Quick Facts About Plus and Minus

§ The comparator switches on when the voltage on the “plus” input in a comparator changes to become more positive than the voltage on the “minus” input. The “plus” input is called the noninverting input.

§ Similarly, the comparator switches on when the voltage on the “minus” output in a comparator changes to become more negative than the voltage on the “plus” input. The “minus” input is called the inverting input.

The Output

I’ve been talking about the output from a comparator, but in fact, many comparators don’t create a simple high or low output. They have an open collector output. This is shown in Figure 6-10.

Simplified view of the interior workings of a comparator. The two positive voltages do not have to be the same, so long as they share the same negative ground.

Figure 6-10. Simplified view of the interior workings of a comparator. The two positive voltages do not have to be the same, so long as they share the same negative ground.

The comparator actually contains various components, but the one of interest to us here is an output transistor, which is often a bipolar type. When the transistor switches on, it conducts current, so it sucks the current from an external pullup resistor and dumps it into negative ground. It also sucks current from any components connected with the comparator. This makes it seem that the comparator has a low output.

When the transistor switches off, it blocks current. The current from the pullup resistor cannot sink through the comparator anymore, so it goes out to any components connected with the output. This makes it seem that the comparator has a high output.

Figure 6-11 provides a graphical view of the way that a comparator works.

When a comparator is used in noninverting mode (with voltage applied to its “plus,” or noninverting, terminal), its output behaves as shown here.

Figure 6-11. When a comparator is used in noninverting mode (with voltage applied to its “plus,” or noninverting, terminal), its output behaves as shown here.

In practice, you don’t need to remember what the output transistor inside the comparator is doing. You just have to remember that the “high” output from the comparator is really being supplied through the pullup resistor, while the “low” output means that current is sinking through the comparator.

In Figure 6-1 you may have wondered why I didn’t show the LED with a resistor in series with it, as is the usual practice. This is because the LM339 has an open-collector output, so really the LED is being powered through the 470Ω pullup resistor.

Time, now, to sum up what we know about output.

More Quick Facts About Comparators

§ You must include a pullup resistor on the output of a comparator if it has an open collector or open drain. Otherwise, the comparator won’t work. Check the datasheet for any comparator that you use.

§ If you use a low-value pullup resistor, more current will sink through the comparator, and you can burn it out. When in doubt, use your meter to check the current flowing from the pullup resistor into the comparator’s output pin.

§ Most comparators should have their outputs connected to devices with high input impedances, such as logic chips. That kind of chip demands very little current, so you can use a relatively high-value pullup resistor, which is typically 5K. The 470Ω resistor in the schematic in Figure 6-3 is relatively low because the comparator had to drive an LED.

§ The comparator can’t sink much more than 20mA. You can always add an external transistor to the output if you need more current.

Now, this next point is very important, and is a new concept:

§ The positive voltage feeding the pullup resistor does not have to be from the same source as the voltage powering the comparator, so long as they share the same negative ground. For instance, you could have a power supply for the comparator that is 5VDC relative to its negative ground, and a pullup resistor powered by 9VDC relative to the same negative ground. Consequently, a comparator can function as a voltage amplifier.

Very interesting—we now know how to amplify current, with a transistor, and voltage, with a comparator. This information can be useful in the future.

Just be careful that you don’t sink too much power through that internal transistor. The datasheet for a comparator will tell you what the limits are.

Inside the Chip

I used an LM339 in this experiment because it’s one of the oldest types of comparators, but still very widely used—and very cheap! In Figure 6-12 you’ll see that it is actually a quad comparator, meaning that it contains four comparators, only one of which we are using (so far) in this experiment.

Four comparators are built into the LM339 chip.

Figure 6-12. Four comparators are built into the LM339 chip.

The Circuit Redrawn

The schematic that I showed you in Figure 6-3 was laid out so that it would be as easy as possible for you to transfer to a breadboard. But other people don’t make it so easy for you to breadboard a circuit. They tend to put positive voltage at the top, negative ground at the bottom, input on the left, and output on the right. This convention is used because the circuit is easier to understand when you look at it for the first time.

Figure 6-13 is an example of a conventional schematic using a comparator. The components and connections are actually the same as in Figure 6-3.

A more typical layout of components in a basic positive-feedback loop using a comparator.

Figure 6-13. A more typical layout of components in a basic positive-feedback loop using a comparator.

And the essence of it is shown in Figure 6-14.

The basic concept of hysteresis in a positive-feedback circuit.

Figure 6-14. The basic concept of hysteresis in a positive-feedback circuit.

Warning: Inverted Comparators

In this book, I will always show comparators (and, later, op-amps) with the noninverting, “plus,” input below the inverting, “minus,” input. In schematics that you may see elsewhere, this is the most common configuration—but it is not universal. Sometimes the person who draws a schematic may feel that it’s more convenient to show the noninverting input above the inverting input, because this will allow fewer wiring crossovers, or will enable components to be placed closer to each other.

This can be confusing. You really have to be careful to note the positions of the “plus” and “minus” inputs. If you fail to see that a comparator has been drawn “upside down,” your circuit will do the opposite of what you expect.

Comparisons with a Microcontroller

Comparator chips are a bit old-school. People these days tend to reach for a microcontroller when they want to process a variable input and create an on-or-off output.

The hardware in many microcontrollers incorporates one or more analog-digital converters (often abbreviated ADC). Typically each ADC is assigned to a specific pin. It can accept a variable voltage and convert it into an integer—that is, a whole number—usually ranging from 0 to at least 1,000 (in decimal notation).

If you power a phototransistor with 5VDC as I suggested in the experiments here, its output should be compatible with the input of a 5V microcontroller. That sounds nice and simple: just link the phototransistor with the microcontroller. (Actually, you may wish to insert a resistor of 5K or 10K between them, to protect the input of the microcontroller. Because the input of the chip has such a high impedance, the resistor won’t drop the voltage at the input pin significantly.)

Your next step would be to expose your phototransistor to a light that is midway between “dim” and “bright,” to establish a transition point. Above it, you want the microcontroller to do something. Below it, you want the microcontroller to stop doing something.

While your transitional light is on, you need to find out what number the ADC in the microcontroller is generating. The easiest way to handle this is to hook up the microcontroller with some kind of digital display, and write a little program that will show the number on the display.

Now you can write a new program that contains a conditional statement, telling the microcontroller to start doing something if the light is above the dividing line, and stop doing it if the light is below the dividing line.

So far, this doesn’t sound like too much work—although if you change your mind about the transition point, you’ll have to rewrite your program and reinstall it in the microcontroller all over again. Clearly, this is more of a hassle than just tweaking a trimmer potentiometer.

But you want hysteresis, don’t you? In that case, you have to define two levels of light, to establish the upper and lower levels of the gray zone in which the microcontroller should ignore small variations. Basically the program will tell the chip, “If the light value rises above this upper level, start doing something; if the light value falls below this lower level, stop doing it; and if the light value is between the upper and lower levels, continue as before.”

The real problem, once again, occurs if you change your mind and want to make modifications. Suppose you use your microcontroller in a phototransistor-based device to switch an outside light on at sunset, and off at dawn. You need some hysteresis so that the light won’t flicker on and off in response to small random variations caused by differing amounts of cloud cover in twilight conditions. How can you figure this out on a workbench? Well, you can’t. You’ll have to assemble your hardware, move the device to the location where you want to use it, and see how it responds. To adjust the hysteresis, you’ll need to use a laptop to install a new version of the program, setting new upper and lower light values.

To me, this doesn’t sound like a lot of fun.

Microcontrollers are indispensable in many situations, but sometimes a simple analog circuit built around a chip that costs less than $1 will be a more practical option.

Make Even More: A Laser-Based Security System

You now have all the electronics knowledge you need to create a perimeter defense system using the circuit that you just built, with a cheap laser pen and some mirrors that will reflect the beam around the edges of the protected zone. Instead of using an LED attached to the output from the comparator, you’ll attach the output to the base of a 2N2222 transistor that will route power to a coil in a latching relay. The relay will then sound an alarm—or notify you of the intrusion in a more discreet way.

If you want a more elaborate system, you could use multiple lasers and phototransistors so that if one is triggered, you’ll know roughly where the intrusion occurred. Remember, there are four comparators inside the LM339 chip, and they can each function independently.

To make the system work well, you’ll need to build the phototransistor into an enclosed box with just a small hole where the laser beam can get in. This way, the phototransistor will be protected from ambient light, and the system may work during the day. Even so, you will still need to set the sensitivity of your phototransistor and the extent of the hysteresis. The only way to do this is by trial and error.

How many other applications can you think of, for a phototransistor? If you use your imagination, I’m sure you can come up with many ideas. My favorite is the chronophotonic lamp switcher, which you will find in the very next experiment.