Go Down

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

#### Dr_Ugi

#15
##### Dec 06, 2011, 04:10 pmLast Edit: Dec 06, 2011, 04:16 pm by Dr_Ugi Reason: 1

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.

#### noobtoarduino

#16
##### Dec 06, 2011, 04:17 pm
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!

#### noobtoarduino

#17
##### Dec 06, 2011, 04:21 pm
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.

#### noobtoarduino

#18
##### Dec 06, 2011, 04:26 pm
Dr Ugi,

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

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.

#### Flame Soulis

#19
##### Dec 06, 2011, 04:35 pm
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
Arduino Mega (ATMega1280) & ATTiny85 user

#### noobtoarduino

#20
##### Dec 06, 2011, 04:41 pm
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: [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(){

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

#### FlyingSteve

#21
##### Dec 06, 2011, 10:03 pm

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

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.

#### GoForSmoke

#22
##### Dec 06, 2011, 10:20 pm
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

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### robitabu

#23
##### Feb 25, 2012, 08:00 am
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 :-)

#### GoForSmoke

#24
##### Feb 25, 2012, 10:26 am
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?

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up

Please enter a valid email to subscribe