2
\$\begingroup\$

I'm getting strange data from my LDR sensor.

Without any significant changes in light is constantly goes from 0 to its max value 1023 and back.

What could be the problem? Is it possible my LDR sensor is broken?

Code:

int ldr = 0;             //analog pin to which LDR is connected
int ldr_value = 0;        //variable to store LDR values

void setup()
{
  Serial.begin(9600);   //start te serial monitor
}

void loop()
{
  ldr_value = analogRead(ldr);          //reads the LDR values
  Serial.println(ldr_value);                 //prints the LDR values to serial monitor
  delay(1000);                  //wait
}

Output:

0
0
0
0
0
26
102
206
349
505
643
813
957
1023
1023
1023
964
821
633
434
249
83
0
0
0
0

and so on

\$\endgroup\$
3
\$\begingroup\$

This suggests you didn't properly connect it. I'll presume you have your LDR connected to ground and the series resistor that forms the divider to Vcc. If the value is zero, that would mean that the LDR's resistance is less than 1/1000th of the series resistor. That's possible. But then the 1023 isn't possible, because that would mean that the LDR's resistance is larger than 1000 times the series resistor, and LDR's don't have a > 1000 000 range between light and dark resistance.

If you have a multimeter measure the LDR's dark and light resistance, and pick a series resistance in between those values. The series resistance goes between Vcc and tha ADC input, the LDR between the ADC input and ground.

edit
You say the pin was unconnected, and still you got this input variations. If you plot them out you get a perfect sine:

enter image description here

It's not impossible that the ADC picked this up from the noise the mains radiate. The sine is much slower than the mains frequency, but that's an artifact you can get with subsampling.

\$\endgroup\$
  • \$\begingroup\$ I indeed misplaced a wire, the analog in port was connected to an empty row on my breadboard. \$\endgroup\$ – Boyd Jun 12 '12 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.