This is the solution for the activity eight, where you have to turn on an LED.
If movement is detected and you have to turn it off if there is no movement.
So first of all we are going to import.
So from Gpio zero we're going to import everything we need.
So imports we need the LED.
And then dot.
We need the motion sensor.
All right then what do we need to do.
We need to initialize them.
So LED is equal to LED like this.
And we're going to use the first one.
So should be Gpio 17.
You can use any LED you want.
You can even use several LEDs okay.
It doesn't matter.
And then the peer let's create peer variable with motion sensor Gpio number four.
Great.
And now I'm going to do the solution in two steps.
So that's going to be the same result for both.
It's just different ways to write the code.
And let's start with a while loop.
So what I can do is while true okay.
So we want to do what we want to turn on the LED or turn off depending on if there is a movement, but
we just want to do that as was as long as we run the program.
Okay.
So let's use an infinite loop.
And if I use an infinite loop I'm going to also import time.
And then I'm just going to add a small delay time dot sleep 0.01 just to slow down this loop.
Otherwise it's going to take the full CPU.
And then it's quite simple if pi r dot motion detected.
Okay.
So before we just printed that.
But now we can just do if because you know it's a it's going to give true or false.
So if this is true what do we do.
We do LED dot on.
And then if it's not true it's going to be false.
So we'll just go to the else LED dot off and that's it.
So now I can run the program.
So this time we don't print anything.
But then you see if I put my hand here the LED here is powered on.
Okay.
Put my hand again.
All right.
You see on I keep doing movement.
Maybe I'm a bit too close.
Okay.
That's why we have some issues.
But if I move like this.
Okay.
It's on.
All right.
And then I remove my hand is going to turn off.
And then I put my hand again.
You see there's a small delay before it can go on again.
I remove my hand and it's off.
Great.
So it's correctly working.
And so, as I told you, there are several things that could affect the behavior of the Pir sensor.
For example, if there is too much light, like in my case, I have a lot of light.
And then maybe if you are too close to the Pir sensor, it's better to be a bit more distanced anyway.
It can detect movement up to 3 to 7m depending on one of the potentiometers, so you can do the movement
a bit more far and the result should be a bit more precise.
So that's it for this activity.
But actually we are going to improve the code with what I gave you in the previous video.
So I'm going to actually I'm going to comment that and I'm going to comment import time because we don't
need this as well.
Okay.
And what I will do is I will say that after we initialize the component, I do PR dot when motion is
equal to and I need to give the name of a function that we need to call when motion is detected.
And this function we already have it here.
It's led dot en.
So I put led dot en.
But note and I have already explained that before we don't put any parentheses here because we don't
call the function, we just register the function to be called back again in the future when a motion
is detected.
Okay.
So that's a very different thing.
If I come back to a previous analogy I did, it's like if you give your phone number to somebody that
they call you back when a motion is detected, then you give them, well, your phone number.
They don't call you directly.
They will call you later.
Okay.
So that's what we do here.
And then per dot when no Motion because, well, we want to turn on the LED if there is a motion, but
if there is no motion, we also want to turn it off.
So led off without any parentheses.
And this is nice.
But if we just run the program like this, well, this is doing nothing in its own.
It's just registering a callback, but it's not going to pause the program or anything.
So we need to pause the program.
And I'm going to just do from signal import pause.
And I'm going to run pause here.
And here I use the parentheses because I'm actually calling the function okay.
So here it's registering a callback.
Here we call a function.
And you can see this is it okay.
So the program is smaller and well you can run the program.
It's going to do the exact same thing.
There's almost no difference between those two parts of code.
Great.
This activity is now finished.
And that's also the end of this section on the Pir sensor that we will use again for the final project
of this course.