# Light dependent resistor

I need to understand if someone has switched on/off a light in a room. I want to use a LDR but I need an algorithm to differentiate between ambient light changes and a light bulb switching on/off.

Please specify how close you can get to the bulb. Can you measure the on/off voltage, or the light emitted by the bulb, or do you look into a distant room through a window?

Find a magnifying glass and a tube with matching diameters. Check where you want to place the tube. Aim it against the light bulb. Place the LDR in the focal point inside the tube where the light bulb light hits.

I cant get close to the bulb. The sensor will be in a box that will be randomly placed in a room. What
I need to resolve is how to understand the difference between the sun gradually going up and down, sudden clouds, closing curtains, etc and a light bulb suddenly being switched on or off

bahamaschris:
I cant get close to the bulb. The sensor will be in a box that will be randomly placed in a room. What
I need to resolve is how to understand the difference between the sun gradually going up and down, sudden clouds, closing curtains, etc and a light bulb suddenly being switched on or off

For the things that you called "sudden", there really is no difference. If it were a phototransistor, you could look for the presence of 50/60Hz line flicker. The LDR will pickup up a tiny bit of that, but not much because it reacts too slowly.

Put a voltage or current sensor into the light bulb supply and a wireless module (BT...) that transmits the light state to a receiver.

bahamaschris,

I think the 1st thing you need to do hook it up and run [u]AnalogRead[/u] to see if you're getting useful data.

If your brain can tell if the light is on or off you can start working on a program/algorithm. If you can't tell by looking at the readings, it's probably not going to work.

Make a voltage divider, 5V to LDR to analog in pin (A0), A0 to 100k resistor to GND, sample the voltage 4 times per second. If the ADC results compared to the previous results jumps up or down more than a certain amount, the light switched. Probably take lots of trial and error.

If you don't have the LDR yet, consider a phototransistor circuit instead. Then you can analog sample it at 4x the line frequency in your country. The 4 samples can be interpreted as a quadrature detector, which can differentiate the sampling frequency from DC and other frequencies. If you have 4 consecutive samples, 1-2-3-4, 1-3 will give you 'I' and 2-4 will give you 'Q'. In other words, multiply 1 by 1, 2 by 1, 3 by -1 and 4 by -1. If you accumulate and average (really it's a low pass filter), you will have a very sensitive line frequency detector. This will probably detect the light very reliably, unless there are many lights.

If, however, this is a school exercise, you’ll have to work within the constraints you have been given. You then have to look for a rapid change in the light level. Assuming that the light is not on a dimmer control, it will be about the only thing which will cause a measurable change in the light level within say 100 mS.

The 50/60 Hz flicker is probably the best clue to whether the light is on or off. And as has been pointed out, a photo transistor is better to detect that than an LDR.

I cant get close to the bulb. The sensor will be in a box that will be randomly placed in a room.

Well, that really is an impossible task, then. What are random places? On the floor? Behind the curtains? Under the bed? No way to adjust its position?

Can someone explain the benefits of a photo transistor over an LDR? My research suggests that an LDR is more capable of measuring light levels whereas photo resistors are a bit more binary in that their current increases exponentially when light is detected. As I want to carefully distinguish artificial light being activated, even in the presence of daylight, I would have thought that an LDR would have been the better choice but am in no way an expert and am happy to learn otherwise.

It's not a school exercise but a small part of an intended product. Among many other things, that product needs to know if somebody in the same room as the product has switched on or off an artificial light. It could be in the USA where the frequency is 60Hz or in Europe at 50Hz.

The product cannot be targeted (positioned) at a specific light bulb but simply needs to know if somebody has switched on a light.

I think the 1st thing you need to do hook it up and run AnalogRead to see if you're getting useful data.

If your brain can tell if the light is on or off you can start working on a program/algorithm. If you can't tell by looking at the readings, it's probably not going to work.

This is certainly the correct approach. I guess I was hoping that someone could share their experience of producing an algorithm of a similar problem.

Well, that really is an impossible task, then. What are random places? On the floor? Behind the curtains? Under the bed? No way to adjust its position?

The user will place the box in their room at a convenient location for them. It will be exposed but not pointed in a particular position. As I said earlier, this is just a small part of the function that the box has to perform.

Is the mains frequency flicker thing so pronounced with LED bulbs now?

bahamaschris:
Can someone explain the benefits of a photo transistor over an LDR? My research suggests that an LDR is more capable of measuring light levels whereas photo resistors are a bit more binary in that their current increases exponentially when light is detected. As I want to carefully distinguish artificial light being activated, even in the presence of daylight, I would have thought that an LDR would have been the better choice but am in no way an expert and am happy to learn otherwise

A phototransistor is much faster, has a higher luminance sensitivity bandwidth. How is an exponential function binary? In your research, you probably found a lot of application notes. Did you not see them used in a variety of photometric applications?

Both devices are so cheap, you are wasting a lot of time agonizing over the difference. You can buy and try both for the price of a cup of coffee. Your real challenge will be the code. Have you made any progress understanding the demodulator formula in reply #8? It's a direct conversion AM receiver.

To answer a late breaking question, most LED bulbs rectify but don't filter the AC. So yes, they flicker. Just not as much as a gas tube.

This is a digitally filtered signal of my KY-039 heart beat sensor, which has an infrared light transistor.

The big waves are my heart beat, but the small zigzag waves are the 50 Hz flickering from the ceiling led lights in my kitchen. If you don't want to buy both LDRs and light transistors, just compare their datasheets. You get the speed of LDRs from the datasheet. I bet you will find out that they are too slow for reliable detection of a 50 Hz signal.

For a solar tracker project, I used a separate photo diode that is used to detect ambient light. I placed a shade over the photo diode to dampen sudden light changes and hard coded a ambient light number to be used in a comparison detection code.

You may want to consider that if you are making a consumer product you might want to add in 2 circuits and code. Add in some form of ambient light detection and add in a user adjustable ambient light level threshold thingy.

Have you made any progress understanding the demodulator formula in reply #8? It's a direct conversion AM receiver.

This is a bit beyond me, it is not my field. I haven't made much progress although I have been trying to research it. The author did say that it wouldn't work if there were many lights and there may very well be. This method, I believe, is trying to detect the artificial light from the line frequency. This was not really the method that I was trying to use.
I was hoping to find an algorithm that would detect the "jump" in light detected to signal an event (that the light was switched on or off) without getting caught by someone closing curtains, sudden cloud cover, etc.
To be more clear, the product cannot tolerate any false positives at all.

Out of interest, I am new to this forum and have not understood how to insert a quote, from a previous reply, that includes the author, date and time as others do.

There is no "algorithm" that performs magic. It's all math. You need to respect the laws of nature in your design. It doesn't matter what you "want" if it contradicts the basic functioning of the universe. An LDR has no eyes or brain. It sees only intensity with minimal directional component.

There have been replies here already pointing out the absence of any real difference between the light and ambient light. You seem to be just fishing over and over again for what you want, seemingly dismissing novel solutions that might work.

The simplest algorithm (or method) simply looks at the rate of change of the light. Perhaps at your apparent level of thinking, you should just try for that. It's a very easy method to implement. Try it out and see how it works - actually you could have finished this in the time you've spent posting here.

aarg:
If you don't have the LDR yet, consider a phototransistor circuit instead. Then you can analog sample it at 4x the line frequency in your country. The 4 samples can be interpreted as a quadrature detector, which can differentiate the sampling frequency from DC and other frequencies. If you have 4 consecutive samples, 1-2-3-4, 1-3 will give you 'I' and 2-4 will give you 'Q'. In other words, multiply 1 by 1, 2 by 1, 3 by -1 and 4 by -1. If you accumulate and average (really it's a low pass filter), you will have a very sensitive line frequency detector. This will probably detect the light very reliably, unless there are many lights.

Interesting concept, I'll have to give this a try when I get the time.

Nitpicking question though. Won't the fundamental frequency be twice the line frequency? For an incandescent bulb, for instance light output power is nominally V2/R so

Vpsin(wt)2/R = Vpsin(2wt)/(2R) by trig identities

bahamaschris:
To be more clear, the product cannot tolerate any false positives at all.

That's probably not possible with remote sensing.