- Published: Tuesday, 21 December 2010
- Written by Jon Chandler
- Hits: 15720
This tutorial will cover fundamentals and some basic applications. A few side trips are included to explain some necessary fundamentals.
The chart below shows the response of photocells in stock at Digikey. As you can see, the resistance changes cover a wide range. For use with PIC ADC or digital inputs, dark resistances below 1 M-Ohm are suggested.
The table below shows the light and dark resistance of these photocells. An important item to note is the range of resistance for each photocell when exposed to light at 10 LUX. Photocells are not precision sensors, and there may be a wide range of resistance values for even the same type of sensor exposed to the same light level. If a calibrated light level is the goal, there are better options than photocells.
||Dark Resistance (Ohms)||Light Resistance Range at 10 LUX (Ohms)
|| 2 M
|| 27 ~ 60 k
||300 k||4 ~ 11 k|
||500 k||11 ~ 20 k|
||5 M||10 ~ 50 k|
||1 M||10 ~ 100 k|
||500 k||9 ~ 20 k|
||20 M||5 ~ 20 k|
||20 ~ 45 k|
||23 ~ 33 k|
||2.5 M||50 ~ 94 k|
||16 ~ 33 k|
||200 k||3 ~ 11 k|
||150 k||4 ~ 11 k|
||500 k||16 ~ 33 k|
||300 k||8 ~ 16 k|
||20 M||48 ~ 140 k|
||5 M||80 ~ 200 k|
||20 M||80 ~ 240 k|
||5 M||80 ~ 200 k|
||1 M||40 ~ 120 k|
||300 k||9 ~ 20 k|
||2 M||27 ~ 60 k|
||500 k||12 ~ 30 k|
||500 k||4 ~ 20 k|
The Voltage Divider
A microcontroller input can't read resistance directly. The photocell's resistance must be converted into a form the microcontroller can use, namely voltage. Don't panic, this is pretty simple. Note: there are other methods to measure resistance, but this is by far the easiest method.
A voltage divider uses 2 resistors and provides an output proportional to the ratio of the resistors. The circuit is shown in the figure to the right. Two resistors are connected in series between "Vin" and ground. Vin will usually be connected to Vdd (the supply voltage). The voltage at "Vout" (which in this case will be connected to a microcontroller input) is proportional to the ratio of R1 and R2.
I can never remember the equation to solve for the ratio of Vin/Vout but the derivation is pretty simple. We'll make one assumption before we start. The input impedance of a microcontroller input is high, so the current flow from Vout is insignificant. The arrow labeled I is the current flow through the system, from Vin through the resistors to ground. In accordance with Ohm's Law,
The resistance in the above equation is the sum of R1 and R2, which simply add for series resistors, so this equation becomes:
The current through the circuit is the same everywhere. The same current flows through R1 and R2.
Using Ohm's Law again, the voltage across R2, which is Vout, is
Which we can re-arrange to
Combining equations (2) and (4):
Finally, rearranging (5) yields
See? That wasn't so hard!
Let's look at some examples:
This example shows that equal resistors will result in the output voltage being one-half the input voltage.
This example shows that if R1 is smaller than R2, the output is moved towards Vin.
Conversely, if R2 is smaller than R1, the output is moved towards ground.
Using Photocells With Analog inputsThe resistance of a photocell is inversely proportional to the light striking the cell. A voltage divider converts this resistance to a voltage that the PIC's ADC input can measure. If the resistance of one of the elements in a voltage divider changes, the output level changes. One possible arrangement is shown here. The photocell is in the upper part of the voltage divider, corresponding to R1 in Figure 1. When exposed to a bright light, the resistance of the photocell, and hence R1 in our equation, decreases. Taken to the extreme when R1 = 0, the equation becomes Vout = Vin (R2/R2), or Vout = Vin. When it gets dark, R1 gets large. Let's say R2 equals 1/10 the maximum photocell resistance. In the dark, the equation becomes Vout = Vin (1/11). The voltage the ADC is reduced to about a tenth of Vin. The important thing to note about the configuration in Figure 1 is that the voltage is proportional to light level: bright light = high voltage.
The photocell could also be installed in place of R2, as shown in Figure 2. Remember that the photocell's resistance decreases when the light level increases, so R2 in the eqn (5) will decrease. What happens when R2 decreases? Look at example 3 above. Vout will decrease.
You might think of the arrangement in Figure 2 as a darkness measurement. The darker it is, the higher the voltage.
A photocell the configurations shown in Figure 1 or 2 can be used to detect light or the absence of light. A circuit like this might be used to control an LED backlight or turn on a light in the dark. Photocells are great to detect relative light levels, but if absolute light levels are to be measured, the results must be calibrated because the tolerance of photocells isn't that good.
Another application is to focus a flashlight beam or even a laser on the photocell. When the beam is blocked, the light level will fall. This is a good way to detect people passing by or maybe products going by on a conveyor belt.
What happens if we put similar photocells in each position of the voltage divider and expose both to the same light?
Referring back to eqn (6), the cells will be close in resistance value, so R1≈R2, and Vout will be about 1/2 Vin.
What happens in the dark? R1≈R2. In the bright light? R1≈R2. Vout will be about half of Vin if the cells are exposed to the same light level no matter what it is. What good is this?
If matched photocells are used in both positions of the voltage divider, we can create a two-state sensor using a flashlight or laser pointer. Shine the light on the R1 sensor, and Vout will increase above 50%. Shine the light on the R2 sensor and Vout will be less than 50%. It doesn't matter if the room is light or dark as long as the flashlight is brighter than the light level. If we read the ADC level while the light is shining on one of the sensors, we can determine which one is illuminated.
This is pretty slick, but don't build this yet. We can do far better!
Using Photocells With Digital Inputs
Photocells are analog sensors, so you might not think of using them with a digital input but there are some great applications using them in this way.
We have to make anther side trip to learn a little bit about digital inputs. When we think of digital, we think of ones and zeros, high levels and low levels. With a traditional TTL input, there are well defined limits on levels. A low-level signal doesn't have to be zero - a few hundred mV is within the limits of what constitutes a "low," and a high-level signal doesn't have to be 5 volts (Vdd), a "high" can be as little as 2 volts.
Figure 5 to the right shows the range of TTL levels for a PIC18F2520 operating greater than 4.5 volts. For a signal to be read as a low-level signal, it must be 0.8 volts or less. For a signal to be read as a high, it must be above 2 volts.
The region between 0,8 volts and 2 volts is kind of a no-man's-land. The signal is at an undefined level and bad things can happen. Certainly, if your program depends on knowing this level, anything could happen! But even more importantly, damage can happen inside the PIC microcontroller because of the nature of TTL electronics. In this undefined range, high currents can flow within the chip.
This type of input may not be the best place to connect our photocell circuit unless we can be sure that the photocell's output voltage won't stay in this range.
TTL is what we normally think of when we think of digital inputs - at least I know that's been how I've considered things. But PIC microcontrollers have a few tricks for us. If you'd looked at a data sheet for a microcontroller, you may have noticed that some port pins are labeled at TTL inputs and some are labeled as Schmitt Trigger inputs. What difference does it make?
A Schmitt Trigger will make no difference at all if the signals being monitored are solidly high or low. For signals with noise or slowly changing signals, a Schmitt Trigger is invaluable. TTL input levels are like a line in the sand. If the input level is on one side of the line, it's one thing and if it's on the other, it's a different thing. Suppose you have a noisey high level signal that's jittering around 2 volts. At one instance in time, it may be slightly greater than 2 volts and a valid level. In the next instance, it might be just a shade under 2 volts and be an undefined level. Your program may interpret what should be a continuous high level as a random pulse stream. A Schmitt Trigger has hysteresis. Rather than changing state at an exact value, the signal level must be 4 volts or greater (assuming Vdd = 5v) to be high. But if that signal drops in level, it won't be considered low if it's slightly less than 4 volts. In fact, it must fall below 1 volt to change state. A Schmitt Trigger input doesn't have that no-man's-land in the middle. The previous state remains set until the trip point for the other state is reached. It won't have the jitter problem desribed above for the TTL input with a noisey signal.
(A Schmitt Trigger input will cause problems with an output from a 3.3 volt part. See end of article.)
The difference in response is shown in the figures below. The blue line might represent the output of the photocell circuit over time as clouds pass by. The green bars represent the high state or low state the input would measure. In Figure 7, the TTL input response also has red bars. These represent the undefined level when the input is between high and low trip points. The level reported by the input will be indeterminate.
Compare this to Figure 8, the Schmitt Trigger input response. The state does not change until the opposite level is reached - there is no undefined region between the trip points. The state simply stays what it was if it's in the range between the set points.
Compare these two plots carefully. The Schmitt Trigger input won't have any problem with our slowly changing photocell signal. There's no undefined region to cause problems. But also note the green bars representing high and low conditions. What the input sees is interpreted quite differently! For a noisy or slowing changing signal, the difference between a TTL input and a Schmitt Trigger input may be extreme.
To determine which pins on a given PIC are TTL inputs and which are Schmitt Trigger, consult the data sheet. A quick glance at one data sheet says that Port B is TTL for general-purpose inputs, and Port C is Schmitt Trigger.
Back to Photocells...
Let's consider what happens if we connect the photocell arrangement of Figure 4 to a Schitt Trigger digital input.
If the flashlight is off, and both solar cells are exposed to about the same ambient light level, the state of the input won't change. It's whatever it was prior to "now." If we shine the flashlight on the R1 sensor, (and the ambient light level isn't too high), the input will see a voltage level of > 4 volts, and the input will be set to high, When the flashlight is shut off, the resistance of the two photocells will be approximately the same so the voltage seen by the input will be about 2.5 volts, and the input will remain set.
If the flashlight is directed at the R2 sensor, the resistance of R2 will be much less than that of R1, and the Schmitt Trigger input will be toggled low. It will remain low until the flashlight strikes the R1 sensor again.
The flash light just needs to illuminate sensor R1 to make the input high, and the sensor R2 to make the input low. This circuit is a bi-stable switch that maintains its state until we change it. We can look at the state of the input when we get around to it - it's not necessary to check the input at the time the light strikes it to observe the change.
Compare the bi-stable function of this circuit to the function of the same circuit connected to an analog input. To observe the illumination of the photocell by the flashlight using an analog input, we must continuously read values to capture the change in value.
Wow, just changing the input used makes a huge difference in operation and the code to read the sensor.
I cound a couple interesting circuits in Fairchild Semiconductor Application Note 140, June 1975, CMOS Schmitt Trigger – A Uniquely Versatile Design Component. It's a great referene if you'd like to learn more about Schmitt Triggers and circuit ideas.
Basic Light Detector Circuit for Schmitt Trigger Input
This is the basic light detector circuit from the application note. When the light level is high enough that the output is greater than 4 volts, the Schmitt Trigger will see a high input. When the light level is significantly lower and the input drops to 1 volt or less, the output will toggle to low. Adjusting the pot changes the sensitivty. Note that the Schmitt Trigger shown in the illustration is built into the PIC.
OR Light Detector Circuit for Schmitt Trigger Input
Figure 10 shows a photodetector with 2 photocells arranged in an OR configuration. We the light level is high enough at either photocell, the inut level will be high. It will remain high until the light levels on both photocells are significantly reduced. The pot adjusts sensitivity.
AND Light Detector Circuit for Schmitt Trigger Input
Figure 11 shows an ADD detector using two photocells. Both photocells must be illuminated to result in a high input level. When either photocell is significantly dark, the output will be low. The pot adjusts the sensitivity.
Looking at the Schmitt Trigger input characteristics, it's clear that driving a 5 volt input with a low-voltage part such as a 18F25K20 must be carefully considered. If a Schmitt Trigger input is used on a 5 volt part, a high level must be at least 4 volts. This won't happen from a 3.3 volt part.
A TTL input may be used for interfacing with a low voltage part, provided (in the case of an 18F K-series part) that the operating voltage is great than 2.7 volts, as the minimum high level (for the 18F K-series) is Vdd - 0.7 volts.
The illustration below will help clarify the situation.