|Example of a DIY circuit, connected by DIY cable|
One feature that has contributed to the Raspberry Pi's success is the possibility of interfacing the virtual (software) with the physical world. We are speaking of course of the "General Purpose Input and Output" pins, more commonly known as GPIO. They are available on the Pi at the P1 connector.
Making a GPIO cable
We are doing some green computing today! We are going to recycle some old computer cable and make a Raspberry Pi interface cable with it. We will then be able to connect motors, LEDs, buttons and other physical components.
The interface cable is a flat cable with 26 conductors. It is quite similar to an IDE hard disk cable (or ATA) with 40 conductors (avoid the ATA66/133 with 80 conductors):
|Original IDE/ATA cable with 40 wires|
Let's get to work
We will only need 2 connectors on our interface cable and not 3. With a cable with 3 connectors, we just need to cut one section off.
|Cutting it with a pair of scissors|
Before doing the cutting, we will do some marking. The reason is that we only need 26 wires, and we have 40. With the red wire on the left, we have to count 26 wires and mark the split with a fine permanent marker. We count on the right side to make sure we do have 14 conductors, not a single one more or less.
|Using a permanent marker to write|
|splitting in two parts|
We then have to cut one section of the connectors off, with a small saw, such as a metal saw (or a Dremel style cutting wheel).
|we need to cut on the 7th column from the right|
We remove the top part, then the cable section with 14 wires, and finally, after notching it, we remove the bottom part.
|almost done, just remove the part on the right|
We are done with the cutting. We can now connect the cable to the Raspberry Pi. The red wire is closest to the SD card side, and farthest from the RCA video out (yellow connector):
With the cable ready, we are now going to test it. Let's add 2 LEDs to do this test. We will use a red and a green LED, but you can use amber or yellow LEDs too. Blue, violet or white LEDs will not work, since they need more voltage.
The connection is really simple:
Red LED and green LED, short leg -> third hole on the left.
Red LED, long leg -> second hole on the right
Green LED, long leg -> third hole on the right
Python CodeFirst thing first, you have to get the RPi.GPIO Python module. It is a module that will allow us to control the GPIO of the Raspberry Pi. On Raspbian, it is now included, but for another version of Linux, it can be installed with
sudo easy_install RPi.GPIO
Or through apt-get (or equivalent package manager):
$ sudo apt-get install python-rpi.gpio
Here is the code:
#!/usr/bin/env python """ Setting up two pins for output """ import RPi.GPIO as gpio import time PINR = 0 # this should be 2 on a V2 RPi PING = 1 # this should be 3 on a V2 RPi gpio.setmode(gpio.BCM) # broadcom mode gpio.setup(PINR, gpio.OUT) # set up red LED pin to OUTput gpio.setup(PING, gpio.OUT) # set up green LED pin to OUTput
#Make the two LED flash on and off forever try: while True: gpio.output(PINR, gpio.HIGH) gpio.output(PING, gpio.LOW) time.sleep(1) gpio.output(PINR, gpio.LOW) gpio.output(PING, gpio.HIGH) time.sleep(1) except KeyboardInterrupt: gpio.cleanup()
Just save the code into a file named flashled.py.
- PINR is the GPIO for the red LED (0 for Rpi V1 and 2 for V2)
- PING is the GPIO for the green LED (1 for Rpi V1 and 3 for V2)
Running the code
Usually, a LED should be protected with a resistor to limit the current going through it, but in this case it will blink intermittently, just to test the cable, so we don't need any.
For continuous use, it is recommended to put a resistor in series (about 220 Ohm to 360 Ohm).
Before we can run the program, we have to make it executable:
CTRL-C will stop the program.$ chmod +x flashled.py $ sudo ./flashled.py
The follow up to this will be about making our own breadboard adapter, and having fun with a transistor.