WEBVTT

00:00.050 --> 00:05.570
Now that you have created your first circuit with an LED, let's control it with Python.

00:05.570 --> 00:11.270
If it's not done yet, you can power on your Raspberry Pi and just connect to it like you did before.

00:11.300 --> 00:13.340
For example, here I'm using VNC.

00:13.490 --> 00:17.930
Then you can open Thonny and we will write some Python code right here.

00:17.930 --> 00:24.530
And first of all we are going to include a Python module named Gpio zero.

00:24.530 --> 00:26.510
So Gpio zero.

00:26.540 --> 00:26.840
Okay.

00:26.870 --> 00:29.900
That's going to be the Python module that we want to import.

00:29.900 --> 00:34.940
So we can control the Gpio for the LED and how to import this module.

00:34.940 --> 00:40.160
Well previously you saw that we did that import time for example okay.

00:40.190 --> 00:43.640
Because time is a library that we wanted to use.

00:43.640 --> 00:44.900
We are also going to use it here.

00:44.900 --> 00:46.010
So I'm going to keep it here.

00:46.010 --> 00:48.860
You just write import and then the name of the library.

00:48.860 --> 00:52.430
Then we can add another import after that or even before that okay.

00:52.460 --> 00:54.320
The order doesn't really matter.

00:54.320 --> 00:58.700
And for example we could do import Gpio zero.

00:58.730 --> 01:06.000
Now the thing is when you do import library that's going to import the whole library with all the functionalities

01:06.000 --> 01:07.410
in this library.

01:07.410 --> 01:14.010
And as we will see with Python, sometimes it's better and we just want to import one specific functionality

01:14.040 --> 01:15.090
from the library.

01:15.090 --> 01:21.450
Because Gpio zero contains a lot of things for LEDs, push button and while many different devices.

01:21.450 --> 01:28.440
So if we want to just import for example the LED from Gpio zero, then we can just write.

01:28.440 --> 01:32.790
And notice here I used the word from we can just do from okay.

01:32.790 --> 01:36.630
This is a keyword from Gpio zero import.

01:36.630 --> 01:39.390
And then there's going to be led.

01:39.750 --> 01:44.640
So here we just import the LED because that's the only thing we need from that library okay.

01:44.640 --> 01:46.260
So that's kind of a good practice.

01:46.290 --> 01:52.050
And if you want to import several things from that library you can just add a comma and then put other

01:52.050 --> 01:53.040
functionalities.

01:53.160 --> 01:56.730
And let's just test that it works here I'm just going to run that.

01:56.730 --> 01:58.680
And you see we don't have any error.

01:58.710 --> 02:01.320
This means that it was found okay.

02:01.350 --> 02:04.770
So the Gpio zero is already installed for you.

02:04.800 --> 02:07.710
With Raspberry Pi OS you don't need to do anything.

02:07.710 --> 02:09.350
It's already installed and configured.

02:09.350 --> 02:09.680
Great.

02:09.680 --> 02:12.830
So now we can initialize our LED and how to do that.

02:12.860 --> 02:14.090
Well it's very simple.

02:14.090 --> 02:16.070
We're going to create a variable here.

02:16.250 --> 02:17.450
Let's call it LED.

02:17.450 --> 02:19.940
Lowercase is equal to.

02:19.970 --> 02:21.740
And then we're going to use the LED here.

02:21.740 --> 02:26.720
So uppercase LED we're going to open and close parentheses.

02:26.750 --> 02:30.680
And inside that we're going to need to put the Gpio number.

02:30.680 --> 02:35.390
So on the image you can see we used the Gpio number 17.

02:35.390 --> 02:37.670
So you just put 17 right here.

02:37.670 --> 02:39.800
And that's how you initialize an LED.

02:39.800 --> 02:42.650
So under the hood what it will do.

02:42.650 --> 02:48.800
And as you've seen in the previous lesson it will set the Gpio 17 as output.

02:48.800 --> 02:51.890
So then we can control it then how to control it.

02:51.890 --> 02:55.730
So how to say that we want the state to be high or to be low.

02:55.760 --> 02:57.500
Then you can do LED.

02:57.500 --> 03:03.230
So you can use this variable which in fact more technically is going to be an object.

03:03.260 --> 03:03.440
Okay.

03:03.470 --> 03:05.180
But I'm going to keep the word variable here.

03:05.180 --> 03:07.880
So you're going to use LED dot.

03:07.940 --> 03:12.970
And then you have a function inside the LED Uh, functionality here.

03:13.000 --> 03:16.270
LED Dot on to turn on the LED.

03:16.510 --> 03:17.860
It's quite simple.

03:17.860 --> 03:21.370
And as you call a function, you need to put some parentheses.

03:21.610 --> 03:25.570
Every time you call a function directly, you need to put the parentheses here.

03:25.570 --> 03:29.410
We don't have any argument to pass to the function okay.

03:29.440 --> 03:32.020
But we still need to give the parentheses.

03:32.020 --> 03:35.770
So LED on is going to turn on the LED.

03:35.770 --> 03:37.930
So it's going to set the state for the LED.

03:37.960 --> 03:41.530
So for the pin to high or 3.3V.

03:41.530 --> 03:45.370
And then you also have led dot off okay.

03:45.400 --> 03:47.050
So it's quite simple to understand.

03:47.080 --> 03:49.180
And this one is going to turn off the LED.

03:49.180 --> 03:55.420
So it's going to set the Gpio to low or basically close to zero volts okay.

03:55.420 --> 03:58.510
So what's going to happen if we run this program.

03:58.510 --> 04:04.600
What we will not see anything because you ask to turn on the LED and to turn it off right after it.

04:04.600 --> 04:08.440
So you will not see anything, or maybe just a very brief flash.

