Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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
 
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!
 
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.

3v0
 
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.
 
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.
 
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.
 
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 :D

The capacitor charging method can easily provide far higher resolution than normal A2D converters.
 
What kind of cap do you use to keep the repeatability high?
 
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.
 
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
 
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.
 
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:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top