WEBVTT

00:00.080 --> 00:03.950
And we are back to our Raspberry Pi os with Tony.

00:04.070 --> 00:09.890
So make sure that you wait enough time so that the Raspberry Pi is connected again to the Wi-Fi network

00:09.890 --> 00:10.580
if needed.

00:10.580 --> 00:15.020
It can happen sometimes that the Raspberry Pi never connects well, you just remove the power cable

00:15.020 --> 00:16.130
and you put it again.

00:16.130 --> 00:19.190
But make sure that you wait at least 2 or 3 minutes before you do this.

00:19.190 --> 00:26.090
And then, well, we are connected back to the Pi, and we can use Python to detect when the button

00:26.090 --> 00:28.430
is pressed or not pressed.

00:28.430 --> 00:30.050
So how are we going to do this.

00:30.050 --> 00:33.980
Well we are going to also use the Gpio zero.

00:33.980 --> 00:38.570
So we're going to do from Gpio zero and then import.

00:38.600 --> 00:41.540
So instead of LED this time we have button.

00:41.540 --> 00:44.990
So button with only the be uppercase.

00:44.990 --> 00:49.010
Then I'm also going to import time because we're going to use it a bit later.

00:49.040 --> 00:49.490
All right.

00:49.490 --> 00:52.520
And we have everything correctly imported.

00:52.520 --> 00:54.890
Now how do we initialize the button.

00:54.890 --> 00:57.950
It's going to be very similar at what we did with the LED.

00:58.160 --> 00:59.780
You're going to create a variable here.

00:59.780 --> 01:05.330
Let's name it button for example, is equal to and you're going to use the same name here.

01:05.330 --> 01:08.570
So button with be uppercase.

01:09.320 --> 01:14.870
You open and close the parentheses and you need to give the Gpio number for that pin.

01:14.870 --> 01:23.510
And the Gpio we use to read the pushbutton value is the Gpio number 26 as you can see on the image.

01:23.510 --> 01:31.610
So what this line is going to do, it's going to initialize the Gpio 26 as an input pin for the LED

01:31.640 --> 01:32.690
was output.

01:32.720 --> 01:34.520
Here it's input.

01:34.520 --> 01:38.300
So then we can read the value from the pushbutton.

01:38.300 --> 01:41.180
And how to read the value from the pushbutton.

01:41.180 --> 01:44.090
Well we have inside button we can do button.

01:44.120 --> 01:47.090
Dot is pressed.

01:47.270 --> 01:50.960
And note here that we don't have any parentheses.

01:51.200 --> 01:53.060
It's just is pressed like that.

01:53.060 --> 02:00.320
This is going to return true if the button is pressed, and false if the button is not pressed, which

02:00.320 --> 02:02.240
is basically the two states of the.

02:02.270 --> 02:03.500
But so what to do with this?

02:03.530 --> 02:07.220
We can put it inside a variable and we can print this variable.

02:07.220 --> 02:08.840
We can also directly print it.

02:08.840 --> 02:11.540
That's what we're going to do just to test.

02:11.540 --> 02:14.540
So let's run that and let's see what we have.

02:14.840 --> 02:17.030
And well you can see we have false.

02:17.030 --> 02:19.940
So as I told you this is going to return true or false.

02:19.940 --> 02:23.300
And because the button is not pressed then we have false.

02:23.300 --> 02:26.660
And now what I will do is I will press the button okay.

02:26.690 --> 02:27.560
So the button is pressed.

02:27.560 --> 02:29.660
And at the same time I run the program.

02:30.110 --> 02:31.730
And you see now it is true.

02:31.760 --> 02:32.960
Okay, I run again.

02:32.990 --> 02:33.620
True.

02:33.650 --> 02:38.270
Then I release and then it's false again.

02:38.630 --> 02:41.330
So you can see for a push button is very easy.

02:41.330 --> 02:44.090
You just initialize the push button and then you do button.

02:44.120 --> 02:47.420
Dot is priced and you just know if the button is pressed or not.

02:47.450 --> 02:53.780
Just to improve that, let's say we want to keep reading the state of the button and print the state.

02:53.780 --> 02:58.910
For example, every one second we can do something similar as what we did with the LED.

02:58.940 --> 03:00.320
We can do a while.

03:00.350 --> 03:01.340
True.

03:01.610 --> 03:01.940
Okay.

03:01.940 --> 03:03.770
So let's create an infinite loop.

03:03.770 --> 03:07.610
And we are going to print the button in this loop.

03:07.610 --> 03:12.710
And of course we don't want to print it at an infinite speed.

03:12.710 --> 03:13.580
So it's not infinite.

03:13.610 --> 03:16.550
It's basically the the speed of the Raspberry Pi of the CPU.

03:16.550 --> 03:22.700
But we want to print it every one second, so time.sleep with one.

03:22.910 --> 03:23.060
Okay.

03:23.090 --> 03:28.040
So with this we initialize the button and then we just print its state every second.

03:28.040 --> 03:32.870
So I'm going to run this program and you can see it is false.

03:35.240 --> 03:35.750
Okay.

03:35.750 --> 03:40.460
And now so it's still false I will press you see I press on the button.

03:40.460 --> 03:42.290
It's true I release.

03:42.440 --> 03:49.610
It's false I press again, it is true I release it's false etc. etc..

03:49.970 --> 03:50.420
Great.

03:50.420 --> 03:55.700
So in this section you have seen how to control an LED and a push button with Python.

03:55.700 --> 04:01.310
We are going to practice more with this and see how to combine a push button, an LED and then even

04:01.310 --> 04:02.660
more components.
