Components, kits and expansion
The Raspberry Pi is supplied as a circuit board only – chips can be damaged by static electricity and housing it in an enclosure is a sensible precaution; this will also allow it to be placed on metallic surfaces without fear of short-circuiting the board. There are many cases to choose from, and it's largely a matter of taste, but ease of access to ports is one important consideration.
A breadboard allows circuits to be prototyped without the need for soldering and, when combined with a breakout cable, provides a low cost platform for hardware experimentation. Breadboards can be purchased in varying sizes and breakout kits such as Adafruit's Pi Cobbler provide everything that is required for connecting these up to the Pi. Components are then simply inserted into the solderless contacts of the breadboard in order to construct circuits and connect these up to GPIO.
Source: Adafruit Industries CC BY-NC-SA 2.0
Shorting or overdriving GPIO could be catastrophic and, as previously mentioned, it's generally advisable to protect this when experimenting. More comprehensive kits such as the Starter Kit-B from SK Pang Electronics include I/O expander chips along with other staples such as buttons, LEDs and resistors. The I/O expander interfaces with the Raspberry Pi via the I2C bus and uses up just 2 pins of GPIO. Typically it provides 8 or 16 channels of I/O where mistakes should limit damage to the expander, which itself would be much cheaper to replace than the Pi.
Source: Freeburn Robotics Ltd
Expansion boards provide a head start for extending the Raspberry Pi and combine additional I/O with commonly used components such as buttons and LEDs. The Gertboard from the designer of the Raspberry Pi alpha hardware, Gert van Loo, was perhaps the most eagerly anticipated of these and includes an Arduino microcontroller and analogue-to-digital and digital-to-analogue converters. Most expansion boards are designed to support a broad range of uses, but one exception is the PiBorg which is intended for use in robotics and can directly control motors and monitor them to work out the torque and their speed.
Writing software
Software to access GPIO can be written in any one of a number of languages and even shell scripts can be used to set and sample the state of pins.
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction
echo "1" > /sys/class/gpio/gpio4/value
Shell commands to enable GPIO in sysfs, configure a pin as an output and set it to logic high
public static void main(String args) {
GpioGateway gpio = new GpioGatewayImpl();
//set up the GPIO channels - one input and one output
gpio.setup(Boardpin.PIN11_GPIO17, Direction.IN);
gpio.setup(Boardpin.PIN12_GPIO18, Direction.OUT);
// input from pin 11
boolean input_value = gpio.getValue(Boardpin.PIN11_GPIO17);
// output to pin 12
gpio.setValue(Boardpin.PIN12_GPIO18, true);
}
Java GPIO example using rpi-gpio-java (eLinux.org, CC BY SA 3.0)
The C language is a favourite of embedded developers and the Raspberry Pi community wiki provides two code examples: one that directly manipulates the GPIO register and another that makes use of a C library for the BCM2835 system-on-a-chip (SoC) used by the Raspberry Pi.
Source: © SK Pang Electronics Ltd
Python is the official educational language of the Raspberry Pi Foundation and GPIO support is provided via the RPi.GPIO module. Those with experience of Arduino development may wish to make use of the WiringPi library which replicates core I/O functions found in the Arduino development environment.
The more adventurous could even try their hand at ARM assembly language programming – the University of Cambridge Computer Laboratory has published an entire Raspberry-Pi-based operating systems development course under a Creative Commons licence. This takes the student all the way from writing code to control an LED, to driving a display and using a USB library to take keyboard input.
Next: A hardware hacking Hello World