Monday, August 20, 2012

Raspberry PI – Writing to GPIO Pins

The following will get all of the General Purpose I/O pins toggling high and low.


DO NOT connect leds directly to the pins, as the 3.3 volt rPI I/O pins are NOT buffered and you can blow the rPI board!


If you want to drive leds or other loads, you will need buffer electronics. (I used a high impedance logic probe to observe the pin outputs).



1) Install Python Development libraries (if not already installed)

  $ sudo apt-get install python-dev

( ref: http://nickhumphreyit.blogspot.ca/2011/10/buildbot-install-error.html )


2) Install RPi.GPIO Python Library (if not already installed)

Step 1 – Download the library

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

Step 2 – Extract the archive to a new folder

    $ tar zxf RPi.GPIO-0.3.1a.tar.gz

Step 3 – Browse to the new directory

    $ cd RPi.GPIO-0.3.1a

Step 4 – Install the library

  $ sudo python setup.py install

Step 5 – Remove the directory and its contents

  $ cd ..

  $ sudo rm -rf RPi.GPIO-0.3.1a/

Step 6 – Delete the archive file

  $ rm RPi.GPIO-0.3.1a.tar.gz

( ref: http://www.raspberrypi-spy.co.uk/2012/05/install-rpi-gpio-python-library/ )


3) Nano up this python program as "leds.py"

(If you want to make it shorter you can leave out some of the pins)

-----------------------------------------------------------------------------
import time
import RPi.GPIO as GPIO
#
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
#
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
#
count = 1
while (count < 100):
     print ("High")
     GPIO.output(3, True)
     GPIO.output(5, True)
     GPIO.output(7, True)
     GPIO.output(8, True)
     GPIO.output(10, True)
     GPIO.output(11, True)
     GPIO.output(12, True)
     GPIO.output(13, True)
     GPIO.output(15, True)
     GPIO.output(16, True)
     GPIO.output(18, True)
     GPIO.output(19, True)
     GPIO.output(21, True)
     GPIO.output(22, True)
     GPIO.output(23, True)
     GPIO.output(24, True)
     GPIO.output(26, True)
     time.sleep(1)
     print ("Low")
     GPIO.output(3,False)
     GPIO.output(5,False)
     GPIO.output(7,False)
     GPIO.output(8,False)
     GPIO.output(10,False)
     GPIO.output(11,False)
     GPIO.output(12,False)
     GPIO.output(13,False)
     GPIO.output(15,False)
     GPIO.output(16,False)
     GPIO.output(18,False)
     GPIO.output(19,False)
     GPIO.output(21,False)
     GPIO.output(22,False)
     GPIO.output(23,False)
     GPIO.output(24,False)
     GPIO.output(26,False)
     time.sleep(1)
print ("End")
-----------------------------------------------------------------------------

4) Run the python program (sudo runs it at root to get access to the metal )

  $ sudo python leds.py

Note that the pin numbering is the actual pin number of the GPIO ribbon connector, NOT the GPIO number (this had me scratching my head for a while).

References:

http://elinux.org/RPi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29




http://elinux.org/RPi_Low-level_peripherals#P2_header





Eric Pierce VA3EP - See the Disclaimer in the Introduction
 
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.



No comments:

Post a Comment