LDR giving inconsistent displays

Dear all,

I have been working on a little project, involving a LDR and 6 servos (powered by 5V). The servo motors are up and running. As you can see in the schematic, I have a power module for consistent power (needed for the servos to run multiple at a time). My problem is as stated below.

I mounted the LDR to a backlit screen, which has the same brightness every time (the LDR is protected/covered from any outsource light). I had a 100.00kohm resistor in place, which works perfectly, except for the following:a lot of times the LDR detects light around 964/963/962, but sometimes it gives me the value of 96. What is the problem?

I fiddled around with a single ground node (the power module has 1 on each side), also with the given ground node on the arduino itself, but no change. Another thing that I’ve tested is with several other resistors (of the same resistance, or other resistances like 10.00kohm)

The code is as followed (a long code, but most of the stuff:

#include <Servo.h>

Servo servo1; //servo1 = left/up
Servo servo2; //servo2 = right/down
Servo servo3; //servo3 = x/a
Servo servo4; //servo4 = y/b
Servo servo5; //servo5 = start
Servo servo6; //servo6 = select

int sensorPin = A0;
int sensorValue = 0;

int minLight; //Used to calibrate the readings
int maxLight; //Used to calibrate the readings
int lightLevel;

int rest = 90;
int servo1press = 155; //155
int servo1release = 20; //20

int servo2press= 160; //160
int servo2release = 20; //20

int servo3press= 160; //160
int servo3release = 15; //20

int servo4press= 155; //170
int servo4release = 5; //10

int servo5press= 20; //40

int servo6press = 155; //40

int pos = 0; // variable to store the servo position

void setup() {
servo1.attach(3); // attaches the servo on pin 3 to the servo object
servo2.attach(5); //""pin 5 “”
servo3.attach(6);
servo4.attach(9);
servo5.attach(10);
servo6.attach(11);

minLight=15;
maxLight=20;
Serial.begin(9600);
}

void loop() {

delay(500);
servo3.write(servo3release);

sensorValue=analogRead(sensorPin);
Serial.println(sensorValue);

if((sensorValue>960) && (sensorValue<965)){

servo3.write(servo3release);

delay(3500);
}

else{
exit(0);
}

}

The servo power supply needs to be separate from the Arduino power supply (but with the grounds connected), to avoid noise in ADC readings.

Please post a wiring diagram (not Fritzing), showing how everything is currently powered.

not sure what you mean, how to show a different diagram. The picture that I sent is exactly how I wired it, except it has 1 powersupply 5 V and those drive the servos. The arduino of course connected to the laptop, but not providing 5V to anything (purely use the connection pins)

The servo power supply needs to be separate from the Arduino power supply (but with the grounds connected), to avoid noise in ADC readings.

Explanation: you need two different power supplies, one for the Arduino and ADC/LDR, the other for the servos. Connect the grounds.

Ah I thought...thank you, I will make sure to do that and I'll reply to this post tomorrow whether the problem has been solved.

MartinQB: a lot of times the LDR detects light around 964/963/962, but sometimes it gives me the value of 96. What is the problem?

Loose wire? Is everything soldered or how did you make the connections?

not sure what you mean, how to show a different diagram.

A "schematic diagram" is the accepted international language used to communicate circuit information. Very unlike Fritzing, such diagrams are clear and unambiguous.

If you are sincerely interested in this hobby, learn to read and draw schematic diagrams (pencil and paper is fine). Tutorial here.

Why are you calling exit()?

I made the connections with the wires that are appropriate for the arduino, i did solder 2 wires to the LDR, so ill check if that connection is in any way at fault. The connections for the rest are on a breadboard, as shown in the picture. This is the current layout

What seems to be the problem is that the frame before the current screen is of value 98/96, whereas the desired frame has 987 or something. Is it possible that the LDR reads the value BEFORE the delay, then delays and then displays the number? read--->delay--->display

but my code shows delay--->read--->display

delay(3000);

sensorValue=analogRead(sensorPin); Serial.println(sensorValue);

I still have not been able to fix it, anyone knows whats wrong?

Wired using a solderless breadboard? Then try a different part of your breadboard. Poor connections are a common issue with such breadboards.

A proper circuit diagram may be helpful, too. Those Fritzies are generally useless.

anyone knows whats wrong?

Yes. It is a problem with the code or the wiring or the parts you used.