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

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Creating a multiplayer quick-reaction game

    The quick-reaction game provides an introduction to building simple circuits with the Raspberry Pi and controlling those circuits with ScratchGPIO, an advanced version of Scratch.

  • A Python interface to a large-format pen plotter

    Getting a large-format plotter operational presented a personal challenge. A Raspberry Pi with a USB-to-serial dongle was the easiest way to start plotting on my home network.

  • Pygame modules for interactive programs

    Pygame modules are particularly suited to programming highly interactive software. We look at the modules dedicated to events, sound, and input by keyboard, mouse, and game controller.

  • Graphical displays with Python and Pygame

    As its name implies, Pygame is a set of Python modules designed to write games. However, many Pygame modules are useful for any number of projects. We introduce you to a few Pygame modules that you can use to create custom graphical displays for your project.

  • Fast clocks, model railroads, LED displays, and more

    Some model train enthusiasts like to run their trains like a real railroad with a dispatcher controlling movements, issuing train orders, and making sure the train stays on schedule. But, time runs differently for these trains, thanks to fast clocks.