LDR light sensor does not return my expected value

I wrote an Arduino program to test two light sensor: one is the LDR light sensor (http://arduino-info.wikispaces.com/Brick-LightSensor), and the other is the Phidget precision light sensor (Precision Light Sensor - 1127_0 at Phidgets). The pictures of results can be found in the http://alturl.com/5vc4m .... From this picture, I found that :

  • at the beginning (time < 500), both sensors are working fine
  • when I turned a light off (400 < time < 700), both sensors returned lower values, but the output values do not overlap
  • when it is dark (700 < time < 1000), both output values are very closed to zero
  • After I turned on the light (1000 < time < 3100), output values of LDR light sensor jumped to 76x, even I turned a light off, LDR light sensor can not output lower values
  • dark again (time > 3100), both output values are very closed to zero

My LDR light sensor, is a new sensor, never returns values higher than 76x. I have two LDR light sensors, they are all have the same problems.

Anyone could tell me what's the problem of my LDR light sensor? Am I bought bad sensors? Should I need to change my code, which is simply the analogRead(LDR_LIGHT_PIN)?

Here comes the codes:

#define LDR_LIGHT_PIN A0
#define PHID_LIGHT_PIN A4

/----( SETUP: RUNS ONCE )----/
void setup()
{
pinMode(LDR_LIGHT_PIN, INPUT); // for LDR light sensor
pinMode(PHID_LIGHT_PIN, INPUT); // for Phidget lighet sensor
Serial.begin(9600); // Enable the Serial data output
Serial.println("#------- Test Light Sensors ---------");
Serial.println("#time (sec), LDR, Phidget Precision--");
}
/--(end setup )---/

/----( LOOP: RUNS CONSTANTLY )----/
void loop()
{ char str[40]; // string for results printing

// print the luminous
sprintf( str, "%06ld, %4d, %4d \0", millis()/1000,
analogRead(LDR_LIGHT_PIN), analogRead(PHID_LIGHT_PIN) );
Serial.println( str );

delay(5000); // Wait 5 second
}
/--(end loop)---/

The two links are the same :frowning: I cannot see the LDR code =>

please post the code which compares the two ...

How did you connect the LDR and how the phidget?
LDR need a voltage divider!

Chacked your wiring ?

PLease modify your post, selct the code and press the # button, the look will be better ...

The analogRead() is not stable in the last bit.

Please try this code:

  • based upon yours
  • increase baudrate
  • removed meaningless comments
  • removed the use of delay
#define LDR_LIGHT_PIN  A0
#define PHID_LIGHT_PIN  A4

unsigned long prevtime = 0;

void setup()
{
  pinMode(LDR_LIGHT_PIN, INPUT);   
  pinMode(PHID_LIGHT_PIN, INPUT);   

  Serial.begin(115200);              

  Serial.println("#------- Test Light Sensors ---------");
  Serial.println("#time (sec), LDR, Phidget Precision--");
}


void loop()
{
  if (millis() - prevtime >= 5000)
  {  
    prevtime = millis();

    // MAKE MULTIPLE READINGS AND AVERAGE THEM TO MINIMIZE "JITTER"
    int ldr = 0;
    for (int i=0; i< 5; i++) ldr += analogRead(LDR_LIGHT_PIN);
    ldr /= 5;

    int phidget = 0;
    for (int i=0; i< 5; i++) phidget += analogRead(PHID_LIGHT_PIN);
    phidget /=5;

    // OUTPUT THE AVERAGED RAW READINGS
    char str[40];   
    sprintf( str, "%06ld, %4d, %4d \0", millis()/1000, ldr, phidget);
    Serial.println( str );
  }
  // ... do other things 
}

Thank you, robtillaart

Change the baud rate is not work for this case. I've tried 9600, 19200,38400,57600, 115200....

I've used your code, but still have the same trouble. The Phidget sensor works properly, but the LDR light sensor returns incorrect results....

BTW, in your code, in the loop(), the second LDR_LIGHT_PIN should be replaced by PHID_LIGHT_PIN

Currently, the output of my LDR light sensor:

  • time > 4000
  • if there is some lights, returns 76x
  • if it is dark, returns 0, or value < 5
  • if the LDR light sensor is under the light directly, it returns 76x

IMHO, the problem is in design LDR brick light-sensor, even I don't have complete schematic of their unit, but what they say on page http://arduino-info.wikispaces.com/Brick-LightSensor
LM324 - used as amplifier, which isn't rail-to rail. Data sheet for OPA says that maximum output voltage for power +5V is 3.3 / 3.5V, this is why you can't get more than 76X .
Are the any reason you need to keep this shield instead of connected separate LDR (I mean just light sensitive resistor) and resistor? You always can calibrate your setup by adjusting external resistor or use a pot.

BTW, in your code, in the loop(), the second LDR_LIGHT_PIN should be replaced by PHID_LIGHT_PIN

modified my post above to reflect that, thanks,

Are the readings more stable?

Thank you, Magician & robtillaart,

The value is stable or not is not my key focus point. Thank you for giving me the hint for solving the future problem.

Now I know the reason why my LDR light sensor can only reach max 76x. But there is still a question......I don't understand why the LDR light sensor can work properly at the beginning, but after I turned off lights and then turn on the light, it cannot work properly anymore.... the return value is always 76x no matter how many lights has been turned on, that is, the illumination of the room is changed, but the LDR sensor always ouput 76x........ the only exception is when the room is totally dark, it returns 0.

Wish I could use the LDR light sensor instead of the LDR + 1k Ohm resistor + breadboard.

Effect, as you described it, likely related to big time constant of the circuitry. Which is completely LDR sensor bias resistor fault selection , or could be some on-board electrolytic cap If there is any, as I don't have a schematic and can't see reverse side of the board. (probably you can post high resolution pictures both side of the board).
Ether way, it's design flow, prototype should be tested thoroughly before production.

Thank you, Magician,

I use the following boards:

  • Arduino UNO
  • Sensor Shield V04, which is mounted on the top of UNO
  • Phidget Precision Light Sensor, a Arduino sensor cable is connected between Sensor Shield and the Sensor
  • LDR Light Sensor, front (http://alturl.com/qwcfh) & back (http://alturl.com/vggkp) pictures

The UNO and the Sensor Shield are the Arduino official products. The Phidget light sensor works fine. So, I think you need the picture of LDR light sensor.

Let me know, if I need to provide more detailed information. :smiley:

Shield isn't the same with shield you posted a link to in first post. I was hoping get better quality pictures, can't see a trace. IMHO, it's unlikely, you can do anything in order to fix it.

edited:
There is a link that explain what are missing components for:
http://iteadstudio.com/store/index.php?main_page=product_info&cPath=62_65&products_id=461

try to use the LDR only, without IC

Thank you, Magician & Testato,

The image of my LDR light sensor (the latter one) is different my first post. Do you mean you need the image from different angles?

I used a 100K ohms resistor + LDR and get the following results http://alturl.com/dt2zt
I'm not sure whether the lux equation is correct or not. Here is my code:

#define LDR_LIGHT_PIN  A1 
#define PHID_LIGHT_PIN  A4 

/*----( SETUP: RUNS ONCE )----*/
void setup()
{
   pinMode(LDR_LIGHT_PIN, INPUT);  // for LDR light sensor
   pinMode(PHID_LIGHT_PIN, INPUT);  // for Phidget lighet sensor
   Serial.begin(9600);             // Enable the Serial data output
   Serial.println("#------- Test Light Sensors ---------");
   Serial.println("#time (sec), LDR, Phidget Precision--");
}
/*--(end setup )---*/


/*----( LOOP: RUNS CONSTANTLY )----*/
void loop()
{  char str[40];   // string for results printing

   // calculate the voltage, v_out = analog reading * 5V / 1024
   float v_out = analogRead(LDR_LIGHT_PIN) * 0.0048828125;

   // calculate the illumination, 100K Ohms, 
   int lux = 500 / ( 100.0 *( (5.0 - v_out) / v_out ) );

   // print the luminous
   sprintf( str, "%06ld, %4d, %4d \0", millis()/1000, lux, analogRead(PHID_LIGHT_PIN) );
   Serial.println( str );

   delay(5000);                          // Wait 5 second
}
/*--(end loop )---*/

so now it work ?
why not used 10K res like in the schematics ?
you may use a pot for calibrate the LDR to the other sensor result

Thank you, Testato

I do not have 10K ohms resister in hand, can only find the 100k one.

Actually, If anyone could help me to figure out the problem of my LDR sensor module that will be great.

The LDR + resister is just another exercise for learning how to calculate the lux, and wish can know whether my lux equation is correct or not?

// calculate the illumination, 100K Ohms, 
   int lux = 500 / ( 100.0 *( (5.0 - v_out) / v_out ) );

Don't think this is correct equation.
Basically, there are two variable:

  1. LDR resistance , which is depends on specific chemistry of the sensor. You should make your research on what material LDR made from, how resistance of given sensor depends on LUX. Most cases it isn't linear, but could be approximated to linear with log scale. If you get a formula
    R = f (LUX), next task would be transform resistor values to voltage, that arduino can measure.

  2. Voltage output of resistor divider, composed of LDR and constant resistor. Voltage output is depends of both resistors values, and is not linear too.

Good articles to start research:

http://www.ladyada.net/learn/sensors/cds.html

edited:
Good tutorial on alternatives for resistor divider :
http://saba.kntu.ac.ir/eecd/ecourses/inst%2086/Projects/Instrumentational%20Amplifier/Shayeste%20Kiaei/conditioning%20circuit-sent/resistance-to-voltaget.htm