The GPIO pins of a processor are useful for controlling external electronic circuitry. One of the simplest things you can do with these pins is write code, which will output a logic high (3.3V) or low (0V) on them. The Raspberry Pi has a set of 26 pins (arranged as a 2×13 strip), which bring out a few of the GPIO pins of the processor on board; you can identify these pins by looking for label ‘P1’ on the board. The pins on this connector are numbered P1-01, P1-02, P1-03 and so on. Check out http://elinux.org/RPi_Low-level_peripherals for a diagram of this connector and details of the I/O pins. (You will note that certain pins are labelled as ‘do not connect.’ These pins should not be used for interfacing.)
There are many ways in which you can output voltages on these pins. One of the easiest ways is to use a Python library. The Raspberry Pi Python GPIO access library can be downloaded from http://pypi.python.org/pypi/RPi.GPIO. It is available as a tar file, RPi.GPIO-0.2.0.tar.gz. (Don’t worry if the version number you are seeing on the website is different from this; you might be getting a newer version). Installing the module is simple: untar it and run the set-up script (as super user):
tar xvf RPi.GPIO-0.2.0.tar.gz
cd Rpi.GPIO-0.2.0
python2 setup.py install
Connect a red LED in series with a 1-kilo-ohm resistor between pins P1-08 (GPIO pin) and P1-06 (GND) and run the following program:
import RPi.GPIO as GPIO
import time
GPIO.setup(8, GPIO.OUT)
while True:
GPIO.output(8, True) # LED ON
time.sleep(1)
GPIO.output(8, False) # LED OFF
time.sleep(1)
We configure pin P1-08 as an output pin using GPIO.setup and write high/low values to it using the function GPIO.output.
Internally, the Python GPIO library uses special files under ‘/sys’ directory to access the I/O pins. The disadvantage with this method is that you really can’t do timing sensitive operations this way.
Streaming video from a webcam
What role can Raspberry Pi play in the design of your embedded system?
If your application needs fast media processing and networking, you can replace your PC with a Raspberry Pi, which consumes considerably less power (and comes in a much smaller form factor—the size of a credit card). If you need to do timing sensitive operations (like measurement of the time delay between low to high transitions of an I/O pin to microsecond-level precision), you can use something like an Arduino and interface it with the Raspberry Pi.
Here is an example. Say, you wish to capture the video stream from your webcam and make it available over the network. The first step is to connect the webcam and see whether the Raspberry Pi recognises it—if you see a file called ‘video0’ under ‘/dev’, everything should work well!
Next, you can install the popular ‘ffmpeg’ package, which does a whole lot of video manipulations including streaming:
pacman -S ffmpeg
Let’s now create a small configuration file called ‘ffserv.conf.’ This file supplies information like the network port at which the stream is available, the name of the stream, etc:
Port 8010
BindAddress 0.0.0.0
MaxClients 10
MaxBandwidth 1000
File /tmp/webcam.ffm
FileMaxSize 5M
Feed feed1.ffm
Format mpjpeg
VideoSize 160×128
VideoFrameRate 3
VideoIntraOnly
Noaudio
Strict –1
Check out http://ffmpeg.org/sample.html for more information on the parameters in this configuration file. Streaming of the video is done by a program called ‘ffserver.’ Invoke it like this:
ffserver -f ./ffserv.conf
‘ffserver’ automatically goes into the background. Now you have to run ‘ffmpeg’ to capture data from the webcam and supply it to ‘ffserver’:
ffmpeg -v 2 -r 5 -s 160×128 -f video4linux2 -i /dev/video0 http://localhost:8010/feed1.ffm
You can open the browser on a remote machine and enter the following URL to watch a streaming video: http://192.168.1.5:8010/feed1.mjpeg. (You have to replace the address 192.168.1.5 with the IP address assigned to your Raspberry Pi’s Ethernet interface.)
Move your webcam with a servo motor!
You can mount your webcam on the shaft of a hobby servo motor and control it using the Raspberry Pi! The easiest way to do this is to run the servo control code on an Arduino and send commands to the Arduino from the Raspberry Pi. When the Arduino is connected to the USB port of the Raspberry Pi, the serial port on the Arduino is accessible as a special file (usually ‘/dev/ttyACM0’) on the Raspberry. The Python module ‘pyserial’ can be used to write the code, which sends and receives commands over this serial channel. (In our case, the ‘command’ may be simply a number that indicates the angle at which the servo shaft should be positioned.)
The servo control code as well as the Python code running on the Raspberry are available for download at: https://github.com/rlabs/rpi-article.
The start of a revolution!
The Raspberry Pi revolution has just begun. This tiny credit-card sized GNU/Linux system has generated tremendous enthusiasm among hobbyists and ‘do it yourself’ fans all over the world. You can already see people coming up with amazing ideas like the ‘fishpi’ (http://fishpi.org/)—an autonomous Raspberry Pi controlled marine vehicle that is planned to make a journey across the Atlantic unaided!
The author is founder of recursive-labs.com, an online learning start-up focussed on free software-based technologies