Tuesday, October 30, 2012

Raspberry Pi GPIO Input Output

Raspberry Pi GPIO with Python 3 and Fedora 17

In my last post Raspberry Pi with Fedora 17 we setup the Raspberry Pi with the Fedora 17 Remix. Now we want to use the RPi.GPIO module to access inputs wired to the pins on the RPi. The General Purpose Input/Output (GPIO) is a generic pin on a chip whose behavior (including whether it is an input or output pin) can be controlled (programmed) by the user at run time and accessed via the RPi GPIO P1 header.
 This header is connected directly to the Broadcom BCM2835 chip that is under the Samsung memory chip PoP (Package on Package) on the right. You can tell if you have 256Mb or 512Mb of memory by looking at the number just under the word Samsung on that chip. If it has "4G" it is 512 and if it has "2G" it is 256. You can connect to the GPIO pins with the Pi Cobbler Breakout Kit from Adafruit.com for $7.95.
You will need a breadboard and and experimenters kit with LED's, resistors and breadboard jumpers to get started. If you don't have the RPi yet you could get the Raspberry Pi Starter Pack from Adafruit for $105 that has everything you will need to start experimenting.
Now that we have all that we need we can setup the circuit to run the input test on the breadboard.

Boot up the RPi and log on the as the user pi. With the Graphical log on running you may not have enough memory to setup the development environment so we need to shutdown X and set the default log on to console only:

How do I change the default runlevel to 3 to boot to the console in Fedora 17?
$ su
# rm /etc/systemd/system/default.target
rm: remove symbolic link `/etc/systemd/system/default.target'? y
# ln -sf /lib/systemd/system/multi-user.target \ /etc/systemd/system/default.target

Now reboot and make sure that the RPi starts up to a text log on:
# reboot

Now you can SSH back into the RPi as the user pi and change to root then install the dev tools:

# yum groupinstall 'Development Tools'

Transaction Summary
Install  35 Packages (+35 Dependent packages)
Upgrade              (  4 Dependent packages)

Total download size: 128 M
Is this ok [y/N]:Y

This would be a good time to take a break and consume some of your favorite beverage.
After that is complete you will need to install Python 3 (Python 2.7 is installed) to get the latest RPi.GPIO to install.
$ su
# yum install python3.armv5tel python3-devel.armv5tel

Now that we have some memory freed up we can get the GPIO module installed:
You can get the exact download path from http://pypi.python.org/packages/source/R/RPi.GPIO/
and copy the link address from the green download button.

# wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.4.0a.tar.gz

Now extract the tarball:
# tar zxvf RPi.GPIO-0.4.0a.tar.gz

cd into the directory:
# cd RPi.GPIO-0.4.0a/

# python3 setup.py install

To test this install drop into python and check GPIO:

# python3
Python 3.2.3 (default, Jun  9 2012, 06:36:42)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO
<module 'RPi.GPIO' from '/usr/lib/python3.2/site-packages/RPi.GPIO-0.4.0a-py3.2-linux-armv6l.egg/RPi/GPIO.cpython-32mu.so'>

Now drop back to the pi user with [CTRL]+[D] and cd into pi.

# cd /home/pi

Create a file called mybutton.py
$ vi mybutton.py
press "i" to enter the -- INSERT -- mode and then type in the program:


import time
import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.IN)

while True:
        mybutton = GPIO.input(11)
        if mybutton == False:
                print ("Button Pushed")

When done press [Esc] to exit the insert mode and press ":" (or [Shift]+[;]) and enter wq and press [Enter]

This will write the file to disk and quit.

With the circuit wired up we can run mybutton program:

$ sudo python3 mybutton.py
[sudo] password for pi:
Button Pushed
Button Pushed

So when you press the button you should see the LED flash and "Button Pushed" printed to the console.
To exit the running program press [CTRL+C]

The program imports the needed modules and sets the GPIO pin 11 to an input.
Then the GPIO.input reads pin 11 as a True or False state.

A note on the pinouts:
  Use GPIO.setmode(GPIO.BOARD) to use Raspberry Pi board pin numbers (The ribbon connector pins) and use GPIO.setmode(GPIO.BCM) to change to BCM GPIO numbering (GPIO numbers on the CPU pins).
  For most tutorials we will use the GPIO.BOARD numbering.

The next step is to use the GPIO as an output. Set up the circuit below by moving the 470 Ohm resistor 90 degrees and adding the GPIO 18 as input pin 12.

Next, create a new file called pushbutton.py 

# vi pushbutton.py

Press "i" to get into the insert mode and type (or copy and paste from) the program below:


# pushbutton.py

import sys, time
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # Disable Warnings, Channel in use
GPIO.setup(11, GPIO.IN)
GPIO.setup(12, GPIO.OUT)

while True:
    if GPIO.input(11):
        GPIO.output(12, True)
        sys.stdout.write("\b\b /")
        sys.stdout.write("\b\b -")
        sys.stdout.write("\b\b \\")
        sys.stdout.write("\b\b |")

        GPIO.output(12, False)

Then hit the [ESC] key to exit the insert mode and press ":wq" to write and quit vi. Now run it...

# python3 pushbutton.py

This time we imported the sys module to have a little more control with output to the console.  The GPIO 18/Pin 12 is set to be an output line and when the input Pin 11 is pulled to a low state we set the output line Pin 12 low to pull down that side of the LED so that we have a voltage drop across it and it lights up.

The sys.stdout lines print a spinner to the console while waiting for a button press, then prints "on" when the button press sets the LED to on.

The GPIO.setwarning(False) disables the warning that is displayed because we didn't run GPIO.cleanup() on exit. Instead we just press [CTRL]+C to exit the running program (and not getting a chance to cleanup IO first).

If you want more detail on "Interfacing Projects for Beginners" then check out the "In Control" columns Part 1 and 2 that I used as a reference from the Raspberry Pi magazine TheMagPi.com issues 2 and 3.

Next time I will try the temperature project with the I2C  sensor tmp102 from issue 5.

No comments:

Post a Comment