To use the GPIO pins as output, we need to use the Python module gpiozero
. This library is already installed in the workspace so you can use it immediately.
If you own a Raspberry Pi, the gpiozero
library may already be installed; however, if it is not, you can install it by running the following command in the terminal:
pip3 install gpiozero
Refer to the gpiozero documentation for more information.
To use the gpiozero
module in our code, we have to write this line at the top of the script:
from gpiozero import LED
This line instructs Python to bring in all the necessary code that we need to work with LEDs.
The gpiozero
is known as a module which contains prewritten code that we can use. Reference the Codecademy doc on modules for further explanation.
The LED
instructs Python to import the LED class which represents an LED in Python. To use the LED class, we create a variable called light
and assign to it an LED object like so:
from gpiozero import LED light = LED(10)
The variable light
holds an instance of the LED
class. A class is the design of an object, the instance is the object itself. Classes contain variables and functions that you can access and use. Each instance of a class will have its own copy of these variables and functions!
Reference the Codecademy doc on classes for further explanation.
The keyword LED(10)
instructs Python that there is an LED connected to pin 10 of the Raspberry Pi GPIO.
In the next exercise, we will see how we can turn the LED on and off.
Instructions
Write the line of code that is necessary to bring in the proper library to work with LEDs.
Create an instance of an LED and call it led
. The physical LED will be connected on pin 14.