Reflection: What you just accomplished
You have just completed your first physical computing project! Congratulations, and welcome to the wonderful world of blinky lights.
I would like to use this step to reflect on what you have achieved and to pull out some key concepts that will be useful for the rest of the course. This pattern will continue for the remainder of the course: I will present you with a challenge and then ask you to reflect on what you have done.
Variables store information; in Python, any variable can store any type of information. Variables only ever store a single value at any one time, and forget their previous values when they are reassigned a new value. You used
red_led (a variable) to store an LED object. When you wanted to use it, you used this variable name to refer to the object.
Classes and objects
To control the LED, you used the module
gpiozero and specifically the bit that controls LEDs. Inside the module are loads of blueprints that you can use to control components; these are called classes.
I used an analogy when introducing modules earlier, comparing them to books. Continuing this, the classes are like chapters in that book, with all the details Python needs to interact with a specific type of component. Using analogies like this is called ‘abstraction’, and it is a useful tool in computing. It allows you to ignore unimportant complexities so that you can get on with the tasks at hand. You don’t need to worry about how the LED is turned on, only that it can be turned on.
Going beyond this analogy might be useful in this case. In reality, a class is a collection of Python code, usually with a collective purpose (like controlling an LED). You can use this code in your programs by first importing the module, and then creating an object. If a class is a blueprint, an object is what is made when following that blueprint. You can have lots of different objects that are all made from the same class. You created an object earlier when you set the variable
red_led = LED(17); before you did this there was no way to use the
LED class you had imported.
These are complicated concepts that will only become clear after practice, so for now I will distil this into two key learnings:
- To interact with a component, you have to import a class from the GPIO Zero library
- To use the class you must create an object, store it in a variable, and provide a GPIO pin for the specific component
Functions are instructions, or sets of instructions, that are run in order when they are called. Functions that are part of a class have a specific name: methods. The
off() methods you used switch the current flowing to the GPIO pin on and off. You can identify a method (and functions) by the brackets
() at the end.
gpiozero module has some built-in methods to give you even more control of your LED. These are abstractions. Complexity has been hidden away, making your code easier to write and read.
Either remove your
while True: loop or copy and paste your previous code into a new file, so that it looks like this:
from gpiozero import LED from time import sleep red_led = LED(17)
Now you can try adding the following code snippets to your program. Try them one at a time and play with the numbers to see what they do; can you work out what each line is doing? Ask in the comments if you would like any of them explained further.
red_led.blink(0.2, 0.1, 5)
red_led.toggle() sleep(1) print(red_led.is_lit) red_led.toggle() sleep(1) print(red_led.is_lit)
All of these methods are detailed in the documentation for the
gpiozero module. The LED portion can be found here. These documents can be intimidating at first; again, it takes a bit of practice to get comfortable with them.
Throughout the rest of the course, as we use each component I will include a link to the documentation for that component, so you can get some practice reading them yourself.
Choosing a pin
Earlier, you heard that you would have to choose a pin for your components, and you just did (more accurately, I chose one for you:
17). You can use any of the GPIO pins; these are the numbered pins on the guide below.
The numbering of the GPIO pins is not in numerical order, instead relating to the numbering on the CPU of the Raspberry Pi, so there is no easy way to remember them. However, you can use a reference board that fits over the pins, a printed reference (like the image above), or a website guide to the GPIO pins to help you.
Reflect on your learning
- Were you already aware of the concepts covered here?
- Do you have any questions before we continue?
- What was your first impression of the GPIO Zero documentation?
Share your answers in the comments section.