PIR sensor |
When I was working on a project "controlling home appliances using just a wave of hand" I had used a PIR sensor and found some issues:
- PIR doesn't work properly when starts.
- PIR output value is always 1.
- It some times doesn't read motions.
I assume you know what a PIR sensor is and I'll just try to give a solution to the problems so that you can use PIR in your project efficiently, also I've added a code below on how to solve that problem.
As I have mentioned earlier those are Common and Unavoidable,
keep 3 issues in mind:
- After powering - PIR sensor needs 1 minute to function
- For that 1 minute the OUTPUT is always HIGH.
- When a motion is detected it'll take 5 to 7 seconds to detect motion again.
SOLUTION:
After powering - PIR sensor needs 1 minute to function:
which means when you'll power a PIR sensor it will need to be warm to get functional or in simple words, to work smoothly. That's why when the OUTPUT value is always 1 for 1 minute after a restart. But how would you actually make a machine work then? Here's what I did. I have made a 1 minute delay in my code before it executes the actual code. Then I made a function and using a goto loop I made that part repeatable.
Look at this following Code:
void main()
{
delay(60000); //wait for the PIR to become worm-to dunction
goto R; //going to R function
R: //the run
{
your main code here; //your main code: say motion sensor code
goto R; //go again to R:means this section runs repeatedly
}
}
That's the solution for our problem 1 and 2.
When a motion is detected it'll take 5 to 7 seconds to detect motion again:
It's pretty simple, you have made a motion sensor using PIR sensor and after a motion detecting it will not work properly for 5 or 6 second. To solve this we need either to give the feedback after 5 seconds or we can just make feedback and then wait for 6 seconds, the second one is good.
Have a look at this how I wrote a code to solve this:
Algorithm:
- start
- wait 1 minute (60*1000 miliseconds)
- goto R
- if PIR high - feedback and wait for 5000 mili seconds (5 seconds)
- go to 3
Sample Code
void main()
{
delay(60000); //wait for the PIR to become worm-to dunction
goto R;
R: //your main code: say motion sensor code
{
your main code here;
if(digitalRead(pir,HIGH); //If PIR output pin is HIGH
{
make noice/make sound/feedback; //main code: light on off,alarm etc
delay 5500; //wait for 5.5 seconds
goto R; //again go to run-make everything work agin
}
else
{
go to R;
} }
}
Now, use this Algorithm or pattern in your code and run your projects smoothly.
Let me know if you face any problems. Thank you.
I've been working on this project to control lights by just waving my hand on the sesnor. I'll make another post when it'll be done. Till then stay tuned. Thank you.
Thank you for posting this excellent information..it is very useful to me..!
ReplyDeleteClick here:
PIR Sensor