Now that you have added the Pir sensor to your circuit and you have pulled on your Raspberry Pi, then
you can connect back to VNC or whatever you use to get access to the Raspberry Pi.
And let's write some Python code to detect a movement with this sensor.
The first thing to note is that this sensor.
So after it's powered on, it needs about one minute to be initialized.
So try not to run anything in the first minute.
And actually it doesn't mean the first minute after you open here, for example, Sony, but the first
minute after you just power on the Raspberry Pi.
So by the time that the Raspberry Pi is powered on, that it connects to the Wi-Fi and that you connect
to it, it's probably almost a minute.
So it should be fine.
But that's just one thing to keep in mind.
So how to control the Pir sensor.
Well we are going to use the Gpio zero library.
So I'm going to do from Gpio zero.
And we have seen already LED and button.
And there is something else.
So there is another functionality for this sensor and it's called motion sensor.
Like this.
Okay.
With M and S uppercase.
So this is the functionality for the Pir sensor.
And then to initialize it.
So let's create a variable.
Let's name it Pir for example is equal to motion sensor.
So it's the same thing as we did for the push button for example.
And we need to give the Gpio number.
So Gpio number is going to be four.
And what this is going to do.
Well it's going to initialize the Gpio four as an input pin okay.
Because we want to read data from the Pir sensor.
So now this is an input pin.
And then basically what we want to know is quite simple.
We want to know if there is a movement or if there is not a movement.
So we have a simple attribute we can do Pir dot motion detected.
And here there is no parentheses okay.
It's kind of a variable inside the Pir.
So this is going to tell us if a motion is detected.
So I can print that, for example.
And actually it's going to be better if we can print it continuously and then see what's happening.
So instead of just printing it once and then having to run the program again.
So I'm going to do the same thing we did for the push button previously.
So it's not new.
So I'm going to go quite fast.
I can do infinite loop and I just print the value here which is going to be true or false.
And also because what it's going to go very fast, I'm going to add import time.
And I'm going to do a time.sleep with, well for example 0.01 second.
So we run this 100 times per second okay.
That's going to be very fast.
We don't need more than that.
And it's going to save a lot of CPU.
And with this well with this that's it.
We can run this code.
And we're going to see if the movement is detected or not.
And here's the secret.
So you see, I have put my sensor here on one side so that we can detect movement on that side here.
So when I try to detect a movement I'm going to move my hand here.
One thing to note is that I have a lot of light coming here.
So if you have too much light it could influence the result and you might have some false positive.
So try to put this sensor not too close to a light.
I'm using a lot of light here for well to grab and to record this setting, but just know that it can
have an impact.
So let's run this.
And you see it's going to be false.
So no motion is detected.
Now I'm going to put my hand like that.
And you see it's true I remove my hand.
It's still true for some seconds okay.
That's what I told you.
There is a delay.
So when a movement is detected, the sensor is going to output high.
So here we're going to read motion detected for a few more seconds okay.
So that's the potentiometer we have set.
And then one other things I want to show you is that.
So I'm going to put my hand, I remove my hand and then I put my hand again.
And you see that there is a delay.
So after we go back to false, after the motion is not detected anymore, there is a delay so that the
sensor is going to read a positive value again.
Okay.
And this delay is about 2.5 seconds.
So very basically if it detects a movement okay it's gonna you're gonna get true here.
Then if it goes back to false.
So there is no movement detected.
After some seconds it's gonna go back to false.
But before it gets back to true again it needs to wait 2.5 seconds minimum.
Okay.
So there is a second delay between two movements that can be detected.
And finally, the last thing I want to show you is if I move here and I continue to move okay.
Well we have a we have a problem with the reading here, but let's say okay, I move, I continue to
move and you see that it's still true.
And then I remove my hand.
So as long as I keep doing movement in front of the Pir sensor, it's going to give us true here in
the code.
And this is because of the jumper that we set in the high position, which means that as long as we
get some movement detected, the Pir sensor is going to continue to output high.
Okay.
For the delay.
So for the 3 to 5 seconds minimum that I told you so for 3 to 5 seconds is going to basically reset
the delay every time that the movement is detected.
If you were to use the low position, then after those 3 to 5 seconds, then the reading will directly
go back to false, even if you keep doing movement.
Okay.
So that's that's the difference.
But basically you can see we have lots of different delays.
So just try to experiment on your own and see what it does.
So if the data you get is completely unreliable, well make sure you check your circuit again.
So you might need to power off.
So you might check the circuit or the potentiometer or the jumper.
But if you followed carefully the instructions it should be okay.
All right so to recap here you need to import the motion sensor from Gpio zero.
Then you initialize the motion sensor.
You can create a variable.
And then with motion detected it's going to give you true or false.
And if you put your so if you do some movement in front of the sensor it's going to give you true.
And then depending on the potentiometer that you set for the delay is going to return true for a certain
amount of time.
After this amount of time, if there is still movement, then as long as there is movement, it's going
to reset.
Basically this delay and after this goes false, so after no motion is detected for some amount of seconds,
then it's going to go back to false.
And before it can go back to true, there is a delay of about 2.5 seconds that you also need to be aware
of.
Okay, so you are not surprised about the behavior of the Pi sensor.