Tuesday 10 July 2012

RasPi GPIO outputs

Today I decided to give my RasPi GPIO a try. I picked up a breadboard kit ($12.99) and 3 packages of 5 LEDs to (red, green, yellow) each $1.00 and 2 packs of 10 resistors (120 ohm) also each $1.00. The LEDs are 2.1-2.8 voltage drop, leaving about 1.2 volt remaining when being supplied from the 3.3v source of the RasPi (3.3-2.1v assuming minimum voltage drop). Doing the math, in order to keep my current around 10 mA we do V=IR, so 1.2v/0.010A = 120 ohm.

First thing was to install the Python compiler. I didn't do this at first and when trying to compile the RasPi GPIO library I got an error (said it couldn't find Python.h)! Looking to the forums I found the following command to install Python:

sudo apt-get install python-dev

Then according to our trusty friends at TheMagPi issue #2 (page 13), I downloaded the RPi GPIO file using the Midori browser from http://pypi.python.org/pypi/RPi.GPIO (to my home/pi directory) and then used these commands to unzip, untar and then compile it (use sudo if needed):


gunzip RPi.GPIO-0.3.0a.tar.gz
tar -xvf RPi.GPIO-0.3.0a.tar
cd RPi.GPIO-0.3.0a
sudo python setup.py install

Then after a few minutes we have it all installed. Then using nano editor "nano mytest.py", I created my first python program to turn on the GPIO 11 as follows:


#!/usr/bin/python
import time
import RPi.GPIO as GPIO
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# set up the GPIO channels - one output
GPIO.setup(11, GPIO.OUT)
count = 0
while (count < 10):
print "count", count
GPIO.output(11, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(11, GPIO.LOW)
time.sleep(0.5)
count = count + 1

Running the program was basically just running "sudo python mytest.py". I hooked up the GPIO 11 pin 11 from my RasPi to the + rail on my breadboard, and had a resistor and LED in series, then ground to pin 6. When the GPIO is set to high it output 3.3v which was enough to turn on the LED. Success!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.