7/Programming
Inputs and Outputs
with Python
At the end of Chapter 6, you did a little bit
of programming with the Raspberry Pi’s
GPIO pins using a shell script. In this chap-
ter, you’re going to learn how to use Python
to do the same thing… and a little more.
As with the shell script, Python will let you
access the GPIO pins with code that reads
and controls the pins automatically.
The advantage that Python has over shell scripting is that Python
code is easier to write and is more readable. There’s also a whole
slew of Python modules that make it easy for you to do some
complex stuff with basic code. See Table 4-2 for a list of a few
modules that might be useful in your projects. Best of all, there’s
a Python module called GPIO Zero (gpiozero.readthedocs.io/en/
stable) that makes it easy to read and control the GPIO pins. You’re
going to learn how to use that module in this chapter.
Installation
GPIO Zero is now included in the desktop version of the Raspberry
PI OS. It builds on some other libraries that you may be familiar
with, including RPi.GPIO (bit.ly/1vzTBtI) and pigpio, and even lets
you select particular pin libraries as you need them.
Programming Inputs and Outputs with Python 109
GSW_RASPI_4ED_FIN.indd 109GSW_RASPI_4ED_FIN.indd 109 10/28/21 10:54 AM10/28/21 10:54 AM
If you are using Raspberry Pi OS Lite or another OS on the Pi, you’ll
need to install GPIO Zero, which is not a difficult task.
With the Pi OS Lite, first update your repositories list with
pi@raspberrypi: ~ $ sudo apt update
Then install either the Python 2 or Python 3 package:
pi@raspberrypi: ~ $ sudo apt install python-gpiozero
or
pi@raspberrypi: ~ $ sudo apt install python3-gpiozero
If you’re using another OS, you may need to use pip to install it in-
stead. Use
pi@raspberrypi: ~ $ sudo pip install gpiozero
or
pi@raspberrypi: ~ $ sudo pip3 install gpiozero
for Python 2 or 3, respectively.
There are some programmers, myself included, who
frown on using sudo when installing packages with pip.
This is because using sudo may install some of the
dependencies and binaries in directories that are
inaccessible to ordinary, non-root users. That can often
lead to problems when trying to use the packages that
were installed this way. Most often I will use the --user
flag, like so:
pipinstall --usergpiozero
Only if the installation fails that way will I use the sudo method.
110 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 110GSW_RASPI_4ED_FIN.indd 110 10/28/21 10:54 AM10/28/21 10:54 AM
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
In Chapter 6, you learned that digital input and output
signals on the Raspberry Pi must be either 3.3V or
ground. In digital electronics, we refer to these signals
as high or low, respectively. Keep in mind that not all
hardware out there uses 3.3V to indicate high; some
use 1.8V, and others use 5V. If you plan on connecting
your Raspberry Pi to digital hardware through its GPIO
pins, it’s important that the other hardware also uses
3.3V.
These steps give you a rough idea of how to control the GPIO pins by
typing Python statements directly into the interactive interpreter.
It’s hard to imagine it being any easier, to be honest! Just as you
created a shell script to turn the pins on and off in Chapter 6,
you’re going to create a Python script to read and control the pins
automatically.
Blinking an LED
To blink an LED on and off with Python, you’re going to use the
statements that you already tried in the interactive interpreter,
in addition to a few others. For the next few steps, we’ll assume
you’re using the desktop environment (as shown in Figure 7-1),
but feel free to use the command line to write and execute these
Python scripts if you prefer.
Here’s how to blink an LED with a Python script:
1. Open the text editor by clicking the Raspberry menu
AccessoriesTextEditor.
2. Enter the following code:
from gpizero import LED
import time
led = LED(4)
while True:
112 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 112GSW_RASPI_4ED_FIN.indd 112 10/28/21 10:54 AM10/28/21 10:54 AM
led.on()
time.sleep(1)
led.off()
time.sleep(1)
Import the code needed for GPIO control.
Import the code needed for the sleep function.
Set pin 4 as an output for the LED.
Create an infinite loop consisting of the indented code
below it.
Turn the LED on.
Wait for one second.
Turn the LED off.
Wait for one second.
3. Save the file as
blink.py
within the home directory,
/home/pi
.
There’s a shortcut to this folder in the places list on the left side
of the Save As window.
Remember—indentation matters in Python!
4. Open LXTerminal, then use these commands to make sure
the working directory is your home directory, and execute the
script (see Figure 7-1):
pi@raspberrypi ~/Documents $ cd ~
pi@raspberrypi ~ $ python blink.py
5. Your LED should now be blinking!
6. Press Ctrl-C to stop the script and return to the command
line.
Programming Inputs and Outputs with Python 113
GSW_RASPI_4ED_FIN.indd 113GSW_RASPI_4ED_FIN.indd 113 10/28/21 10:54 AM10/28/21 10:54 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset