Preparations for using GPIO on Raspberry Pi
While I’m waiting for the very last parts for my balloon project I decided to test GPIO interface on RPi. My objective was to light LED diode from a python console. I’ll show you how to that step by step.
There is webpage which really helped me, you can find schematics of GPIO pins there: LINK. There is my RPi with one of these schematics:
Carefully connect one wire to 3v3 output and one to GND. There is simple schematic for that:
Then you need to connect it to LED diode and 100Ω resistor if you are using red diode. Remember that longer leg is cathode (+) and shorter is anode (-). It should light up.
If you are using other type of LED diode you need to check it’s voltage and then do some calculations to select suitable resistor for your configuration. You can use special calculator to do that or count that in old-fashioned way by using that formula:
R = (Vs – Vl) / I
r – resistance that you need
Vs – supply voltage (3.3V for us)
Vl – diode voltage (about 2V)
I – LED current (0.02A)
So let’s calculate this:
R = (3.3V – 2V) / 0.02A = 65 Ω
I used 100Ω for that so my diode is a bit subdued. It’s OK to use value that is higher than outcome of calculations.
If everything is working as described you can move to the next step.
Now we’re going to move one of the wires (this that is providing power do not move the grounding one) to pin 23 (GPIO11), there is schematic for that:
After you do that, your diode shouldn’t be lighting any more. Now we need to prepare Python.
At first we need to install python-dev package so type as root:
apt-get install python2.7-dev
Then download class which is able to control RPi’s GPIO. You can find it there: http://pypi.python.org/pypi/RPi.GPIO/0.3.1a#downloads. Unzip package:
tar – xvf FPi.GPIO-0.3.1.tar
Then get into new catalogue:
and type as root:
python setup.py install
GPIO class will be installed and you will be able to use it inside Python Shell. Let’s test if it is working!
Run Python 2 shell by simply typing:
Be sure that you are using Python 2 not 3! It should be set do P2 on default. You can use for that also dedicated program when x server is started (you’ll find it under Programming section in LXDE menu).
After starting Python Shell your command prompt will change to:
Type there following commands:
import RPi.GPIO as GPIO
Now observe your carefully diode and hold back the excitement! Type that command to light it up:
GPIO.output (23, True)
Congratulations! You just used your RPi to manage a low level peripheral! You are only one step from building a really advanced devices!