# Using LDR in a PIC microcontroller Circuit

Status
Not open for further replies.

#### mikespike2004

##### New Member
Hey all
I'm doing a PV sun tracking system project. Briefly i will use 2 LDRs setted up somehow where the value of the resistances of both LDRs are fed into a PIC microcontroller and compared. the question is when i connect the LDR circuit to the PIC, the PIC will have the voltage values not the resistance values right? if yes how can i make it take the resistance values as an input.
Thanks

#### MikeMl

##### Well-Known Member
Do it the same way your Ohmmeter does it: force a constant current through the unknown resistance, and measure the resulting voltage. R = E/I, where I is a constant, like 0.001, where division is easy!

#### 3v0

##### Coop Build Coordinator
Forum Supporter
Use the LDR as half of a voltage divider. One leg of the LDR goes to +5 the other to a resistor. The free leg of the resistor goes to ground. The analog input of the uC is connected to the junction of the LDR and the resistor.

If this does not make any sense read up on voltage dividers. There should be many examples of using LDRs on the web.
voltage dividers
3v0

##### Banned
Use the LDR with a fixed resistor as a voltage divider. I don't think CDS photo cells are linear though so choosing the right fixed resistor in relation to the max/min resistance of your photocell and the range you're interested in might be slightly tricky. Test your photocell on a multimeter set to measure resistance and determine the resistance value's of the light range you're looking for.

#### Nigel Goodwin

##### Super Moderator
These ideas are all very well, but require a PIC with analogue inputs - and it's trivial to measure the resistance 'directly' rather than the voltage.

Simply use the LDR to charge a capacitor, and measure how long it takes - this is how PC joystick ports work - and there's a suitable PIC example in my tutorials.

##### Banned
LDR's tend to have an extremely wide resistance range I'm not sure if the cap charge method for measuring them would work well with the resolution available on a PIC. Someone would have to run the numbers and figure out what the actual time constants would be.

#### Nigel Goodwin

##### Super Moderator
LDR's tend to have an extremely wide resistance range I'm not sure if the cap charge method for measuring them would work well with the resolution available on a PIC. Someone would have to run the numbers and figure out what the actual time constants would be.
It's the normal way it's been done for decades, long before PIC's ever appeared

The capacitor charging method can easily provide far higher resolution than normal A2D converters.

##### Banned
What kind of cap do you use to keep the repeatability high?

#### Nigel Goodwin

##### Super Moderator
What kind of cap do you use to keep the repeatability high?
It depends on the resistor values you're measuring, but for high accuracy you need good stable capacitors - however, for an LDR, it's unlikely any great accuracy or resolution is required.

Just checked my tutorial, it used 0.1uF capacitors.

#### 3v0

##### Coop Build Coordinator
Forum Supporter
The few times I used CDR's I characterized them to choose the resistor. Regardless of which method you use, you will want to tune each of the CDR circuits for proper operation.

Personaly I would go with Nigels method because the range does not depend on an ADC converter. The sun and clouds move slow enough that you have plenty of time to wait while the capacitor charges.

My concern is that a setup tuned to work on a sunny day might now work on a cloudy day.

I have not tried either of the below ideas.

Use selectable resistors in the voltage divider circuit. Tie several resistors to the LDR and hook the other end of each to its own IO pin. Make one of these pins an output and tristate the others.

The resistor in all 3 methods could be replaced by a digital pot.

3v0

#### mikespike2004

##### New Member
Thanks you very much for your replies. i have connected a circuit and programmed the PIC in order to make a motor or a LED operates when there is no light just to have the sense of the LDR. I used a variable resistor to control the sensetivity of the LDR and it worked gr8. Now i have connected a similar LDR to the pic. and my question is i want to use analogue values rather than digital just like Nigel said. But instead of this capacitor thingy, cant i just program the PIC to take the values as analogue values and then compare the voltage values directly? I'm not sure if there are any PIC microcontrollers that can take analogue values as input but if there are can anybody tell me.
thanks again.

#### Nigel Goodwin

##### Super Moderator
There are many PIC's that have digital to analogue converters built-in, if you want to know how to use them then read my tutorials.

#### nirmi

##### New Member
can i use pic16f84a to detect resistance(out put of a LDR)?
i mean,can i jst input the resistance?
pls help me.
i need to display in a 7segment.
eg:-
if the resistance is 1 ---> display 25
if the resistance is 2-----> display 26
etc.

Last edited:

#### blueroomelectronics

##### Well-Known Member
Yes but better to
2. use a PIC with an A/D such as the 16F88

Status
Not open for further replies.