WEBVTT

00:00.080 --> 00:06.260
Now that you have added the Pir sensor to your circuit and you have pulled on your Raspberry Pi, then

00:06.260 --> 00:10.550
you can connect back to VNC or whatever you use to get access to the Raspberry Pi.

00:10.550 --> 00:15.500
And let's write some Python code to detect a movement with this sensor.

00:15.530 --> 00:17.810
The first thing to note is that this sensor.

00:17.810 --> 00:22.610
So after it's powered on, it needs about one minute to be initialized.

00:22.610 --> 00:25.220
So try not to run anything in the first minute.

00:25.220 --> 00:30.350
And actually it doesn't mean the first minute after you open here, for example, Sony, but the first

00:30.350 --> 00:32.870
minute after you just power on the Raspberry Pi.

00:32.900 --> 00:37.730
So by the time that the Raspberry Pi is powered on, that it connects to the Wi-Fi and that you connect

00:37.730 --> 00:40.040
to it, it's probably almost a minute.

00:40.040 --> 00:40.880
So it should be fine.

00:40.880 --> 00:42.800
But that's just one thing to keep in mind.

00:42.800 --> 00:46.160
So how to control the Pir sensor.

00:46.190 --> 00:49.700
Well we are going to use the Gpio zero library.

00:49.700 --> 00:52.370
So I'm going to do from Gpio zero.

00:52.490 --> 00:55.550
And we have seen already LED and button.

00:55.550 --> 00:57.830
And there is something else.

00:57.830 --> 01:03.630
So there is another functionality for this sensor and it's called motion sensor.

01:03.660 --> 01:04.260
Like this.

01:04.290 --> 01:04.500
Okay.

01:04.530 --> 01:06.690
With M and S uppercase.

01:06.690 --> 01:09.750
So this is the functionality for the Pir sensor.

01:09.750 --> 01:11.130
And then to initialize it.

01:11.130 --> 01:12.450
So let's create a variable.

01:12.450 --> 01:18.030
Let's name it Pir for example is equal to motion sensor.

01:18.030 --> 01:21.090
So it's the same thing as we did for the push button for example.

01:21.090 --> 01:23.100
And we need to give the Gpio number.

01:23.100 --> 01:25.500
So Gpio number is going to be four.

01:25.500 --> 01:27.600
And what this is going to do.

01:27.600 --> 01:32.190
Well it's going to initialize the Gpio four as an input pin okay.

01:32.190 --> 01:35.490
Because we want to read data from the Pir sensor.

01:35.490 --> 01:37.440
So now this is an input pin.

01:37.440 --> 01:39.930
And then basically what we want to know is quite simple.

01:39.930 --> 01:43.350
We want to know if there is a movement or if there is not a movement.

01:43.350 --> 01:49.260
So we have a simple attribute we can do Pir dot motion detected.

01:49.770 --> 01:51.420
And here there is no parentheses okay.

01:51.420 --> 01:54.540
It's kind of a variable inside the Pir.

01:54.570 --> 01:58.260
So this is going to tell us if a motion is detected.

01:58.260 --> 02:00.440
So I can print that, for example.

02:02.270 --> 02:08.390
And actually it's going to be better if we can print it continuously and then see what's happening.

02:08.390 --> 02:11.900
So instead of just printing it once and then having to run the program again.

02:11.900 --> 02:15.200
So I'm going to do the same thing we did for the push button previously.

02:15.200 --> 02:16.370
So it's not new.

02:16.370 --> 02:17.930
So I'm going to go quite fast.

02:17.960 --> 02:24.290
I can do infinite loop and I just print the value here which is going to be true or false.

02:24.290 --> 02:30.590
And also because what it's going to go very fast, I'm going to add import time.

02:31.790 --> 02:40.100
And I'm going to do a time.sleep with, well for example 0.01 second.

02:40.100 --> 02:43.460
So we run this 100 times per second okay.

02:43.490 --> 02:44.840
That's going to be very fast.

02:44.840 --> 02:46.160
We don't need more than that.

02:46.160 --> 02:47.960
And it's going to save a lot of CPU.

02:47.960 --> 02:50.420
And with this well with this that's it.

02:50.450 --> 02:51.620
We can run this code.

02:51.620 --> 02:55.280
And we're going to see if the movement is detected or not.

02:55.370 --> 02:57.090
And here's the secret.

02:57.090 --> 03:02.700
So you see, I have put my sensor here on one side so that we can detect movement on that side here.

03:02.700 --> 03:07.110
So when I try to detect a movement I'm going to move my hand here.

03:07.140 --> 03:10.230
One thing to note is that I have a lot of light coming here.

03:10.230 --> 03:16.290
So if you have too much light it could influence the result and you might have some false positive.

03:16.290 --> 03:20.610
So try to put this sensor not too close to a light.

03:20.640 --> 03:27.090
I'm using a lot of light here for well to grab and to record this setting, but just know that it can

