What is the best way for Arduino to determine if it's daylight?

Stupid me. I searched just under "light dependent resistor" and not "photocell". If my PIR doesn't already have a built-in light sensor I will buy the one you pointed out. THANKS!

I just looked at the product description of my PIR but it doesn't say anything about having a built-in light sensor...is there a way to tell from looking at this?

http://www.cutedigi.com/product_info.php?products_id=4290&osCsid=cadcd5e3b5935ee1ad15be97e5f076ba

That link didnt show anything usable 8) My best guess is that this sensor doesn't have a light sensor, as there's only one output...

For LDR's: http://search.digikey.com/ca/en/cat/sensors-transducers/optical-photo-detectors-cds-photoconductive-cells-ambient-light/1967023?k=Light%20resistor

Many thanks to Hideout, PaulS, and WildBill. I love this forum! :slight_smile:

http://www.robotshop.com/productinfo.aspx?pc=RB-Spa-72&lang=en-US

Hi flyingsteve, thanks for posting. Turns out the one Bill suggested is only $1.50, 6 dollars cheaper. Plus, I won't need to be sewing it into my clothes as that is what it looks like the Lilypad is for. Thanks though!

Remember daylight is a lot brighter than artificial light - the threshold level you want will depend on whether its inside or outside.

If outdoors you need to add some hysteresis to the system or it'll go crazy for a while as light levels fall/rise past the threshold at sunset/sunrise. This can be as simple as only sampling every 5 minutes.

WOOSH! The word hysteresis just flew over my head. I wiki'ed it but still don't get it. Even the wiki WOOSHed over my head.

Can you explain what that is like I am FIVE YEARS OLD?

noobtoarduino:
WOOSH! The word hysteresis just flew over my head. I wiki'ed it but still don't get it. Even the wiki WOOSHed over my head.

Can you explain what that is like I am FIVE YEARS OLD?

You want your action to lag the immediate sensor readout, in order no to react too frequently?!

A different direction: add a real time clock. You can use the time of the day to decide when to turn on and when to stay off. You can't be too far off. If you have both clock and light sensor, you are even better off.

Basically, when you are programming your Arduino, you want to have the system interval delays between each time it checks the sensor. What this means is that instead of checking every cycle the Arduino is active, we only check the sensor every x units of time (in this case, 5 minutes). There are a few reasons why you would want to do this:

#1 - Efficiency
Think of your Arduino having a bag of coins. Each time you scan the sensor, your Arduino spends a coin. If your Arduino is out of coins, it can't scan anymore. In comparison, these coins are a representation of your battery life if you are using battery packs. Each time you have your Arduino do something, it uses up a bit of juice from the battery. The more intense the action, much like when you have your computer do some hard processing, the more power that is consumed.

#2 - Result Differences
Ultimately, each scan via the sensor shouldn't yield any big results if you keep scanning every moment the Arduino can. This can be compared to if you blink at this very moment. The room you are in (assuming you are inside) hasn't gotten any brighter nor darker than the moment before you blinked. This is much like an Arduino doing a scan on the sensor without a delay interval. In that case, we are simply collecting information that isn't very helpful and are having the Arduino do unneeded cycles. We can better see differences that may matter if we scan every 5 minutes, depending on what you are doing.

Depending on how fast you want the Arduino to respond will ultimately depend on if you use the interval system or not. If you need it to react in under a moment's notice, then you may want a smaller delay. If having to wait the maximum of 5 minutes for a reaction towards your desired action, then 5 minutes may be the best route. Programming with delay intervals during your 'main loop' is a pretty efficient way of keeping your Arduino and batteries happy while having other useful benefits not listed here.

SUMMERY:

  • Your Arduino uses more battery power if you have it do more intense actions.
  • Ultimately, your sensor won't show any different results from the first millisecond than from the next. Spacing out your scans can help locate differences in lighting.

Hope this helps.

-Flame

EDIT: Being able to use a real time clock sounds also like a good idea. I haven't messed with using clock timing with the Arduino, but I'll assume there are a few tricks here and there that should work.

noobtoarduino:
WOOSH! The word hysteresis just flew over my head. I wiki'ed it but still don't get it. Even the wiki WOOSHed over my head.

Can you explain what that is like I am FIVE YEARS OLD?

Hysteresis is simply that the system doesn't react in exactly the same way going in two different directions. This can be used to stop the system from "dithering" at the switch-over point.

To take an easy example that I am doing at the moment, I am building a '168 controlled greenhouse heater. It measures the temperature and if it gets too low it switches on a heater. Once it's warm enough, the heater goes off.

If I switch on at below (say) 5'C and off at above 5'C then if the temperature hovers around 5'c then I may switch on and off repeatedly and burn out the relay. I therefore built in two safety features:

1 - I sample for 2 minutes and average before making a decision.
2 - I switch on at below 5'C and off again at above 6'C.

Number 2 is the "hysteresis" - it behaves differently depending upon whether the temperature is rising or falling. When the temp is falling it has to reach below 5'C to trigger the switch (on) when it is rising I have to reach 6'C for an off-event.

