Go Down

Topic: What is the best way for Arduino to determine if it's daylight? (Read 4449 times) previous topic - next topic

noobtoarduino

Hello Forum!

I recently completed a basic PIR tutorial like this (http://www.ladyada.net/learn/sensors/pir.html)  and everything works great!

It works great during the night and day.

That's the problem though.  I just realized that I don't need the light to go on during the day as it's just wasting electricity.  What kind of sensor do I need to tell the Arduino to run my PIR program ONLY when it is dark in the room (because it's nighttime or there are no lamps in the room already on)?

THANKS FOR YOUR HELP!

My code is below:
Code: [Select]

/* 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;
   }
}
}

PaulS

Quote
What kind of sensor do I need to tell the Arduino to run my PIR program ONLY when it is dark in the room (because it's nighttime or there are no lamps in the room already on)?

There are a number of light sensors available. The speed that the device reacts to changes in light level is the most important factor in choosing the proper one.

An inexpensive LDR (light dependent resistor) is probably what you need. It will be slow to react, but does it really matter if the sensor takes 50 milliseconds to react?

Hideout

A LDR?

Use the LDR in a voltage divider on one of the analog inputs, when the analog value exceeds a threshold you let your PIR sensor control the light.

Make sure it cannot see the light you turn on with the Arduino 8)

BTW: Many PIR sensors have a built-in light sensor as well, are you sure your's doesn't have such feature?

noobtoarduino

Hi PaulS and Hideout!

Thanks for the guidance.  PaulS, you're right..speed is not an issue for me.  I just need to know if it's day or night / light or dark. 

I searched on Sparkfun for an LDR but they don't seem to carry any!  DO you have any preference for which LDR I should get and which website I should mail-order it from?  I'm happy to send some of my dollars their way.


noobtoarduino

Hideout,

What?  I didn't know that.  How can I tell if it has a built-in light sensor?  This is the exact PIR that I bought from CuteDigi:

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

noobtoarduino

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


noobtoarduino

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

FlyingSteve

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

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!

MarkT

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.
[ I won't respond to messages, use the forum please ]

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?

liudr


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.
Arduino Mega (ATMega1280) & ATTiny85 user

Go Up