- Category: Raspberry Pi
- Last Updated on Friday, 21 June 2013 13:23
This is the first of two articles showing basic GPIO on the Raspberry-Pi using the prototype area of the Slice of Pi. This covers basic details on the GPIO pins, setting up a Python library to allow access to the GPIO. There is an example circuit to build on the Slice and some code to get the outputs working.
This was originally a blog post on Matts blog at lwk.mjhosting.co.uk
The R-Pi has 17 GPIO pins brought out onto the header, most have alternated functions other than just I/O, there are two pins for UART, two for I2C and six for SPI. All the pins can be use for GPIO with either INPUT or OUTPUT, there also internal pull-up & pull-downs for each pin but the I2C pins have and onboard pull-up so using them for GPIO may not work in some cases.
Using any of the pins will require extra care, than most Arduino users maybe be used to. These pins are 3V3 not 5V like the AVR chips, and they a directly connected to the Broadcom chip at the heart of the R-Pi. This means there is not protection, if you send 5V down a pin there is a good chance of killing the Pi.
There will also be an issue with trying to draw to much power form the pins, according to the data-sheet each pin programmed to current drive between 2mA and 16mA, and it has been warned that trying to draw 16mA from several pins at once could also lead to a damaged Pi.
Also from the wiki the "maximum permitted current draw from the 3v3 pin is 50mA" and the "maximum permitted current draw from the 5v pin is the USB input current (usually 1A) minus any current draw from the rest of the board." The current draw for Model B is stated as 700mA so with a 1A power supply this leaves about 300mA to play with.
Anyway enough about power theres still plenty to try driving some basis LED's and use the UART to talk to an XRF.
So how does one go about talking to the GPIO's? Well thanks to a little python library its nice and simple. I'll be using a Debian install but this should work on the others.
NOTE 1: The instructions here refer to an early version of RPi.GPIO. Please search the web for the latest version and replace the version numbers in the instructions below.
NOTE 2: If you have purchased Ciseco's 4G Raspberry Wheezy SD card, configured for Ciseco products, or used the Ciseco Wheezy image to create an SD card yourself, then the RPi.GPIO should already be installed and you can skip this step.
RPi.GPIO is a small python library that take some of the complexity out of driving the GPIO pins, once install a single LED can be lit with 3 lines of python. Installing the library is almost as simple, either at a text console or using LXTerminal enter the following
$ wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.1.0.tar.gz $ tar zxf RPi.GPIO-0.1.0.tar.gz $ cd RPi.GPIO-0.1.0 $ sudo python setup.py install
So now I need and LED or two to Flash. So using a Slice of Pi from Ciseco I have wired up four LED's with resistors and two Push button's.
First to be soldered up were the LED's and push buttons then I adde the resistors and connecting wires.
To keep the GPIO pins nice and safe I'm using 470R resistors for the RED,YELLOW and GREEN LED's and a 330R for the BLUE, this will keep the current of each led to around 6-7mA. Here's the schematic and the board
If you look at the LED's I'm using the 3.3v rail to power the led and have the cathode going to the GPIO's this mean that to turn the LED on we set the output to LOW or False (in python's case) but to turn the off we set the output to HIGH or True.
This means we a sinking the current through the Raspberry Pi rather that trying to source it from the pin's. For the push button we are using a 10K pull-down resistor, this makes sure the button read a solid LOW or False when not pressed, when pressed the 3.3v is connected and we get a solid HIGH or True reading.
I said turning LED's on was easy well try this in a terminal:
$ sudo python >>> import RPi.GPIO as GPIO >>> GPIO.setup(18, GPIO.OUT) >>> GPIO.output(18, False)
Again it should look a little some thing like this
Anyone trying this themselves may notice that the RED LED actual turned on after the GPIO.setup line and that GPIO.output did nothing. This is because we have the cathode of the LED's wired to the GPIO, and are using the Raspberry Pi to switch the GND. The default state for a output pin is LOW and so this also power to flow throughout the LED. So how about turning that yellow LED on:-
>>> GPIO.setup(16, GPIO.OUT)
>>> GPIO.output(18, True) >>> GPIO.output(16, True)
Now why if we wired the red LED to GPIO5 on the Slice of Pi, why am I using 18 to control it? Well theres a bit of fun with the pin numbering on Raspberry Pi, and some how i don't expect it to go anyway and time soon, it like that odd gap on the Arduino headers. It comes from this post originally Pinout for GPIO connectors, better shown in the left pic bellow, anyway it turns out the original numbering of GPIO0-GPOI7 relates to nothing logical once inside linux, as they pins are accessed via there BCM2835(the chip on the Pi) GPIO numbers. See the right pic for the internal numbering, also all the signal named pins can be used as GPIO's and have numbers as well.
Anyway RPi.GPIO python library has used yet another form of reference which is to us the pin number on the header, This give three names for each pin :( When referred to by pin number (officially P1_18) we are talking about pin 18 as counted out on the header, GPIO5 by name and GPIO24 internally by the BCM2835. Now if you ever work with the IO using the Shell file access or C memory mapped registers your going to use the BCM2835 numbering.
Confused yet, I was till did a little table to help. There more on this on the Raspberry Pi wiki page Low Level Peripherals
For now I will leave you with the diagrams and table bellow, and just know you need to use the RPi.GPIO numbers in python.
This is all ready getting far to long so ill cover the inputs and making something interactive in another post.
|Pin Numbers||RPi.GPIO||Raspberry Pi Name||BCM2835|