I have recently put together an auto ON, lights system for my garage, using an Arduino UNO R3 + an LDR + a 5 volt, relay controlled by the UNO, bringing on the 240 lighting when low light levels are detected by the LDR.
To prevent the lights from coming on every time of darkness, without need, i have put a magnetic door switch in the circuit so that the relay can only go HIGH, when the door opens. This works great until the lights come on, then the LDR thinks it's daytime and immediately switches the relay LOW turn to off the lights, off again and so on.
Is it best to put the LDR outside the garage away from a light source, or can i write into the sketch something that stops the relay continually going ON then OFF, something like a run down timer?
Just think the thing through on paper before you start programming e.g.
Door opens
Is it dark - if yes turn on light
That is it.
You also need to decide how and when you turn the light off, you could do it on a timer so many minutes after it turned on or by using a motion detector, or with a manual switch.
Grumpy_Mike:
Or by turning off the light when the door closes.
Maybe. You would want to turn the light off once the door closes after you have left, but if you have just arrived you would probably want at least a delay.
It just shows how even what appears a simple task really needs a bit of analysis before people start buying sensors and jumping into coding.
You would want to turn the light off once the door closes after you have left
That all depends on the layout of the garage and house.
If it is a stand alone garage, you do not want to close the door with you still inside. If it is integrated into a house then turn the light off when you open and then close the door connecting the garage with the rest of the house.
It just shows how even what appears a simple task really needs a bit of analysis before people start buying sensors and jumping into coding.
Many thanks for all your replies, i will just clarify a few questions.
"Hammy" The relay is activated by Pin 2 going HIGH, but i have sent this signal through the mag switch, so the lights cannot possibly come ON until the door opens, so car lights will not effect the operation.
"Grumpy_Mike, The garage is detached, if one is stuck inside the garage and the door closes, well i thought of this, i have installed a override switch next to the LDR.
"ardly", what do you mean by floor plans?
"Wildbill" - "store the time using millis" this seems promising, i take it, it needs adding to my sketch, if so can you point me to some lessons on how to do (save me hours on google)
gresleyman:
4) "Wildbill" - "store the time using millis" this seems promising, i take it, it needs adding to my sketch, if so can you point me to some lessons on how to do (save me hours on google)
No need for google, there are dozens of threads on this site about timing. Try a site search for 'timing with millis'.
As you can see i am new to Arduino + C++ programming can you help further, do i declare If as a variable or an Integer ? i enclose my sketch for your perusal if i may?
int sensorPin = A0; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(2, OUTPUT); //pin connected to the relay
Serial.begin(9600); //sets serial port for communication
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if(sensorValue < 800) //setting a threshold value
digitalWrite(2,HIGH); //turn relay ON
else digitalWrite(2,LOW); //turn relay OFF
if (area = dark) & (light != on) & (door = open)
{
turn on lights;
delay(how ever long you want them to stay on for);
turn off light;
}
delay(1000);
}
....
3) "ardly", what do you mean by floor plans?
....
There was a bit of discussion on the thread about how tasks that at first seem simple actually require quite a bit of thought. In this case how you operate a garage door and lights can vary quite a bit depending on how you can get in and out of the garage - hence the mention of floor plans.
@hammy also mentioned that the car lights might affect the LDR, which is a good point.
OK so I tried to modify the code (hopefully I didn't over complicate it more that needed)
If you attach the door sensor/switch to the arduino as an input the attached code should work.
/* Im not a coding expert so i apologize if ive made a mistake, but I hope this works for you.
* If everything goes right the light should come on when it is dark, and the door opens. the light should stay on for 100 seconds (you can change this) and then turn off
* I'm not sure how you have the switch setup right now, but if you connect it to the arduino as an input this code should work
*
* ****NOTE you will have to find the value from the switch and insert it into the if statment (the value should print in the serial window) but the value when the door is closed should be close to zero (probably)
*
* The phyisical set up for this code is as follows:
* The SENSOR is connected to PIN A0
* The door SWITCH is connected to PIN A1
* The RELAY is connected to PIN 2
*/
int sensorPin = A0; // select the input pin for ldr
int doorswitchPin = A1; // if you hook the reedswitch? up to the arduino you can control the program more
int sensorValue = 0;
int doorswitchValue = 0;
int relay = 2; //pin 2 is what the relay is hookup to?
int relayState;
void setup() {
pinMode(relay, OUTPUT); //pin connected to the relay
pinMode(doorswitchPin, INPUT); // pin connected to the switch on the door
Serial.begin(9600); //sets serial port for communication
}
void loop() {
sensorValue = analogRead(sensorPin); //reads of light sensor
doorswitchValue = analogRead(doorswitchPin); //reads for door switch (is the door open or closed?)
Serial.println("sensor value");// this will help differentiate the 2 variables that will be printed
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
Serial.println("doorswitchValue");// also to help differentiate values
Serial.println(doorswitchValue); // this will let you know what the high value and low value will be for the switch.
delay(10); // the way I understand, this should help smooth some of the variable reads.
relayState = digitalRead(relay);
if (sensorValue < 800 && relayState != HIGH && doorswitchValue > 100 ) // doorswitch value will depend in what the value will be. YOU MAY NEED TO CHANGE THIS depending on the values given when the door is open vs when it is closed
{
digitalWrite(relay,HIGH); //this should turn the lights on
delay(100000); //this value should keep the lights on for 100 seconds
digitalWrite(relay,LOW); //this shuts the lights off after the set time above
}
}
I would wire the MagDoor switch as another input to the UNO, that way you can program your code and edit it if you need to change how your project responds.
At the moment if you want a situation where it is dark and the door is open and no car, your lights do not come on, and any change of code will not make it work.
If you put the switch actuation as an input you can change code rather than change wiring.
Car headlamps are no problem to this project, as my garage is a double garage, with a double door.As such is to heavy for my wife to lift up.
We had an electric motor fitted to the doors some ten years ago, it has a lamp internally fitted to the Motor control, which is operated by remote control, therefore my wife does not need to get out of her car, she presses a button, the door raises, the light comes on automatically, and stays on for about 5 minutes, enough time for her to close the door, get out of the car and exit through the side door, which is the door i want to put the Arduino on.
I have tried your sketch, and it does what i want it to do, but i now have a different problem.
When the Loop is running, with the door closed, the relay is energising for a split second at the end of the last 10000 Delay command, the serial monitor is steady at this point.
When i cover up the LDR to simulate darkness, and then when i open the door.(i used an on/off switch to simulate the door opening) the lights come on, i remove the LDR cover, so that the LDR can read the light value, i then simulate the door closing, the lights go out, but now the relay energises every split second, and the serial monitor is so fast i cannot read it.
I cannot see in the sketch what would cause this, but then i am only a beginner.