interfacing light dependent resistances(LDRs)

hello evryone, this is my first post and i am currently working on my first arduino project , aline following robot. i am facing problems on how to take the input from the LDRs , i mean in what form will the input be. Can you also tell me how to use the input along with the program for the L293d motor driver.
Please do reply>>>>>>>>

An LDR needs to be part of a voltage divider, with a fixed resistor.

+5V ---> LDR -+-> resistor ---> Gnd
|
v
analog pin

The value read on the analog pin will be between 0 and some upper limit that is defined by the size of the resistor. The resistor should be approximately the same resistance as the LDR when it is in the dark. Then, the upper limit on the pin will be about 512. A smaller resistor will result in a higher upper limit. A larger resistor will result in a lower upper limit.

Can you also tell me how to use the input along with the program for the L293d motor driver.

What program would that be?

int pwmPin = 9; //Motor's PWM pin
int dPin_1 = 6;// Digital Pin to turn the motor on/off
int dPin_2 = 7;
int ledPin = 13; //LED pin

void setup()
{
pinMode(dPin_1, OUTPUT);
pinMode(dPin_2, OUTPUT);
pinMode(ledPin, OUTPUT);
}

void loop()
{
digitalWrite(ledPin, HIGH); // turn on LED
digitalWrite(dPin_1, HIGH); //turn on the motor
digitalWrite(dPin_2, LOW);

//Apply PWM to the motor
for(int i=0; i<=255; i++)
{
analogWrite(pwmPin, i);
delay(100);
}

delay(1000); //delay for a second

//switch directions
digitalWrite(dPin_1, LOW);
digitalWrite(dPin_2, HIGH);

for(int i=255; i>=0; i--)
{
analogWrite(pwmPin, i);
delay(100);
}

digitalWrite(ledPin, LOW); // turn off LED
digitalWrite(dPin_1, LOW); //turn off motor

delay(2000); //delay for 2 seconds.
}

First - use the "Code" button in the comment posting editor; go back to your post (via the modify link), select the code you posted, then click the code button in the editor (#), and save it. Thank you.

Now; regarding the code - the code doesn't show anything for the LDRs, plus I am wondering why you are using LDRs to begin with on a line following robot (are you sure you want to use a photo-resistors rather than a photo-transistor? For a line following robot, two matched pairs of photo-transistor/IR-LED would be best, assuming the line will be black marker on white butcher paper or similar).

[rant]
When did this who LDR (light detecting resistor) thing start, anyhow? Whatever happened to the word "photo-resistor"? Why, back in my day, we called them CdS cells, and everybody knew what we were talking about...grumble.
[/rant]

:slight_smile:

Anyhow - the trouble with using an LDR (well, pair of LDRs) for line following might be ambient light messing readings up, as well as stray reflections, shadows, etc. Using two photo-transistor/IR-LED pairs (especially if you modulate the LED output) will keep this issue at bay, and make it easier to develop/debug the code.

As far as the code is concerned, you are going to have either two LDRs, or two photo-transistor/IR-LED pairs (on either side of the line) ; for LDRs, you would hook them up in the arrangement given by PaulS to the analog pin; if you sense darkness on one side or the other, turn the wheels off or slow them down on that side (assuming a differential drive robot here; for Ackerman steering robots, you would steer toward the sensor).

If you use the photo-transistor/IR-LED pairs, the same coding logic applies, only the output of the photo-transistor is fed into a digital input pin, instead of an analog input.

Hope this helps...

:slight_smile:

When did this who LDR (light detecting resistor) thing start, anyhow?

Well it's Light Dependent Resistor and it's what you get taught at school now. Yeah I don't really care myself, it is a resistor that varies with light. I normally think of LDRs as dealing with visible light (mainly) and photo-detectors as non-visible light. I know that is not strictly true though.

Mowcius

I have always known them as LDRs since I first used them in the mid 60's. I had a Phillipp's electronics construction set with one in and thats what the manual called them. They are also called CdS sensors after the Cadmium Sulphide they use to make them.

The main difference is that the in an LDR the effect is due to the photo electric effect which is a bulk effect in material.
Where as the photo transistor or photo diode use a quantum photo electric effect in a crystalline structure. Due to the "energy gap" in the electron shells between the conduction band and the valance band in silicon it is sensitive to lower photon energies (IR).
LDRs are much slower to respond, much lower than 100Hz, this means they are no use in things like remote controllers.

Ahh, thanks for the interesting information as usual.

Mowcius