WEBVTT

00:00.050 --> 00:04.430
This is the solution for the activity eight, where you have to turn on an LED.

00:04.430 --> 00:09.530
If movement is detected and you have to turn it off if there is no movement.

00:09.530 --> 00:12.590
So first of all we are going to import.

00:12.590 --> 00:16.010
So from Gpio zero we're going to import everything we need.

00:16.070 --> 00:18.440
So imports we need the LED.

00:18.560 --> 00:19.460
And then dot.

00:19.490 --> 00:22.070
We need the motion sensor.

00:23.030 --> 00:24.830
All right then what do we need to do.

00:24.830 --> 00:26.540
We need to initialize them.

00:26.540 --> 00:31.460
So LED is equal to LED like this.

00:31.490 --> 00:33.260
And we're going to use the first one.

00:33.260 --> 00:35.450
So should be Gpio 17.

00:35.450 --> 00:36.920
You can use any LED you want.

00:36.950 --> 00:38.900
You can even use several LEDs okay.

00:38.900 --> 00:39.530
It doesn't matter.

00:39.530 --> 00:46.790
And then the peer let's create peer variable with motion sensor Gpio number four.

00:46.820 --> 00:47.360
Great.

00:47.360 --> 00:49.640
And now I'm going to do the solution in two steps.

00:49.640 --> 00:51.860
So that's going to be the same result for both.

00:51.890 --> 00:54.080
It's just different ways to write the code.

00:54.230 --> 00:56.420
And let's start with a while loop.

00:56.420 --> 00:59.600
So what I can do is while true okay.

00:59.600 --> 01:05.450
So we want to do what we want to turn on the LED or turn off depending on if there is a movement, but

01:05.450 --> 01:09.050
we just want to do that as was as long as we run the program.

01:09.080 --> 01:09.290
Okay.

01:09.320 --> 01:10.910
So let's use an infinite loop.

01:10.910 --> 01:14.330
And if I use an infinite loop I'm going to also import time.

01:14.690 --> 01:24.650
And then I'm just going to add a small delay time dot sleep 0.01 just to slow down this loop.

01:24.680 --> 01:26.780
Otherwise it's going to take the full CPU.

01:26.780 --> 01:32.870
And then it's quite simple if pi r dot motion detected.

01:33.920 --> 01:34.040
Okay.

01:34.040 --> 01:35.960
So before we just printed that.

01:35.960 --> 01:40.190
But now we can just do if because you know it's a it's going to give true or false.

01:40.190 --> 01:41.990
So if this is true what do we do.

01:41.990 --> 01:44.630
We do LED dot on.

01:45.110 --> 01:47.450
And then if it's not true it's going to be false.

01:47.450 --> 01:53.060
So we'll just go to the else LED dot off and that's it.

01:53.060 --> 01:55.850
So now I can run the program.

01:55.850 --> 01:57.950
So this time we don't print anything.

01:57.950 --> 02:03.050
But then you see if I put my hand here the LED here is powered on.

02:03.080 --> 02:03.320
Okay.

02:03.350 --> 02:04.760
Put my hand again.

02:05.660 --> 02:06.140
All right.

02:06.140 --> 02:09.210
You see on I keep doing movement.

02:09.330 --> 02:11.400
Maybe I'm a bit too close.

02:11.610 --> 02:11.790
Okay.

02:11.820 --> 02:13.650
That's why we have some issues.

02:13.650 --> 02:15.030
But if I move like this.

02:15.060 --> 02:15.330
Okay.

02:15.360 --> 02:16.290
It's on.

02:17.280 --> 02:18.090
All right.

02:18.090 --> 02:21.930
And then I remove my hand is going to turn off.

02:21.930 --> 02:22.950
And then I put my hand again.

02:22.980 --> 02:25.590
You see there's a small delay before it can go on again.

02:26.280 --> 02:29.490
I remove my hand and it's off.

02:29.580 --> 02:30.210
Great.

02:30.210 --> 02:31.350
So it's correctly working.

02:31.380 --> 02:35.640
And so, as I told you, there are several things that could affect the behavior of the Pir sensor.

02:35.640 --> 02:39.150
For example, if there is too much light, like in my case, I have a lot of light.

02:39.150 --> 02:44.520
And then maybe if you are too close to the Pir sensor, it's better to be a bit more distanced anyway.

02:44.520 --> 02:51.570
It can detect movement up to 3 to 7m depending on one of the potentiometers, so you can do the movement

02:51.600 --> 02:55.080
a bit more far and the result should be a bit more precise.

02:55.080 --> 02:56.670
So that's it for this activity.

02:56.670 --> 03:01.980
But actually we are going to improve the code with what I gave you in the previous video.

03:01.980 --> 03:10.020
So I'm going to actually I'm going to comment that and I'm going to comment import time because we don't

03:10.020 --> 03:11.940
need this as well.

03:12.480 --> 03:12.840
Okay.

03:12.870 --> 03:23.370
And what I will do is I will say that after we initialize the component, I do PR dot when motion is

03:23.370 --> 03:29.790
equal to and I need to give the name of a function that we need to call when motion is detected.

03:29.790 --> 03:32.010
And this function we already have it here.

03:32.010 --> 03:33.690
It's led dot en.

03:33.690 --> 03:35.790
So I put led dot en.

03:35.790 --> 03:42.120
But note and I have already explained that before we don't put any parentheses here because we don't

03:42.120 --> 03:49.230
call the function, we just register the function to be called back again in the future when a motion

03:49.230 --> 03:50.130
is detected.

03:50.160 --> 03:50.400
Okay.

03:50.430 --> 03:51.840
So that's a very different thing.

03:51.840 --> 03:57.600
If I come back to a previous analogy I did, it's like if you give your phone number to somebody that

03:57.600 --> 04:02.310
they call you back when a motion is detected, then you give them, well, your phone number.

04:02.310 --> 04:03.600
They don't call you directly.

04:03.600 --> 04:04.980
They will call you later.

04:05.010 --> 04:05.190
Okay.

04:05.190 --> 04:07.590
So that's what we do here.

04:07.680 --> 04:17.860
And then per dot when no Motion because, well, we want to turn on the LED if there is a motion, but

04:17.860 --> 04:21.040
if there is no motion, we also want to turn it off.

04:21.040 --> 04:24.010
So led off without any parentheses.

04:24.190 --> 04:25.030
And this is nice.

04:25.030 --> 04:30.490
But if we just run the program like this, well, this is doing nothing in its own.

04:30.490 --> 04:35.080
It's just registering a callback, but it's not going to pause the program or anything.

04:35.080 --> 04:37.000
So we need to pause the program.

04:37.000 --> 04:41.290
And I'm going to just do from signal import pause.

04:42.160 --> 04:45.580
And I'm going to run pause here.

04:46.180 --> 04:51.550
And here I use the parentheses because I'm actually calling the function okay.

04:51.550 --> 04:53.800
So here it's registering a callback.

04:53.800 --> 04:55.270
Here we call a function.

04:55.270 --> 04:57.910
And you can see this is it okay.

04:57.910 --> 05:01.150
So the program is smaller and well you can run the program.

05:01.150 --> 05:02.950
It's going to do the exact same thing.

05:02.950 --> 05:06.100
There's almost no difference between those two parts of code.

05:06.130 --> 05:06.520
Great.

05:06.550 --> 05:08.200
This activity is now finished.

05:08.200 --> 05:15.220
And that's also the end of this section on the Pir sensor that we will use again for the final project

05:15.220 --> 05:16.300
of this course.
