And we are back to our Raspberry Pi os with Tony.
So make sure that you wait enough time so that the Raspberry Pi is connected again to the Wi-Fi network
if needed.
It can happen sometimes that the Raspberry Pi never connects well, you just remove the power cable
and you put it again.
But make sure that you wait at least 2 or 3 minutes before you do this.
And then, well, we are connected back to the Pi, and we can use Python to detect when the button
is pressed or not pressed.
So how are we going to do this.
Well we are going to also use the Gpio zero.
So we're going to do from Gpio zero and then import.
So instead of LED this time we have button.
So button with only the be uppercase.
Then I'm also going to import time because we're going to use it a bit later.
All right.
And we have everything correctly imported.
Now how do we initialize the button.
It's going to be very similar at what we did with the LED.
You're going to create a variable here.
Let's name it button for example, is equal to and you're going to use the same name here.
So button with be uppercase.
You open and close the parentheses and you need to give the Gpio number for that pin.
And the Gpio we use to read the pushbutton value is the Gpio number 26 as you can see on the image.
So what this line is going to do, it's going to initialize the Gpio 26 as an input pin for the LED
was output.
Here it's input.
So then we can read the value from the pushbutton.
And how to read the value from the pushbutton.
Well we have inside button we can do button.
Dot is pressed.
And note here that we don't have any parentheses.
It's just is pressed like that.
This is going to return true if the button is pressed, and false if the button is not pressed, which
is basically the two states of the.
But so what to do with this?
We can put it inside a variable and we can print this variable.
We can also directly print it.
That's what we're going to do just to test.
So let's run that and let's see what we have.
And well you can see we have false.
So as I told you this is going to return true or false.
And because the button is not pressed then we have false.
And now what I will do is I will press the button okay.
So the button is pressed.
And at the same time I run the program.
And you see now it is true.
Okay, I run again.
True.
Then I release and then it's false again.
So you can see for a push button is very easy.
You just initialize the push button and then you do button.
Dot is priced and you just know if the button is pressed or not.
Just to improve that, let's say we want to keep reading the state of the button and print the state.
For example, every one second we can do something similar as what we did with the LED.
We can do a while.
True.
Okay.
So let's create an infinite loop.
And we are going to print the button in this loop.
And of course we don't want to print it at an infinite speed.
So it's not infinite.
It's basically the the speed of the Raspberry Pi of the CPU.
But we want to print it every one second, so time.sleep with one.
Okay.
So with this we initialize the button and then we just print its state every second.
So I'm going to run this program and you can see it is false.
Okay.
And now so it's still false I will press you see I press on the button.
It's true I release.
It's false I press again, it is true I release it's false etc. etc..
Great.
So in this section you have seen how to control an LED and a push button with Python.
We are going to practice more with this and see how to combine a push button, an LED and then even
more components.