Testing GPIO in Python
1. Go into the Python interactive interpreter from the terminal
prompt by typing python3.
2. When you’re at the >>> prompt, try importing the LED
module:
>>> from gpio zero import LED
If you don’t get any errors after entering the import command,
you know it’s installed and ready to use.
1. As with other libraries, gpiozero allows you to control each
of the Pi’s pins individually. However, there’s a catch. gpiozero
refers to the pins not by their physical placement on the board,
but by their internal connection to the CPU, which is what’s
known as the pin’s Broadcom or BCM number. Other libraries
allow you to select how you’re going to refer to the pins; gpiozero
only gives you the Broadcom option. Luckily, it’s pretty easy
to find printouts online of the pins’ individual BCM numbers.
There is also an awesome mobile app called Electrodoc
(play.google.com/store/apps/details?id=it.android.demi.
elettronica&hl=en_US&gl=US or apps.apple.com/us/app/
electrodoc-pro/id1146647134) that I have installed on my phone,
which lets you call up the pin-outs for almost any hobbyist board
you can think of. I highly recommend it!
2. Tell the gpiozero library which pin to use. Let’s use GPIO
pin #4, and connect it the way you did in “Beginner’s Guide to
Breadboarding”:
3. >>> led = LED(4)
4. Turn on the LED:
>>> led.on()
5. Turn off the LED:
>>> led.off()
6. Exit the Python interactive interpreter:
>>> exit()
$
Programming Inputs and Outputs with Python 111
GSW_RASPI_4ED_FIN.indd 111GSW_RASPI_4ED_FIN.indd 111 10/28/21 10:54 AM10/28/21 10:54 AM