Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

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:

  1. Import the module to talk to the pins.

  2. Set up how you want to refer to the pins.

  3. Initialize the GPIO pins you want to use.

  4. 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.

#!/usr/bin/env python
#GPIO input state monitor on the Raspberry Pi
#GPIO state - show the state of all the GPIO inputs on P1
#non GPIO pins shown as x
import RPi.GPIO as GPIO
print "Display the GPIO input pin states"
print "Ctrl C to stop"
boardRevision = GPIO.RPI_REVISION
 #define the pins to use
if boardRevision == 1:
  pinout = [-1,-1,0,-1,1,-1,4,14,-1,15,17,18,21,-1,22,23,-1,24,10,-1,9,25,11,8,-1,7]
if boardRevision == 2:
  pinout = [-1,-1,2,-1,3,-1,4,14,-1,15,17,18,27,-1,22,23,-1,24,10,-1,9,25,11,8,-1,7]
GPIO.setmode(GPIO.BCM) # use real GPIO numbering
inputState = [ 5 for temp in range (0,26)] # blank list for input levels
for pin in range(0,26): # set all pins to inputs
 if pinout[pin] != -1:
  GPIO.setup(pinout[pin],GPIO.IN, pull_up_down=GPIO.PUD_UP)
  # replace line above with the line below to see the effect of floating inputs
  # GPIO.setup(pinout[pin],GPIO.IN, pull_up_down=GPIO.PUD_OFF)
while True: # do forever
 needUpdate = False
 for check in range(0,26): # look at each input in turn
  if pinout[check] != -1:
  if GPIO.input(pinout[check]) :
   latestState = 1
  else:
   latestState = 0
  if(latestState != inputState[check]):
   needUpdate = True
   print "GPIO ",pinout[check], "changed to a logic", latestState
   inputState[check] = latestState
 if needUpdate: # display all pin states
  print "Current state"
  for row in range(1,-1, -1):
  for show in range(row,26,2) :
   if inputState[show] != 5:
   print inputState[show],
   else:
   print "x",
  print " "
 #end of main loop

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.

blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.