Hello!
I am a beginner of Arduino. I have started my project given by my college.
I am making automatic room light control system which does the light on if someone enters the room and turn off the light when everyone left the room.
I have programmed for this problem but my bulb is keep turn on even after no one is in the room . Help!
const int pirPin = 8;
const int relay =4;
const int LDRPin = A0; // defining LDR pin A0
int LDRvalue = 0;
void setup() {
Serial.begin(9600);
pinMode (relay, OUTPUT); //set pin to realy as an output
digitalWrite(relay, LOW);
}
void loop() {
while(1)//while motion detected
{
LDRvalue = analogRead(LDRPin);
Serial.print(LDRvalue);
if(digitalRead(pirPin) ==HIGH && LDRvalue <=400)
{
Serial.println("Motion Detected");
digitalWrite(relay, HIGH);
delay(300000); // wait for 5 min
}
else
{
digitalWrite(relay, LOW);
Serial.println("Motion does not detected");
delay(1);
}
}
}
First of all, go to the IDE and press Ctrl+T to autoformat the code. Looks better doesn't it?
Second, you don't tell us how you determine whether people are in or not. I assume a PIR, but how is it wired? What is the LDR doing you didn't tell us about?
Third:
while (1) //while motion detected
Really? Does it? Aka, get rid of it. The loop() already loops.
And fourth a tip:
delay() blocks EVERYTHING. Fine if you really don't want to do anything else but this will make the program impossible to extend for example if you want to turn off the light an other way.
It seems you're trying to do what every run-of-the-mill motion detecting lamp does (switch on when there's motion detected and when the ambient light level indicates it's night time), with the difference that you throw an Arduino in the mix.
Motion alone is not enough to detect if someone is in the room. Or you never had the toilet light switch off while you busy taking care of some big business?
Do you have an external pullup resistor to 5v on the PIR pin? You have not set a pinMode for that pin so it will default to INPUT, and will be floating if you don't have an external pullup.
You can also use the internal pullup with
pinMode (pirPin, INPUT_PULLUP)
What are your LDR readings and why do need an LDR?
Maybe your logic is reversed? Are you sure HIGH to the relay turns-on the light?
Did you try it without the LDR? Did you try it without the delay? And of course, use a shorter time delay for debugging. Work on one thing at a time!
I do like the fact that you're using Serial.print to "watch" what your program is doing!
Once you get this thing working I have a suggestion -
Take out the delay() and use a millis() timer.
The logic is -
Start the timer when motion is detected, and keep resetting the timer every time motion is detected... Don't wait for the time to expire. That way, the light won't go off until 5 minutes after the last motion was detected. (And, you might want to make the time delay shorter?)
I have to turn on the light when someone enters the room and Olson on the basis of intensity of the natural light therefore I have used LDR
What has Olson to do with this?
But can you give a complete description of what you want? Aka, including LDR etc.
And indeed note, "motion detection" and "people detection" have a large portion in common but are not the same.
I have to turn on the light of the motion detected on the room and the natural light is not available.if natural light is available then light should not be turn on. Therefore to check whether the natural light is available or not I have used LDR and to detect motion I have used PIR sensor.
Alright
Now:
a) Did you already make changes to the code as suggested?
b) Did you check the PIR? If so, how? (test program, DMM etc etc)
c) Did you test the LDR? Simplest to do to just make a test program for the LDR
(1.)Yes I have changed the code as suggested. But it is not working.
(2.) No
(3.) No
I'll check. Thanks.!