04:08.440 --> 04:15.140
So what we can do is we're going to use this time library here to do time dot sleep Clip.

04:15.590 --> 04:18.380
And then let's sleep for one second.

04:18.590 --> 04:18.800
Okay.

04:18.830 --> 04:20.390
So this we have used already.

04:20.420 --> 04:25.250
We're going to turn on the LED for one second and then turn off the LED.

04:25.250 --> 04:26.960
And that's the end of the program.

04:26.960 --> 04:30.980
So let's run this program and see what it's going to do.

04:30.980 --> 04:35.270
And you can see the LED is turned on for one second and then turn off.

04:35.270 --> 04:36.680
I can run the program again.

04:36.680 --> 04:40.220
So I click again and we have the same behavior.

04:40.250 --> 04:45.020
Now there is one thing I want to show you is, okay, let's say that we only have this line.

04:45.020 --> 04:46.340
So I'm going to comment.

04:46.460 --> 04:48.050
You see the comment is quite useful here.

04:48.050 --> 04:52.340
So I can comment it and then we can put the code back again later.

04:52.340 --> 04:56.180
So for now we just set the LED to on so to high.

04:56.180 --> 04:58.160
And let's see what's going to happen.

04:59.360 --> 05:06.650
And as you can see the LED is turned on and then it stays on okay the program has finished but the LED

05:06.650 --> 05:07.790
stays on.

05:07.790 --> 05:11.690
And you might think well it's quite obvious we didn't ask to turn it off.

05:11.690 --> 05:18.020
But actually in this Gpio zero there is a what is called a cleanup mechanism okay.

05:18.050 --> 05:24.480
Because when you finish your program, you want to basically reset all the pins to their default state

05:24.480 --> 05:25.650
and default mode.

05:25.650 --> 05:29.130
And basically the default mode for all the pins is input.

05:29.130 --> 05:35.490
And this is just a simple safety mechanism, because if you leave a pin as output and then let's say

05:35.490 --> 05:39.930
you plug a pushbutton in it, you might create a short circuit.

05:39.990 --> 05:41.700
So no need to worry about this for now.

05:41.700 --> 05:47.940
But you just need to know that if we run the code like this from thonny, the Gpio zero is not going

05:47.940 --> 05:49.230
to do the cleanup correctly.

05:49.230 --> 05:53.940
And I'm just talking about this now, because later on, we're going to actually use the terminal to

05:53.970 --> 05:55.110
run our programs.

05:55.110 --> 05:58.590
And when using the terminal we won't have this problem.

05:58.620 --> 05:58.860
Okay.

05:58.890 --> 06:01.260
So don't worry if you didn't really understand what I just said.

06:01.260 --> 06:07.200
Now it's going to make sense when we use the terminal and you're going to see the difference with what

06:07.200 --> 06:08.040
we did right now.

06:08.040 --> 06:09.600
So we have our LED on.

06:09.600 --> 06:11.940
I'm going to come back to this code LED on.

06:11.940 --> 06:14.700
We sleep for one second and then led off.

06:14.700 --> 06:22.080
Now what if you want to do this not just one time, but maybe two, three, five, ten times or even

06:22.110 --> 06:23.740
an infinite amount of time.

06:23.740 --> 06:27.580
Well, you're not going to repeat this code an infinite amount of time.

06:27.580 --> 06:29.320
You can use a loop.

06:29.320 --> 06:35.170
So for example, let's say that I want to simply do a infinite while loop.

06:35.170 --> 06:41.260
And I just want to say that when I start the program, the LED is going to blink until I stop the program,

06:41.260 --> 06:44.260
I can do while and then how to create an infinite loop.

06:44.260 --> 06:45.910
Well, I'm just going to put true.

06:45.940 --> 06:52.480
While true, so true is always going to be true, which means that we are always going to continue executing

06:52.480 --> 06:56.290
the loop until we here, until we press on the stop button.

06:56.290 --> 07:00.940
And so what I need to do here, you can see I have to put that with an extra indentation.

07:00.940 --> 07:06.250
So I select all the code and I press tab which means that this is going to be in the while loop.

07:06.250 --> 07:11.950
And it misses something because if you read the code what's going to happen with an on the LED?

07:11.980 --> 07:15.310
We wait for one second, then we turn off the LED.

07:15.550 --> 07:17.410
We go back and we turn on the LED.

07:17.710 --> 07:22.300
So we don't wait here before led off and led on.

07:22.300 --> 07:27.550
So if you run the code like this, you will see the LED D on all the time.

07:27.550 --> 07:33.190
Because there is there is no delay here between led off and LED on.

07:33.190 --> 07:38.950
So we need to add another time dot sleep with let's say one second as well.

07:39.190 --> 07:45.040
So it's going to turn on the LED, wait for one second and turn off the LED, wait for one second,

07:45.070 --> 07:50.980
etc. and let's run that program here okay.

07:50.980 --> 07:54.220
And you can see now the LED is blinking.

07:54.220 --> 08:00.100
And if I want to stop the program I can just click on stop here okay.

08:00.100 --> 08:03.730
So here I stopped and you can see it was off.

08:03.850 --> 08:06.880
But now let's run it again and let's stop it.

08:06.910 --> 08:13.480
Now you see I stop it when it's on and the LED stays on for the same reason as I told you before, because

08:13.480 --> 08:19.120
the Gpio zero is not cleaning up correctly here when we run the code from Thonny.

08:19.120 --> 08:23.770
But later on, as I told you before, everything is going to be fixed with the terminal.

08:23.770 --> 08:30.190
So you have learned how to control a LED with Python, and you can see with this Gpio library it's very

08:30.190 --> 08:30.910
easy to do.
