How to Control GPIO Pins on Your Raspberry Pi

In order to have the Raspberry Pi gain access to the GPIO pins, you must install a Python module that acts as an interface between the language and the pins. This allows a program to read in what is happening to the signals on the GPIO lines.

A few different choices exist, but perhaps the easiest to install is RPi.GPIO. Perhaps the easiest way to install it is from a terminal window with the command

sudo apt-get install python3-rpi.gpio

If you have a previous version of this module, you have to first uninstall the old one by manually deleting the files. Find them with this command in a command line window

find /usr | grep python | grep -i gpio

and delete the ones you find.

Any Python program that needs to access the GPIO pins must do the following operations:

Import the module to talk to the pins.

Set up how you want to refer to the pins.

Initialize the GPIO pins you want to use.

Read or write to the pins.

This simply sets all the GPIO pins to be inputs and then constantly reads them back into the Raspberry Pi. It prints out the new state when any of the inputs change.

Take a look at the code and see what is happening. After importing the RPi.GPIO module, the code finds the board revision and initializes a list based on what the board revision is. The list has a value of -1 in the pin positions where there is no GPIO pin (that is, where there is a power or ground pin).

Otherwise, it has the GPIO number in the corresponding pin position of the list. Then the code creates a list that holds the input values for each pin.

This code is filled in with an arbitrary value of 5, knowing that any real input value will be only a zero or one. Next, all the pins are set to be inputs with the internal pull-up resistors enabled. That ends the setup portion of the program.

The rest of the code is a loop that runs forever. It checks each input by looking at the pinout list and calling the GPIOinput(pin) to return the value of each input. This is compared with the value from last time. If it's different, it prints out the current state of that input along with all the values of input arranged in the same order as the P1 connector’s pins.

To run the program, open up a terminal window, navigate to the directory containing the program, and type

sudo python GPIOstate.py

You need the sudo prefix because the operating system restricts access to the GPIO pins and you need root privileges to be able to use them.

To test this out, simply connect one end of a wire to one of the ground connections and go around, in turn, connecting the other end of the wire to each input. All of the inputs initially show as logic ones on the screen and change to logic zeros when you ground the input.

Be careful not to connect this ground wire to any of the power outputs: This will short out your Raspberry Pi’s power. To prevent this, put a small piece of plasticine (Play-Doh, modeling clay, or blue tack) in the screw tops of the power connectors.