A commonly used hardware-hacking "Hello World" is to blink an LED. The example described here uses Python and the software dependencies can be installed with the following commands.
$ sudo apt-get install python python-dev python-pip
$ sudo easy_install -U distribute
$ sudo pip install rpi.gpio
The code that follows can either be typed into the Python interpreter interactively or entered into a file which is then executed by Python. In either case Python will need to be run as root in order to access GPIO, ie type
sudo python and enter commands, or enter them into a file, save this and execute it with
sudo python filename.py.
First we need to import the RPi.GPIO module along with one that provides time-related functions.
import RPi.GPIO as GPIO
Source: Gordon Henderson Next we tell the GPIO module that we would like to identify GPIO pins by the labels used in the Broadcom documentation (the alternative is to identify them by P1 pin number).
We then set GPIO17 to be an output.
Finally creating a loop inside which we set the GPIO to be logic high, pause for one second, set it to be logic low, pause for another second and then repeat until the program is interrupted by CTRL-C.
while True :
Note that this example does not use any protection/buffering of the GPIO and damage is possible if GPIO17 were shorted-circuited or otherwise presented with a load of too low a resistance, etc.
Comparisons with Arduino
Care must be taken when comparing the Raspberry Pi with Arduino, as, while they are both hacker-friendly embedded platforms, they were created with quite different purposes in mind. Arduino provides an integrated hardware and software development environment that is designed for non-programmers and with a focus on ease of use and extensibility. Whereas the Raspberry Pi is aimed at teaching computer science and provides a more powerful Linux-capable system.
The Pi may use an energy-efficient ARM processor and consume much less power than a laptop, but with Linux booted and idle it will draw a few watts compared to the microwatts drawn by the Arduino's ATmega processor in sleep mode. As such, Arduino is the obvious choice for use in battery-powered wireless sensor nodes, for example. The Raspberry Pi is better suited to applications which benefit from the Linux ecosystem, a faster processor and much more memory, etc.
Other considerations include the fact that the Arduino is a prototyping platform, its hardware designs are open source, and the ATmega processors and comprehensive documentation for them are widely available. In contrast the Pi is designed as an end user product and the Broadcom SoC and accompanying detailed technical documentation are, at the time of writing, only available to very high volume customers and under strict NDA.
Embedded systems can be found in so many things that we take for granted, and with the Internet of Things on the horizon, such embedded systems are set to become even more critical to everyday life; the widespread interest generated by the Raspberry Pi couldn't be more timely.
Software developers, both new and experienced, are discovering that basic hardware hacking is far from magic and are able to choose from a rapidly growing selection of accessories, kits and guides that support experimentation.
Raspberry Pi is by no means, as some have excitedly proclaimed, an "Arduino killer", and, as is frequently the case, it's a question of the right tool for the job.
Andrew Back (@9600) is a freelance consultant who originally trained as an electronics engineer and first used Linux in the mid-90s. He has since worked at BT's open source innovation unit, Osmosoft, founded the Open Source Hardware User Group, and more recently co-founded SolderPad – a place to collaborate on electronic design.