Sketch Coding For PIR Sensor For Detection Of Motion In Arduino Software
Ok, so we are in step-3 here we added our motion detector (pir sensor) now i would like to see what kind of new lines of code we added into the sketch. So, the first one is a new global variable declaration that will help us to know. Where the pir sensor is connected ? It is connected to pin # 12 in our case, and a new variable that is called "pirstate" that will help us to update. The pir sensor state all the time, we will see how we are going to use that in the sketch itself.
Now i will scroll down, looking on the setup() function you will see that i added some delay, that will help the pir sensor to learn the environment. When you are turning up the system with a pir sensor,. You need to let the sensor to learn the environment. You just need to apply some delay time, i already played with that a little bit so i know that in that case around 10 seconds is enough, and when it finished, looking on the serial monitor screen you will see that the system is ready, and of course it will be able to detect a motion
Now i will scroll down, down to the the loop function, i just added a new function that is called "checkpir ()", and now we will see that new function:
"checkpir" that's the new function, the logic is simple. So we are reading, using the "digitalread" function the status of the pir sensor. In case it is equal to high, meaning the pir sensor sense some motion and it will raise the signal level and in that case it will first turn on all the leds, and we will check the "pirstate" value.
When the sensor is detecting some motion it will keep the signal high for some period, something you can adjust manually using a potentiometer on the pir module, and decide how long the signal will be high.
Now during this time you don't want switch the leds on and off all the time. So this is why we are using the "pirstate". And if that's the first time that a motion is detected, meaning the previous on the pir state is equal to low. Then i am printing to the screen "motion detected!", and change that to high
If i will scroll down, under the "else" option, then of course when the digitalread is returning low then in that case i will turn off all the leds, and if the pir state was high, meaning going down from high to low, meaning the motion is ended, and we will print that on the screen and change the "pirstate" back to low.
That's all, that is the code we need to use while using a pir sensor, another thing i would like to show you is the serial monitor screen so i will run that
I already upload the sketch into the board and now i am getting some message that let me know the system is initializing and i am getting the message "system is ready"
Now, if i will pass my hand near the pir sensor, i will get a message "motion detected!" and after some period i will get "motion ended!"
I will do that again, motion detected, motion ended, very simple, now we will see that on top of the board itself.
Dear visitor. Click on "Follow" button from right side above and sign in with your Gmail account. Write unlimited comments on every post. 100% Free