# LDR to MCU

Discussion in 'The Projects Forum' started by shico90, Aug 30, 2010.

1. ### shico90 Thread Starter New Member

Jul 22, 2010
7
0
Hello,
I have a question, I'm building a robot and I want to detect if there is a light or not, only two states, I want the LDR to give me zero voltage if it is dark or 5 volt if it is light, I want that as a input to the MCU
Can i connect it directly to the MCU without the need of ADC?

2. ### debjit625 Well-Known Member

Apr 17, 2010
790
186
LDR is not a digital device ,its an analog device so the output is variable rather logical ,you need some sort of circuit to make the analog signal to a digital logic and it totaly depends on you as you will decide the logical values of light intensity.In my option if you use ADC you dont need any external circuit ,just you will be measuring the resistance of the LDR.

Good Luck

shico90 likes this.
3. ### hobby16 Active Member

Aug 30, 2010
30
4
LDR is about > 100k in the dark and <10k with light.
So if you make a divider with a 47k and feed an I/O pin, you can discriminate darkness from light. Use a trigger schmidt digital input if your MCU has one (on Microchip pic, it's on PortA), so you'll have better noise rejection.
Otherwise, you can use a RC setup to do analog measument. All you need is to add a 100nF capacitor connected to 0V. Measuring the time the LDR charges C gives you the light level :

0 count = 0
1 set pin Out = Vcc during 1s => the LDR fully charges C
2 set pin Out =0V during 1 ms => the LDR discharges C
3 inc count
4 set pin In
5 if pin = 1 repeat from 2

shico90 likes this.
4. ### tom66 Senior Member

May 9, 2009
2,613
214
hobby16, in my experience, LDRs have a much wider swing; somewhere near 10 megohms in darkness and as few as 500 ohms in daylight. (I just tested it, with it against my 8W CFL it read only 150 ohms and covered up it read 20 megohms. So that's spanning over about 5 orders of magnitude.)

To the OP (original poster): A simple circuit to do what you want would be to wire up a resistor and LDR in a voltage divider to get the sensitivity you want, then connecting the center point between to the base on a small signal NPN (like the 2N3904 or 2N2222), then connecting the collector to your MCU's supply voltage and the emitter to the digital input. This will, however, have a 'dead band' where it is neither on nor off and this could register as either state, so be careful to use this only with definite ON or OFF states.

shico90 likes this.
5. ### windoze killa AAC Fanatic!

Feb 23, 2006
605
24
You could also feed that voltage divider centre point to a comparator thus avoiding the "dead zone".

shico90 likes this.
6. ### tom66 Senior Member

May 9, 2009
2,613
214
Some microcontrollers even have build in comparators. (Comparators still have a dead zone but it is *much* smaller compared to the NPN transistor circuit I posted.) Some even have internal voltage references, so you can avoid extra components for that.

shico90 likes this.
7. ### shico90 Thread Starter New Member

Jul 22, 2010
7
0
Thank you all for yout help, That would make me use ADC to avoid any problems.