Now that you have created your first circuit with an LED, let's control it with Python.
If it's not done yet, you can power on your Raspberry Pi and just connect to it like you did before.
For example, here I'm using VNC.
Then you can open Thonny and we will write some Python code right here.
And first of all we are going to include a Python module named Gpio zero.
So Gpio zero.
Okay.
That's going to be the Python module that we want to import.
So we can control the Gpio for the LED and how to import this module.
Well previously you saw that we did that import time for example okay.
Because time is a library that we wanted to use.
We are also going to use it here.
So I'm going to keep it here.
You just write import and then the name of the library.
Then we can add another import after that or even before that okay.
The order doesn't really matter.
And for example we could do import Gpio zero.
Now the thing is when you do import library that's going to import the whole library with all the functionalities
in this library.
And as we will see with Python, sometimes it's better and we just want to import one specific functionality
from the library.
Because Gpio zero contains a lot of things for LEDs, push button and while many different devices.
So if we want to just import for example the LED from Gpio zero, then we can just write.
And notice here I used the word from we can just do from okay.
This is a keyword from Gpio zero import.
And then there's going to be led.
So here we just import the LED because that's the only thing we need from that library okay.
So that's kind of a good practice.
And if you want to import several things from that library you can just add a comma and then put other
functionalities.
And let's just test that it works here I'm just going to run that.
And you see we don't have any error.
This means that it was found okay.
So the Gpio zero is already installed for you.
With Raspberry Pi OS you don't need to do anything.
It's already installed and configured.
Great.
So now we can initialize our LED and how to do that.
Well it's very simple.
We're going to create a variable here.
Let's call it LED.
Lowercase is equal to.
And then we're going to use the LED here.
So uppercase LED we're going to open and close parentheses.
And inside that we're going to need to put the Gpio number.
So on the image you can see we used the Gpio number 17.
So you just put 17 right here.
And that's how you initialize an LED.
So under the hood what it will do.
And as you've seen in the previous lesson it will set the Gpio 17 as output.
So then we can control it then how to control it.
So how to say that we want the state to be high or to be low.
Then you can do LED.
So you can use this variable which in fact more technically is going to be an object.
Okay.
But I'm going to keep the word variable here.
So you're going to use LED dot.
And then you have a function inside the LED Uh, functionality here.
LED Dot on to turn on the LED.
It's quite simple.
And as you call a function, you need to put some parentheses.
Every time you call a function directly, you need to put the parentheses here.
We don't have any argument to pass to the function okay.
But we still need to give the parentheses.
So LED on is going to turn on the LED.
So it's going to set the state for the LED.
So for the pin to high or 3.3V.
And then you also have led dot off okay.
So it's quite simple to understand.
And this one is going to turn off the LED.
So it's going to set the Gpio to low or basically close to zero volts okay.
So what's going to happen if we run this program.
What we will not see anything because you ask to turn on the LED and to turn it off right after it.
So you will not see anything, or maybe just a very brief flash.
So what we can do is we're going to use this time library here to do time dot sleep Clip.
And then let's sleep for one second.
Okay.
So this we have used already.
We're going to turn on the LED for one second and then turn off the LED.
And that's the end of the program.
So let's run this program and see what it's going to do.
And you can see the LED is turned on for one second and then turn off.
I can run the program again.
So I click again and we have the same behavior.
Now there is one thing I want to show you is, okay, let's say that we only have this line.
So I'm going to comment.
You see the comment is quite useful here.
So I can comment it and then we can put the code back again later.
So for now we just set the LED to on so to high.
And let's see what's going to happen.
And as you can see the LED is turned on and then it stays on okay the program has finished but the LED
stays on.
And you might think well it's quite obvious we didn't ask to turn it off.
But actually in this Gpio zero there is a what is called a cleanup mechanism okay.
Because when you finish your program, you want to basically reset all the pins to their default state
and default mode.
And basically the default mode for all the pins is input.
And this is just a simple safety mechanism, because if you leave a pin as output and then let's say
you plug a pushbutton in it, you might create a short circuit.
So no need to worry about this for now.
But you just need to know that if we run the code like this from thonny, the Gpio zero is not going
to do the cleanup correctly.
And I'm just talking about this now, because later on, we're going to actually use the terminal to
run our programs.
And when using the terminal we won't have this problem.
Okay.
So don't worry if you didn't really understand what I just said.
Now it's going to make sense when we use the terminal and you're going to see the difference with what
we did right now.
So we have our LED on.
I'm going to come back to this code LED on.
We sleep for one second and then led off.
Now what if you want to do this not just one time, but maybe two, three, five, ten times or even
an infinite amount of time.
Well, you're not going to repeat this code an infinite amount of time.
You can use a loop.
So for example, let's say that I want to simply do a infinite while loop.
And I just want to say that when I start the program, the LED is going to blink until I stop the program,
I can do while and then how to create an infinite loop.
Well, I'm just going to put true.
While true, so true is always going to be true, which means that we are always going to continue executing
the loop until we here, until we press on the stop button.
And so what I need to do here, you can see I have to put that with an extra indentation.
So I select all the code and I press tab which means that this is going to be in the while loop.
And it misses something because if you read the code what's going to happen with an on the LED?
We wait for one second, then we turn off the LED.
We go back and we turn on the LED.
So we don't wait here before led off and led on.
So if you run the code like this, you will see the LED D on all the time.
Because there is there is no delay here between led off and LED on.
So we need to add another time dot sleep with let's say one second as well.
So it's going to turn on the LED, wait for one second and turn off the LED, wait for one second,
etc. and let's run that program here okay.
And you can see now the LED is blinking.
And if I want to stop the program I can just click on stop here okay.
So here I stopped and you can see it was off.
But now let's run it again and let's stop it.
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
the Gpio zero is not cleaning up correctly here when we run the code from Thonny.
But later on, as I told you before, everything is going to be fixed with the terminal.
So you have learned how to control a LED with Python, and you can see with this Gpio library it's very
easy to do.