the button was pressed. This comes with a few drawbacks: you’d
be using the processor on the board constantly, which will make
it difficult for other programs to function, and it would increase
the Raspberry Pi’s power consumption. Because
button.py
has to
share resources with other programs, you have to be careful that
it doesn’t hog them all up.
These are challenges that you’ll face when you’re using polling to
check the status of a digital input. One way to get around these
challenges is to use an
interrupt
, which is a way of setting a
specified block of code to run when the hardware senses a change
in the state of the pin. gpiozero supports interrupts—specifically,
detecting and responding to an edge
state change
(when the pin
changes from HIGH to LOW, or vice versa) and you can read about
how to use that feature in the library’s documentation (gpiozero.
readthedocs.io/en/stable/api_pins.html).
Note that edge detection only works on pins that support it,
and the library documents state that depending on the hardware,
the information read from the state at any given time is not guaran-
teed to be accurate.
Project: Simple Soundboard
Now that you know how to read the inputs on the Raspberry Pi,
you can use the sound functions of the Python module Pygame
to make a soundboard. A soundboard lets you play small sound
recordings when you push its buttons. To make your own sound-
board, you’ll need the following in addition to your Raspberry Pi:
• Three push button switches
• Female-to-male jumper wires
• Standard jumper wires or hookup wire, cut to size
• Solderless breadboard
• Computer speakers, or an HDMI monitor that has built-in speak-
ers. You can also use a simple pair of headphones—anything
that will plug into the Pi’s audio analog OUT port.
116 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 116GSW_RASPI_4ED_FIN.indd 116 10/28/21 10:54 AM10/28/21 10:54 AM