There will be more noise and general over-sensitiveness on your LDR so you could easily get a load of nonsense switching around the threshold point. As a result you can either:

Have a different level for switch-on in comparison with switch-off, so that once on, the level has to rise a measurable amount before it will switch off again; or

Average (or simply wait to sample) for a while before making a decision. Then you have time for the overall trend to lighter or darker to overtake the noise and also even if you do switch on and off again a few times it won't be flickering like a dying strip-light for 20 minutes each morning and evening. Of course you can do both, as I did above.

Hope that makes some sense.

Liudr,

Ohhh I see. I want to avoid the time clock...because here where I live it can get so cloudy during the day that my room is dark. Thus, I really want it to respond to light levels in the room as opposed to time of day. Thank you for the idea though!

Hi Flame!

Every one of your posts definitely helps me towards a better understanding.

#1. If my gadget is plugged into a DC adapter, which it is, I won't have to worry about battery life. Thanks for bringing up this point though!

#2. I'm using the PIR to detect when I walk into my room. If I scan every 5 minutes..or even every 10 seconds...there's too much of a lag that my room won't light up as soon as I walk into it. I'd like my light to go on the instant I walk into my room.

Dr Ugi,

I'm 5 years old and I don't know what "dithering" means. :slight_smile:

But I do understand your greenhouse heater example. It's like when a trapped mouse runs back and forth until it tires itself out.

I'll be sure to use different values for switch-on and switch-off. I understand that using a single value can cause a LOT of switching and that's bad.

Thanks for your insight!

Dr Ugi -
That was perhaps one of the best explanations of hysteresis and dithering I've seen in awhile! Kudos!

As for for the project discussion, I spot a small issue with the light sensing to control other lights: if you are using the light sensor to scan for the absence of light to turn a light on, wouldn't the light you just turned on result in the sensor turning said lights off and putting it into a loop? Just a possible problem I picked up on.

-Flame

Hi Flame,

Hmm....my original project was to just user a PIR to detect when I come into my room and turn on the light. However, it worked during day and night and I didn't need a light coming on during the day.

The light sensor should not have the ability to turn off the light, only the ability to turn it on if the light sensor senses it is dark in the room. This discussion is helpful because once my light sensor/photocell arrives in the mail I have to figure out a way to program what I just said.

This is what I have so far. This code simply turns on the light when I walk into the room, and stays on for 1 minute.

/* PIR sensor tester*/

int ledPin = 9; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);
}

void loop(){
 
val = digitalRead(inputPin); // read input value


if (val == HIGH) 
{ // check if the input is HIGH
    digitalWrite(ledPin, HIGH); // turn LED ON
    if (pirState == LOW) {
        // we have just turned on
        Serial.println("Motion detected!");
        // We only want to print on the output change, not state
        pirState = HIGH;
    }
}else 
{
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
        // we have just turned off
        Serial.println("Motion ended!");
        // We only want to print on the output change, not state
        pirState = LOW;
    }
}
}

noobtoarduino:
Hi flyingsteve, thanks for posting. Turns out the one Bill suggested is only $1.50, 6 dollars cheaper. Plus, I won't need to be sewing it into my clothes as that is what it looks like the Lilypad is for. Thanks though!

A simple photoresistor will do the trick. I figured I'd show you that one since it gives a more specific sensor output right from the box... No tinkering required. Also, I thought you might mention the sewing part, but I hope you are only jesting... Of course you do not need to sew it to anything :slight_smile:

There is another one, http://www.robotshop.com/productinfo.aspx?pc=RB-Phi-13&lang=en-US

That measures in Lux... Very handy. But if you just need to know whether it's day time then ya, just use a photoresistor.

A red led with resistor connected to a digital pin and ground can be used as a visible light sensor.

Straight from the Playground:
http://www.arduino.cc/playground/Learning/LEDSensor

I know it's an old thread, nonetheless...

noobtoarduino:
...
#2. I'm using the PIR to detect when I walk into my room. If I scan every 5 minutes..or even every 10 seconds...there's too much of a lag that my room won't light up as soon as I walk into it. I'd like my light to go on the instant I walk into my room.

I'd suggest you let Arduino scan the light every 5min (or another reasonable interval) and store this status in memory, ready to be taken into account as soon as you enter the room and trigger the PIR sensor. Now Arduino already knows if it's dark or bright (because it reads the stored status and doesn't need to check in real time) and choose what to do accordingly.

Just my 2cents :slight_smile:

It could check the PIR 100x a second and still have time to go into sleep mode. Once a person is detected coming in the light level can be scanned and kept scanned also 100x a second, you never know when a cloud might block the sun for seconds to hours but why bother if no one is there when you can tell and react in under 200 usecs?

Since the thread I found that with just a led between ground and an analog pin I can read light intensity.

However, PIR isn't foolproof and two people going in may register as one. Then when one leaves, the lights go out?