# Problems with light sensor

Hi,i am working with this Photoresistor Sensor Module Light Detection Light for Arduino,and i found this code ,i tested it but the problem it gives me is that during day/light the intensity/value in lux of this sensor decreses,when it is night the value in lux increses.Can someone tell me how to fix this problem.

This is the code i tested:

/* Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground

int photocellPin0 = 0; // the cell and 10K pulldown are connected to a0
int photocellReading0; // the analog reading from the analog resistor divider
float Res0=10.0; // Resistance in the circuit of sensor 0 (KOhms)
// depending of the Resistance used, you could measure better at dark or at bright conditions.
// you could use a double circuit (using other LDR connected to analog pin 1) to have fun testing the sensors.
// Change the value of Res0 depending of what you use in the circuit

void setup(void) {
// We’ll send debugging information via the Serial monitor
Serial.begin(9600);
}

void loop(void) {
float Vout0=photocellReading00.0048828125; // calculate the voltage
int lux0=500/(Res0
((5-Vout0)/Vout0)); // calculate the Lux
Serial.print(“Luminosidad 0: “); // Print the measurement (in Lux units) in the screen
Serial.print(lux0);
Serial.print(” Lux\t”);
Serial.print(“Voltage: “); // Print the calculated voltage returned to pin 0
Serial.print(Vout0);
Serial.print(” Volts\t”);
Serial.print(“Output: “);
Serial.print(photocellReading0); // Print the measured level at pin 0
Serial.print(“Ligth conditions: “); // Print an approach to ligth conditions
if (photocellReading0 < 10) {
Serial.println(” - Dark”);
} else if (photocellReading0 < 200) {
Serial.println(” - Dim”);
} else if (photocellReading0 < 500) {
Serial.println(" - Light");
} else if (photocellReading0 < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
delay(1000);
}

Instead of a simple LDR i used this photoresistive sensor light module:http://www.ebay.com/itm/2PCS-Photoresistor-Sensor-Module-Light-Detection-Light-for-Arduino-NEW-/261810233117?pt=LH_DefaultDomain_0&hash=item3cf51b1f1d The problem can be the connections of the light module to the Arduino. The sketch assumes that the LDR is connected to a 10k resistor. What is the value of the resistor in the case of your light module?

i made excactly what is described in this link:Using a Photocell | Photocells | Adafruit Learning System
I connected the Photoresistor light sensor with a 10 k resistor.
I have another problem that i didn’t found any datasheet for this sensor i am using(so actualy i don’t know what is the value of the resistor of this photoresistor light sensor ).Can you help me please?

``````Serial.print(photocellReading0);               // Print the measured level at pin 0
``````

does this reading seem right? if not why?
Do you have a small capacitor (.01 or .1mf) between the analog pin and ground? It would also be good to add a few other caps on the power rails.

"actualy i don't know what is the value of the resistor of this photoresistor light sensor"
Do you have a volt ohm meter, then you can measure it. Else, go get a VOM.

"If you'r gonna play in Texas, you'v got to have a fiddle in the band"
If you'r gonna play with electronics, you'v got to have a VOM on hand!

Thank you,i found at your forum this code:

5v----10k–v–LDR----GND
(A5)___

All self explanatory, the v and (A5) below it is the connection between 10k and LDR to analogue pin 5 on the arduino.

The code is tuned to the circuit you’re using, to be as precise as possible, so where it says 10.72 that is the 10k resistor in kOhms that I checked using a mulitmeter. Likewise I double checked the 5v, which was exactly 5v on my voltmeter. The very small long number (0.00488…) is 5/1023, again use the true voltage to get accurate results.

Quote

int LDR = 5; // select the input pin for the LDR

void setup() {
Serial.begin(9600);
pinMode(LDR, INPUT); // declare the LDR as an INPUT
}

void loop()
{
float volts0=photocellReading0*0.004887585532746823069403714565; // calculate the voltage
Serial.print(volts0); //raw voltage
Serial.println(" Volts.");
Serial.print((500/((10.72/(5-volts0))*volts0)), 2); //lux calculation
Serial.print(" Lux.");
Serial.println("");
delay(1000); //delay for a second
}

i tested it and i dont have any more the problem that (when it was light the lux level decresed,now that i tested the new code when the light level increse lux level increse) but i have still the problem in this part of the code:
if (photocellReading0 < 10) {
Serial.println(" - Dark");
} else if (photocellReading0 < 200) {
Serial.println(" - Dim");
} else if (photocellReading0 < 500) {
Serial.println(" - Light");
} else if (photocellReading0 < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}

the serial monitor shows lux level 105------Dark
lux level 220---------Dark
lux leven 452--------Dark
How can i fix this problem?

1 Like

Sorry, but I can’t see how the script you provide, will produce the output that you show. Can you check that?

I had previously ask a couple 3 questions that I did not get answers to. Would you mind providing answers, so we may more understand, and help with your post?

Was it the ldr that wad 10.72k?
And the fixed resistor was at 10.00k ?

In your schematic, the ldr being on the low side (to ground) would make the signal reverse of what you were expecting you know?

Just for debugging, maybe add photocellReading0 to each of the printlns.
if (photocellReading0 < 10) {
Serial.println(" - Dark");
} else if (photocellReading0 < 200) {
Serial.println(" - Dim");
} else if (photocellReading0 < 500) {
Serial.println(" - Light");
} else if (photocellReading0 < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}

1 Like

Hi,
If you are using the adafruit sketch.
You need the LDR between the 5V+ and A0
The 10K between A0 and Gnd
The opposite to what you have in said in post #4.

That is why your values are backwards.

Tom.... 1 Like

Thank you guys,all what you said was very helpfull,i resolved the problem

1 Like

Hi,
To help us all, can you tell us what the problem was.

Tom..... 1 Like