Pages: 1 [2]   Go Down
Author Topic: What is the best way for Arduino to determine if it's daylight?  (Read 3702 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Ugi was not declared in this scope
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 06, 2011, 10:16:09 am by Dr_Ugi » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! 
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dr Ugi,

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

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!
 
Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Arduino Mega (ATMega1280) & ATTiny85 user

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Palermo
Offline Offline
Full Member
***
Karma: 0
Posts: 174
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know it's an old thread, nonetheless...
...
#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 :-)
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: 1 [2]   Go Up
Jump to: