Building game show buzzers with a Raspberry Pi
Imports – Lines 1 to 3
The import
command is Python's directive to reference a library or external Python file. Line 3 uses the as
keyword to reference a library by a different name. Without the as
keyword, I would have to use the full library name every time I referenced it. A name like RPi.GPIO
isn't too bad, but when the library is something like hugeLibrary.finalRelease.multimedia.sound.sample.load
<as wave
>, it's extremely handy!
Raspberry Pi GPIO Setup – Lines 5 to 10
The GPIO.setmode
function configures the library to use the appropriate GPIO number to physical pin mapping. You have two numbering methods on the Raspberry Pi: GPIO.BCM and GPIO.BOARD. BCM refers to the pin numbers on the processor itself. To see where you're connecting, you have to refer to a datasheet for the specific model of Raspberry Pi that your project uses [5].
BOARD refers to the pin numbers of the GPIO header itself. These numbers will remain the same until the physical layout of the Raspberry Pi changes. Why use BCM numbering over BOARD numbering? In this project, it would work just fine either way. When you start using special features of the processor (UART, SPI, I2C), they are connected to actual processor pins, so using BCM numbering makes more sense.
The GPIO.setup
function accepts three arguments. The first argument is the pin number you want to set up. This relies on the number mode you decided on earlier. The second argument configures the pin as an input or an output – whether the pin is "listening" or "talking." GPIO.OUT
configures a pin as an output. It will immediately begin actively driving the pin low (also known as grounded, 0 volts, off, 0, or false) or high (3.3 volts, on, 1, or true). GPIO.IN
configures the pin as an input. If the pin is an input, then you also have to configure the pull direction.
The third argument defines the pull direction. It is only applicable if the pin is an input. When a pin is configured as an input, it is "listening" to voltage changes coming from whatever is connected to the pin. Setting up a pull-up or pull-down resistor effectively provides a default value on the electronics side. In the absence of any other connection, a small amount of current flows, which is just enough for the processor to sense a low (pull-down) or high (pull-up) level. When the button is pressed or switch closed, it provides an unrestricted current path to the opposite voltage pole. The strong signal overrides (literally overpowers) the pull resistor, which provides a distinct change from state to state.
Without a pull resistor, the pin floats. If nothing is connected to the pin, then it is susceptible to outside interference and will flip unpredictably between low and high states. Assume your button is connected between ground and the GPIO pin. When the button is pressed, you'll get a solid low state, but there's no guarantee that the pin will return to a high state after the button is released. Enabling the pull-up resistor solves the problem.
Buy this article as PDF
Pages: 8
(incl. VAT)