LDR Problem

Thecode.pde (3854Bytes)

 

I have a problem I am building a Photovore using an Arduino and I cant get the same readings from the 2 LDRs which are affected by the same amount of light. And I need a solution and some example code would be helpfull I tried mapping the values down to 1 to 100 but it doesnt work good Oh yeah one of the ldrs is more sensitive to light i think is this possible.

photo resistance and dark resistance may differ

Yes. It is possible that two LDRs have different sensitivity. Here is a chart where you see the photo resistance and dark resistance.

code

int motorA = 3; //Direction Pin for Left Motor

int motorB = 4; //Direction Pin for Left Motor

int motorSpeedA = 6; //Speed Pin for Left Motor

int motorSpeedB = 5; //Speed Pin for Right Motor

int motorC = 7; //Direction Pin for Right Motor

int motorD = 8; //Direction Pin for Right Motor

// Variable definitions

 

 int SensorRight; // This pin is used to read the value of the Right Sensor.

 int SensorLeft; // This stores the value of the Right Sensor pin to use later on in the sketch

int SensorDifference; // This value is used to determine the difference between the Left and Right

 

// the setup() method runs once when the program is run. When the

// Arduino is reset, the setup() will be executed once again.

 

void setup() 

 

{

pinMode(motorA, OUTPUT);

pinMode(motorB, OUTPUT);

pinMode(motorC, OUTPUT);

pinMode(motorD, OUTPUT);

pinMode(motorSpeedA, OUTPUT);

pinMode(motorSpeedB, OUTPUT);

pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.

pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.

digitalWrite(motorSpeedA,HIGH);

digitalWrite(motorSpeedB,HIGH);

digitalWrite(A1, HIGH); // Enables an internal pullup resistor

digitalWrite(A2, HIGH); // Enables an internal pullup resistor

Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600

Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()

}

 

// the loop() method runs over and over again,

// as long as the Arduino has power

 

void loop() 

 

  {

SensorLeft = 960 -  analogRead(LeftSensor);

delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.

SensorRight = 1020 -  analogRead(RightSensor);

delay(1);

SensorDifference = abs(SensorLeft - SensorRight); // This calculates the difference between the two sensors and then saves it to an integer.

 

// This section of the sketch is used to print the values of the

// sensors through Serial to the computer. Useful for determining

// if the sensors are working and if the code is also functioning properly.

 

Serial.print(“Left Sensor = “); // Prints the text inside the quotes.

Serial.print(SensorLeft); // Prints the value of the Left Sensor.

Serial.print(”\t”); // Prints a tab (space).

Serial.print(“Right Sensor = “); // Prints the text inside the quotes.

Serial.print(SensorRight); // Prints the value of the Right Sensor.

Serial.print(”\t”); // Prints a tab (space).

 

 

// This section of the sketch is what actually interperets the data and then runs the motors accordingly.

 

if (SensorLeft > SensorRight && SensorDifference > 85) 

  LeftSpin();

}

 

if (SensorLeft < SensorRight && SensorDifference > 85) 

{

 RightSpin(); 

}

 

else if (SensorDifference < 150) {rints Forward when the robot would actually go forward.

}

  Forward();

}

 

 

void Forward()

 

{

 

  digitalWrite(motorC,LOW);  

 

  digitalWrite(motorD,HIGH);

 

  digitalWrite(motorA,LOW);

 

  digitalWrite(motorB,HIGH); 

 

}

 

 

 

void Reverse() 

 

{

 

  digitalWrite(motorC,HIGH);  

 

  digitalWrite(motorD,LOW);

 

  digitalWrite(motorA,HIGH);

 

  digitalWrite(motorB,LOW);  

 

}

 

 

 

void LeftSpin() 

 

{

 

  digitalWrite(motorC,HIGH);  

 

  digitalWrite(motorD,LOW);

 

  digitalWrite(motorA,LOW);

 

  digitalWrite(motorB,HIGH); 

 

}

 

 

 

void Rightspin() 

 

{

 

  digitalWrite(motorC,LOW);  

 

  digitalWrite(motorD,HIGH);

 

  digitalWrite(motorA,HIGH);

 

  digitalWrite(motorB,LOW); 

 

}

 

 

 

void Stop() 

 

{

 

 digitalWrite(motorC,LOW);  

 

 digitalWrite(motorD,LOW);

 

 digitalWrite(motorA,LOW);

 

 digitalWrite(motorB,LOW);

 

 delay(10);

 

What do you mean by
What do you mean by schematics and how would I post them by pictures fritzing

I need the errors removed
I need the errors removed from code I got the Ldr fixed and the code just. Needs to be fixed.

im making the fritzing thing

im making the fritzing thing right now

heres the schematic

(No subject)

its a pdf

right click on the pdf thing

right click on the pdf thing and open

Can you repost that schematic?

I can’t seem to “open” it or zoom in or anything.

Ok got it now…

Ok, finally got your image open.

Looks pretty straight forward to me (and has been covered by comments above). Right off the bat, you are missing a + connection going to the LDR on the right. I would assume this was a mistake when drawing the picture, and exists in real life. Assuming that is the case, everything actually looks pretty good. I think the real issue here is the fact that, with say, a 5% tolerance on your resistors, you are never going to have 2 set-ups exactly the same.

Here is how you fix it:

1) do what Max said and replace your (2) fixed resistors with pots. You probably want to use a pot with the same value as your LDR’s. From there, just run a test program that spits out the ADC numbers to the serial monitor --Turn the pots until you like the numbers.

2) Fix it in code. At start-up you can run a routine to read your LDR’s. --This is assuming you physically set the robot in a “light-neutral” place at start up. Fire up your code, take a reading from each side and calculate a “fudge factor” number. Use this number later in your code to “fudge” the numbers back to “centered”.

Also, it may help to take a few readings from each LDR and average them.

Dunno 'bout declaring analogs…

I am not sure, but I think if you declare analog inputs as pinMode(A0,INPUT); they become digital inputs. Am I wrong? I should test this maybe…

 

ok

The LDR is connected to vcc i drew the fritsing wrong first time. And i will try with the 5k pots luckily i got 2 at home. and thank you for the help

Heyy!

I recognize that code, It was written by me :slight_smile: although it seems you have made some changes…

Sorry if it gave you any problems…

At first glance I noticed this, 

{

LeftSensor = 960 -  analogRead(LeftSensor);

delay(1); // the delay allows the Analog to Digital converter IC to recover for the next reading.

RightSensor = 1020 -  analogRead(RightSensor);

delay(1);

}

 

it should be {1023 - analogRead();} for both sensors, unless you changed that to equalize the sensors.

It is youre code I could not

It is youre code I could not find a way to make a photovore so I cannabalized youre code from instructables and my own Im Sorry why does it have to 1023 - analogread i do not understand.

that line of code is used to

that line of code is used to invert the value. I use the internal pullup resistor inside the arduino pins and connect the other end of the LDR to ground. The values usually show up as 0 being very bright, and 1023 being completely dark. But I use 1023 - analogRead() to invert the sensor value so that 1023 is the brightest and 0 is the darkest. I find it easier to work with that way. 1023 is the maximum value of the analogRead function. 

I have yet got another proble

Ive got a problem again it is that during debugging the LDRs I get a steadt stream of data but when it is in the real program which also moves the motors the values jump alot and i mean majorly what is the problem is it because my motors are powered by the same powersource as my LDR I think that is the problem if it is what should i do.

When I say jumping I mean
When I say jumping I mean the values of the LDRs and I am using chowmix code cause the code you gave the motor is going so slow that it’s not moving the motors only move from 255 and 220 so I can hear the noise. When I do a test run With only the LDRs the values are the same but when the motors are moving the values change a lot (jumping). I could post a video of the debugging while testing and with the motors running. I was thinking sine the LDRs and motors are connected to the same power rails and power source maybe spikes from the motors are disturbing the LDRs. What do you think.

Definitely try to seperate

Definitely try to seperate the power source. What motor driver are you using. It’ll be very helpful if you can post a schematic. Basically you want to have a power source for the motors that only shares a common ground with the arduino’s power.