03:27.090 --> 03:28.260
have an impact.

03:28.260 --> 03:31.320
So let's run this.

03:31.320 --> 03:32.910
And you see it's going to be false.

03:32.910 --> 03:34.350
So no motion is detected.

03:34.350 --> 03:36.180
Now I'm going to put my hand like that.

03:36.180 --> 03:38.520
And you see it's true I remove my hand.

03:39.060 --> 03:41.040
It's still true for some seconds okay.

03:41.070 --> 03:41.970
That's what I told you.

03:42.000 --> 03:42.900
There is a delay.

03:42.900 --> 03:47.760
So when a movement is detected, the sensor is going to output high.

03:47.790 --> 03:53.220
So here we're going to read motion detected for a few more seconds okay.

03:53.250 --> 03:55.500
So that's the potentiometer we have set.

03:55.500 --> 03:58.220
And then one other things I want to show you is that.

03:58.220 --> 04:02.390
So I'm going to put my hand, I remove my hand and then I put my hand again.

04:02.690 --> 04:05.390
And you see that there is a delay.

04:05.390 --> 04:11.870
So after we go back to false, after the motion is not detected anymore, there is a delay so that the

04:11.870 --> 04:14.960
sensor is going to read a positive value again.

04:14.990 --> 04:15.200
Okay.

04:15.200 --> 04:17.780
And this delay is about 2.5 seconds.

04:17.780 --> 04:23.060
So very basically if it detects a movement okay it's gonna you're gonna get true here.

04:23.060 --> 04:25.550
Then if it goes back to false.

04:25.550 --> 04:26.960
So there is no movement detected.

04:26.960 --> 04:30.290
After some seconds it's gonna go back to false.

04:30.290 --> 04:35.750
But before it gets back to true again it needs to wait 2.5 seconds minimum.

04:35.780 --> 04:36.110
Okay.

04:36.140 --> 04:40.520
So there is a second delay between two movements that can be detected.

04:40.520 --> 04:46.310
And finally, the last thing I want to show you is if I move here and I continue to move okay.

04:46.340 --> 04:51.350
Well we have a we have a problem with the reading here, but let's say okay, I move, I continue to

04:51.380 --> 04:55.380
move and you see that it's still true.

04:55.380 --> 04:57.030
And then I remove my hand.

04:58.500 --> 05:05.460
So as long as I keep doing movement in front of the Pir sensor, it's going to give us true here in

05:05.460 --> 05:06.120
the code.

05:06.120 --> 05:13.230
And this is because of the jumper that we set in the high position, which means that as long as we

05:13.230 --> 05:18.630
get some movement detected, the Pir sensor is going to continue to output high.

05:18.660 --> 05:18.900
Okay.

05:18.930 --> 05:20.010
For the delay.

05:20.010 --> 05:26.460
So for the 3 to 5 seconds minimum that I told you so for 3 to 5 seconds is going to basically reset

05:26.460 --> 05:28.860
the delay every time that the movement is detected.

05:28.860 --> 05:34.890
If you were to use the low position, then after those 3 to 5 seconds, then the reading will directly

05:34.890 --> 05:38.100
go back to false, even if you keep doing movement.

05:38.130 --> 05:38.340
Okay.

05:38.370 --> 05:40.080
So that's that's the difference.

05:40.080 --> 05:43.320
But basically you can see we have lots of different delays.

05:43.320 --> 05:46.800
So just try to experiment on your own and see what it does.

05:46.800 --> 05:52.320
So if the data you get is completely unreliable, well make sure you check your circuit again.

05:52.320 --> 05:53.840
So you might need to power off.

05:53.840 --> 05:57.260
So you might check the circuit or the potentiometer or the jumper.

05:57.260 --> 06:00.770
But if you followed carefully the instructions it should be okay.

06:00.800 --> 06:06.020
All right so to recap here you need to import the motion sensor from Gpio zero.

06:06.050 --> 06:08.900
Then you initialize the motion sensor.

06:08.930 --> 06:10.370
You can create a variable.

06:10.370 --> 06:14.480
And then with motion detected it's going to give you true or false.

06:14.510 --> 06:19.670
And if you put your so if you do some movement in front of the sensor it's going to give you true.

06:19.670 --> 06:25.280
And then depending on the potentiometer that you set for the delay is going to return true for a certain

06:25.280 --> 06:26.180
amount of time.

06:26.180 --> 06:31.880
After this amount of time, if there is still movement, then as long as there is movement, it's going

06:31.910 --> 06:32.390
to reset.

06:32.390 --> 06:39.800
Basically this delay and after this goes false, so after no motion is detected for some amount of seconds,

06:39.800 --> 06:41.720
then it's going to go back to false.

06:41.720 --> 06:47.660
And before it can go back to true, there is a delay of about 2.5 seconds that you also need to be aware

06:47.660 --> 06:47.990
of.

06:48.020 --> 06:52.130
Okay, so you are not surprised about the behavior of the Pi sensor.
