LDR picking up flicker from LED - Using an LDR to measure output from LED

Hi,

I have this project in which I am to use an LDR to measure the voltage output from an LED. The purpose of this project is to measure the voltage drop when I place something in between the ldr and led to which measures the amount of light passing through the object.

The led brightness is being controlled through pwm in which I will manually change the int value.

The problem which I am facing is that the ldr is picking up on the led flickering and is not giving me a stable value unless i set the led at max(255). I need to be able to tune the led brightness using the pwm function and still read a stable value from the ldr.

Any kind soul please help figure out what the problem is thank you.

int sensorPin = A0; // select the input pin for LDR
int lum = 100; // led output
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
Serial.begin(9600); //sets serial port for communication
pinMode(9, OUTPUT);
}
void loop() {
analogWrite(9, lum);
sensorValue = analogRead(sensorPin); // read the value from the sensor
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage); //prints the values coming from the sensor on the screen
Serial.println("V");
delay(500);

}

Consider adding a capacitor on the LED-side of the 220R resistor. You can calculate a good value using your PWM frequency, resistor value, time constant etc. You can probably start with 0.1uF just to baseline it.

Take many readings and average those readings to smooth out the pulses from PWM.

PWM is done at 490Hz. If you use the tone() function on your pin, you can drive it at a much higher frequency which might fool the LDR. If not, you can always average multiple readings of your LDR.

You can try putting a capacitor in parallel with the 2K resistor. For example, the RC time constant (R x C) of 2K and 500uF is one second. (The effective "R" is actually the parallel equivalent of the fixed resistor and LDR, so the actual time constant will be less.)

Or you can try some [u]smoothing[/u].

And, I assume the whole thing is in a light-tight box? Fluorescent lights flicker at the power line frequency.

Thank you all for the prompt replies. I will try adding a capacitor to see if it helps with the measurement.

DVDdoug:
You can try putting a capacitor in parallel with the 2K resistor. For example, the RC time constant (R x C) of 2K and 500uF is one second. (The effective "R" is actually the parallel equivalent of the fixed resistor and LDR, so the actual time constant will be less.)

And, I assume the whole thing is in a light-tight box? Fluorescent lights flicker at the power line frequency.

Thank you sir, your solution works. And yes the whole thing is in a light tight box. Unfortunately i do not have a 500uF capacitor with me right now instead I am using a 100uF capacitor. Will this be accurate enough?

robinjky:
instead I am using a 100uF capacitor. Will this be accurate enough?

I'm sure your measurements will tell... Try it out! Value is not critical; the larger the slower the reaction of the LDR.