Fluctuations in photodiode readings

Hi everyone,

I've been working with some photodiodes (Honeywell SD3410-001) to create a brightness detecting display that turns on when it is dark. Instead of a simple on-off mechanism, I wanted to see if I could create a fade in/fade out mechanism using the value read from the photodiodes and mapping the value between 0 and 255 to send to the analogWrite() function.

I followed this tutorial to get an idea of how to use the photodiodes, but noticed that there are a lot of fluctuations in the readings. I plotted the value out and saw that this was happening:

Mapping these values directly to analogWrite will cause the LED lights to flicker a lot, so I tried averaging the values out... But the sinusoidal pattern still persists.

I want to know why this is happening. I've searched the forum a bit and saw that it might be due to the voltage provider of the Arduino (I am using the USB port to provide power) or because of the frequency of the lights themselves. It happens in sunlight too, so I'm guessing that it's due to the voltage provider...

Any insights?

If you're using artificial lighting there will always be some variation due to the alternating voltage of the supply.

At 50 or 60 Hz x 2 depending where you live.

I bet you don't get this problem with daylight.

Take an average , or provide some analog smoothing.

Could you post your circuit?

Allan

Any insights?

Not without seeing the properly posted code (use code tags) and a circuit diagram (not Fritzing, please). The tutorial you linked does not use a photodiode, it uses an LDR.

See the "How to use this forum" post.

allanhurst:
I bet you don't get this problem with daylight.

I actually do get it in sunlight too. :frowning: It's too late to plot it now, but I can plot the results from sunlight tomorrow when the sun comes back up.

Here is the code I used to get the readings, real simple:

void setup() {
  Serial.begin(9600);
  delay(500);
}

void loop() {
  sensorVal = analogRead(0);
  Serial.println(sensorVal, DEC);
}

And here is a picture of the circuit. I use a Due and its pin 0 for the reading.

That is a Darlington phototransistor. Data sheet here, unfortunately the pinout diagram is wrong and/or difficult to interpret.

What is the value of the associated resistor (it looks like 1K)?

There are two ways to wire a phototransistor in that circuit, so which is it?

The resistor is 10K and here is a drawing of the breadboard if this makes any more sense.

From the Due's analogue-in part of the datasheet:
"Applying more than 3.3V on the Due’s pins will damage the SAM3X chip."
I think I see the photodiode connected to 5volt.
Leo..

No, that diagram is not helpful. We need to know exactly how you have wired the collector and emitter of the phototransistor. The collector should be connected to 3.3V.

If you wired it backwards, it might have been destroyed by applying 5V, as that is the EC breakdown voltage.

See above about damaging the analog input!

Since the saturation voltage drop of the phototransistor is about 1.1V, this gives you very little dynamic range for light levels. This part is not a good choice for your project.

That is a Darlington phototransistor. Data sheet here, unfortunately the pinout diagram is wrong and/or difficult to interpret.

The pinout is quite clear : pin1 = emitter, 3 = collector.

But I'm puzzled as to the ac type waveform in daylight... mains noise? though a 10k isn't that low impedance....

And the code will give a sampling rate dependent of the 9600 baud send delay. If any on a Due....

Allan

Wow, thanks for pointing the 3.3V out! I switched to a different board just in case I damaged the other one. The original one, thankfully, seems to operate okay...

jremington:
No, that diagram is not helpful. We need to know exactly how you have wired the collector and emitter of the phototransistor. The collector should be connected to 3.3V.

According to the data sheet, the one closest to the little tab on the casing (as can be seen from the picture) is the emitter. Thus, like you said, the collector is connected to the 3.3V (not 5V anymore).

I'm going to use the newly wired set and see what it does in sunlight tomorrow. That will help me figure out what the cause of the fluctuations is.

allanhurst:
Take an average , or provide some analog smoothing.

What are some analog smoothing methods I can use? I've read some things about moving averages, but I wonder if you guys have any other suggestions.

Assuming you want to measure ambient light...
Can't you just use a slooow photoresistor (LDR).
And a cap across, to make it even slower.

Makes more sense (for pin/wiring safety) to use the photo transistor (or LDR) from pin to ground.
With a pull up resistor from pin to 3.3volt.

The map() function can fix both inverted and range.

I would suggest to use the internal pull up (no external resistor) on the analogue pin,
but I'm not sure it will work on a Due (SAM3X A/D chip).
Leo..

Edit: Can also add a smoothing cap across the photo transistor

Wawa:
Assuming you want to measure ambient light...
Can't you just use a slooow photoresistor (LDR).
And a cap across, to make it even slower.

+1

If you still want to use the photodiode:
To filter the signal, you could use a single-pole digital filter, and then use hysteresis to decide whether to turn on or off the lights.

Pieter

A moving average algorithm. Updated every reading

    ave1 += inputval;
    ave1 *= (1-1/aveLen);
    ave2 = ave1/(aveLen-1);

Ave1,ave2 are floats. aveLen (int) is the number of samples over which to average.

ave2 gives the smoothed value.

Allan