|How to use a LDR (Light dependent resistor), photo transistor|
Light dependent resistors - LDR
LDR's are the resistors whose resistance varies with the intensity of light incident upon it. The resistance is typically very high when no light in incident and it begins to reduce as light is incident upon it. LDR or a photo sensor finds its application in many robotics/embedded system applications such as line following robot, Light seeking robot, garage door opener when cars light is incident upon it, solar tracker etc.
What questions this document addresses?
The resistance R1 in the above circuit can vary. It should not be a very high value (~ 1Kohm) as then the current flowing through the diode would be very less and hence the intensity of emitted light would be lesser. By increasing the current flowing in the circuit, you can increase the effective distance of your LDR sensor. However, there are drawbacks of reducing the resistance. Firstly, it would increase the current consumption of your circuit and hence drain the battery (one of the few ‘precious’ resources for any embedded system) faster. Secondly, increasing the current might destroy the Tx. So, the final choice should be a calculated trade off between these various factors.
You can also modulate the Tx to achieve better distance and immunity.The receiver has a very high resistance, typically of the order of mega Ohms when light is not incident upon it. However, when visible light is incident upon it, the resistance decreases sharply to the order of a few kilo Ohms or even lesser. This feature forms the basis of using LDR as a sensor. You will need to connect a resistance of the order of a few mega Ohm in series with the Rx. Then tap the output voltage at the point of connectivity of these two resistors. A complete Tx-Rx circuit is given below.
Remember, the value of R2 can vary depending upon the Rx diode you are working with. You are advised to first check the resistance of Rx diode with no light being incident upon it and then select the value of R2 for decent performance.
Case1: when no light is incident upon the Rx
Rx would be of the order of mega ohms and hence the output voltage would be around 2.6v – 3v depending upon your choice of R2 and the Rx.
Case2: when light is incident upon the Rx
The resistance of Rx will sharply fall and hence the output voltage would be around 1.8v - 1.5v depending upon your choice of Rx and R2.
Once you obtain a neat difference between the output voltages in case1 and case2, your sensor is ready.
How to use this LDR sensor?
So far, we had just prepared the sensor. Now, we will see 2 different methods of using this in your machine.
The output voltage is in the form of analog voltage. You would need to convert it into digital format so that whenever light is incident upon the Rx, the final conditioned output voltage is a logic high (binary 1) and whenever light is not incident upon the Rx, the conditioned output voltage should be a logic low (binary 0).
Pin diagram for LM324 comparator.
It has 4 separate channels meaning it can compare 4 pairs of voltages. For a single LDR sensor, only one channel is enough. Here we would be using pin 1,2 and 3 for our sensor.
Input voltage at pin2 > input voltage at pin3 ; Output1=> logic 0
Input voltage at pin2 logic 1
Connect the output of our sensor circuit to pin2 of this IC. Generate 2v from a potential divider circuit of multiple resistance and feed that 2v to pin3 of the IC. Therefore, Vin at pin3 = 2v (constant).
Case1: when light is not incident upon the Rx.
When the Tx is above a black line, the black line will absorb all the incident light and will not reflect an appreciable amount of light for the Rx to receive. If you are making an obstacle avoiding robot, then when there is no obstacle in front of the Tx, Rx will not receive back the transmitted light. However, when an obstacle comes in front of the Tx, it will reflect the light incident upon it and hence Rx will receive the the light.
Case2: when IR is incident upon the Rx, the output voltage of the sensor = 1.8v. Hence the input voltage at pin2 =1.8v.
Using ADC to convert the analog output voltage from sensor into a digital format. This is a little tedious way of implementing the similar logic but can give you great granular control over the distance/range of your LDR sensor. You can use the built in ADC channels of a microcontroller also.
The program section in the tutorial of robosense covers the program required to use the adc channels of a microcontroller as well as program for implementing a simple line follower or obstacle avoiding robot.
One might be tempted to infer that LDR are good substitues for Infrared sensors and vice versa due to very similar technique of implementation for both of them. However, there are some inherent hurdles to be overcomed if you try to substitute either of these sensors. The challenges faced would be specific to a particular case and would largely be environment biased. For example, if you are using LDR/photo sensors in a line following robot, please ensure that you cover the entire array of the sensors within a case so as to minimize the interference due to ambient light. Calibrating your circuit time and again for varying ambient lighting would not be a preferable option. Hence, we would strongly recommend to make an ingenuous case like the one shown below:
LDR based line following robot.
Taking a closer look at the sensor array
Other minor issues can be to get a decent variation in resistance when light is incident upon the LDR and when it is not. Be careful about not wasting too much of battery by using a bright Tx which consumes high power.
Troubleshooting LDR circuits is not very complex.
Step1: First verify if the Tx led is working fine. Since you would be using visible light here, a quick look with naked eyes should serve the purpose.
Step2: Once you have verified that the TX are working fine, check if the output at the receiver side is showing correct expected voltages when light is incident upon it and when it is absent.
Step3: if the Tx and Rx are behaving correctly, please check the LM324 comparator and check if it is giving correct outputs in different scenarios.