Home - Search - Members
Full Version: Interfacing LDR with 8051
Sep 26 2011, 6:51 AM
Hi guys,
My project is a medical reminder and dispenser system.
So I am planning to use LDR to act like a switch to send a high or low to a 8051 pin with only just my circuit.
My main idea was to use sbit to turn off a buzzer and LED when it is low and turn on a buzzer and LED when it is high.

I need help for the programming codes as I am bad at it.
Any help or tips would be greatly appreciated!

Sep 26 2011, 7:56 AM
sbit is used in c it means u want to do coding in c
// let say pins are as i suppose
sbit LDR = P1^0;
sbit LED = P1^1;
sbit BUZ = P1^2;

void main (void)
BUZ=LED=0;  // off the buzzer and led at start up
while(1)         // forever loop
if(LDR==1)   // if high
else           // it means low
Sep 26 2011, 3:32 PM
Oh ya, I wanna code in with C programming, forgotten to mention it!
Alright, thanks for the help! I will give it a shot and update you about it!
Ajay Bhargav
Oct 2 2011, 12:35 PM
We are waiting for update..
Oct 4 2011, 2:48 AM
Just got back from a school overseas industrial visit, sorry for not updating!
Apparently, I am having some trouble to allow the LDR to communicate with the 8051, as the LDR is analog while 8051 is digital.
So I am finding a way to convert the analog signal to digital as they cant communicate directly.
The LED has 2V when high, 1.5V when low.
While my LDR has 0.7V when high, 0.5V when low.
And I am figuring how to adjust the LED port to 3.3V at the moment.
Oct 4 2011, 5:23 AM
@ SkyTerror17
u can use op amp LM741 as a comparator
Oct 4 2011, 6:35 AM
Alright, am trying it now. But judging from my current schematic, I need to replace the LM741 with the 2N3904 transistor?
Oct 4 2011, 6:43 AM

There is also an exmple project here for LDR and 8051..

Oct 4 2011, 7:30 AM
Is there any single supply op amp that you guys could recommend?
Oct 4 2011, 7:32 AM
u can use LDR as
it is better way of using LDR
Oct 4 2011, 7:40 AM
Yeah, I saw that website, its really useful.
But I am required to use a single supply op amp and no relay.
Is there anyway to test a schematic using some software program or something?
Oct 4 2011, 10:18 AM
@ SkyTerror17
u can operate that circuit with 5 volt
12 volt is for relay
replace a relay and diode with 10k resistor and get logic on the collector of transistor
Oct 4 2011, 12:47 PM
Oh okay I will try it and update you!
Oct 5 2011, 1:39 AM
Just asking, what if my power supply is single supply? Does it still works?
Oct 5 2011, 4:42 AM
@ SkyTerror17
in normal cases single power supply is need connect (-) in schematic to ground and (+) to 5 volt nothing else
Oct 5 2011, 5:24 AM
Yah, I was alittle confused on that part. I did a schematic and place the components on the breadboard to test it out, still troubleshooting!

The pic below is a new schematic that I just made. I intend to have a 3.3V at the output when LDR is covered (dark) but still having some problems with the output voltage.

Oct 5 2011, 7:53 AM
@ SkyTerror17
what is the problem u just to tune some values nothing else it should work
Oct 5 2011, 3:56 PM

I intend to have a 3.3V at the output when LDR is covered.


Your setup will be low when the LDR is covered.
Either reverse Vr1 and the LDR or reverse the inputs to the op-amp.

Why drop the output to 3.3 volts ?
R3 and R4 are the wrong way round, and I would use lower values for each.

Oct 5 2011, 4:27 PM
Yeah, everything is almost done. I just have to tune the values of the resistor for R3 and R4 to reach the output voltage that I want!

Yeah, I swapped the LDR and VR1.
So does the R3 and R4, I even changed the values to 1k for R3 while 10k for R4.
Apologies as my group-mate got the concept wrongly!
We need to drop the output to 3.3V because our 8051 runs on 3.3V.
And we are gonna send a high or low input to it.
Oct 5 2011, 7:48 PM

So does the R3 and R4, I even changed the values to 1k for R3 while 10k for R4.


10k is too high ..
1k and 1.8k would be better.
Oct 6 2011, 6:56 AM
Okay, thanks for the tip!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Rickey's World © 2003 - 2007