We first featured the Raspberry Pi early last year when they were launched. We loved them, but we didn't realise just how much the rest of the world loved them too. We don't think that back then anyone did.
The Raspberry Pi has struck a chord with hobbyists the world over, in a way no other device in recent years has. The initial production run of 10,000 sold out so quickly that only the lucky and dedicated fans got one, and now they're selling as fast as factories can make them.
Frankly, this isn't surprising given that for around £30 you can get a fully working Linux computer featuring an ARM processor and 512MB of RAM (on boards shipped after 15 October) that's the size of a credit card. The goal of the project is to revolutionise out-dated computing education in the UK, but it's too early to see if it will achieve this.
One thing is for sure, though: the world of amateur hardware hacking will never be the same again. These diminutive, but fully functional, systems are perfect for adding processing power to unusual places where space and electricity are at a premium.
They've been sent into space and are being prepared to cross oceans, but are also being used for home brewing beer and driving remote control cars. We're going to look at some cool projects for the Pi and introduce you to the techniques you'll need to turn yours into the device of your dreams.
Thanks to the versatility and depth of Linux tools, it's easy to tune your Pi to be anything from a desktop computer to a media centre or hardware controller.
Distro Guide
As you probably expect, there's a wide range of distros available on the Pi, and new ones seem to pop up every week. Here, we're going to take a look at some of the most popular, as well as a couple of novel ones.
You install a distro in a slightly different way than on a normal computer. Since everything runs off an SD card, all you have to do is write the new operating system to this card.
The easiest way of doing this is with the command-line tool dd. This tool does a bit-for-bit copy of data between a device and a file (or for that matter, two files or two devices).
Distros are supplied as image files (a bit like ISO files for CDs) that can be written to the dis c, after being unzipped if necessary, with:
sudo dd if=<image-file> of=<sd-card-device> bs=4k
sudo sync
The second line ensures that all the data is written to the card and not stuck in any buffers. So, for example, on our test computer, which has two hard drives (sda and sdb), the SD card comes up as dev/sdc. If you aren't sure which device your SD card is, run df â"h at the terminal, and it will list all the devices. You should be able to see which one it is.
To back up your Raspberry Pi setup, you can create a new image file by reversing the if (input file) and of (output file) flags in the dd command. That is:
sudo dd if=<sd-card-device> of=<new-imagefile> bs=4k
This image can then be compressed, using gzip or bzip so it doesn't take up too much hard drive space.
Raspbian
This is the recommended distro by the Raspberry Pi Foundation. Unless you have a good reason to use a different one, it's probably your best bet. It's based on Debian Wheezy, and so you can easily install anything from the huge Debian repositories.
The default desktop environment is LXDE, which is very lightweight, but a little basic for some tastes. Xfce is available for people who like a few more graphical niceties. It has the raspi-config program, which is probably the easiest way of configuring your Pi.
The Raspberry Pi was designed to get children into programming, and Raspbian was designed with this in mind. You'll find Idle (a Python IDE) and Scratch (a programming environment for young children) on the desktop. It's available from: www.raspberrypi.org
Arch Linux
While Raspbian has been created to try t o shield users from the internal setup of the operating system, Arch Linux is designed to help users understand how the system works.
The initial image, available from www.raspberrypi.org, includes just the basic system to get your Pi running and connected to the network. It doesn't include much of the software you may want to use the system such as, for example, a graphical environment. You should find the information you need at the Arch Linux Wiki.
Taking it from this initial state to a working system will require a bit of work, but along the way you'll learn about how the internals of a Linux distro fit together. Whether or not this is worth all the work is, of course, up to you.
Raspbmc
The Raspberry Pi may have been designed as an educational tool, but hobbyists have been pretty quick to make it a toy. This distro is designed to turn your Pi into a media centre that can be used to control your TV.
It's based on XBMC, which allows you to play music and videos that you have as files, or stream them from the internet. The image can be downloaded from: www.raspbmc.com. For details of how to install it and set it up, see the section a little further down the page.
If you have a MythTV back-end set up, you can use XBMCPi to provide a front-end interface. Depending on what you want to play, you may need to purchase the codec packs that provide access to patent-protected video and audio algorithms.
Android
An official version of Android for the Pi is in the works (official in that it is endorsed by the Raspberry Pi Foundation). They have provided videos of it running, but at the time of writing, it wasn't available for download. By the time you read this, that may have changed. Any news will be announced at www.raspberrypi.org.
The community has been working on an unofficial version. The performance isn't as good as the official version will have (the developers describe it as "barely useable"), but it's available now. Check out: www.razdroid.net.
Raspbian
For the majority of people who use it, Raspbian will be the graphical face of the Raspberry Pi. It can be obtained and installed onto an SD card by following the instructions above.
Once it's up and running, it's a good idea to grab the latest versions of all the software by connecting your Pi to the internet, opening a terminal and running:
sudo apt-get update
sudo apt-get upgrade
The killer feature of Raspbian is the raspi-config prog ram. This will start automatically the first time you boot, or can be run at any time by typing sudo raspi-config in a terminal. It has got quite a few options, but the most important are:
expand_rootfs
Because of the way Raspbian is installed, it will only create a 2GB filesystem, so if you have a larger card, any remaining space will remain unused. You can use this option to expand the filesystem to take advantage of any wasted space.
memory_split
The Raspberry Pi uses the same chunk of memory for both the main processor and the graphics chip. Using this option, you can change the amount allocated to each.
overscan
This option can be used on some displays to make the graphics expand to fill the whole screen. You can safely ignore it unless you have problems.
overclock
Get an extra 50 per cent performance at no extra cost! See the belo w for more details.
boot_behaviour
This rather cryptically named option changes whether your Pi boots into a graphical environment or a text one.
The installed software has been kept to a minimum. This is a good idea, but you may find that tools you use on other desktop distros aren't there. Fortunately, as Raspbian is linked to the Debian Armhf repositories, you have access to more software than you're ever likely to need.
If you like using a mouse, you may want to install a graphical package manager. We recommend Synaptic. To install it, type:
sudo apt-get install synaptic
in a terminal. It can then be opened by going to the LXDE menu > Preferences > Synaptic Package Manager. You can then install any software you want.
14 cool Raspberry Pi hacks
1. Convert a Pi into a laptop
2. Use a Kindle as a screen
3. Turned into a super computer (sort of)
4. Control your Arduino from the web
5. Build a synthesiser
6. Roam the night remotely
7. Control a solar power setup
8. Converse with a foreigner
9. Send a payload into (near) space
10. Control a house from the web
11. Brew beer
12. Play old games
13. Learn to build an OS
14. Make a photobooth
Overclocking
The processor at the heart of the Raspberry Pi is designed to run at 700MHz. That is, perform 700,000,000 operations per second.
Of course, 'designed to run' doesn't mean 'has to run'. You can increase this speed. However, doing this will increase the power consump tion, which in turn increases the amount of heat generated. If it gets too hot, you're liable to have a smoking pile of silicone rather than a functional processor.
Fortunately, the latest version of Raspbian (2012-9-18) includes a tool to help you ramp up the speed while keeping an eye on the temperature. Since this is an official tool, using it won't void your warrantee (unlike earlier unofficial methods).
Overclocking your Pi is simply a matter of running sudo raspi-config and selecting Overclocking. There are a few options to choose from, depending on how brave you're feeling.
If you find that your Pi becomes unstable, reboot with the Shift key held down to disable overclocking, then change the option in raspiconfig. The maximum setting should give you a whopping 50% extra speed, which we found makes a real difference to the desktop user experience, especially for web browsing.
If you want to keep an eye on your core temperature, you can add t he Temperature widget to the LXDE panel. However, your Pi will automatically turn off overclocking once it reaches 85 degrees C.
RaspBMC
You can install a media player, such as VLC, onto Raspbian, and use that to play videos. This works fine if you're using your Pi as a general computer and giving it occasional multimedia functions. However, the small size of the hardware, and the fact that it runs silently, makes it a good choice for building your own entertainment centre.
You could start from Raspbian and customise it to your needs, and this is a good idea if you have any unusual functions in mind. However, here at Linux Format, we're a bit lazy, so we like to steal other people's hard work rat her than do it ourselves.
Fortunately for us (and you), a team of hackers have done all the difficult bits of building a media centre for the Raspberry Pi, and packaged it for you to use. Get the installer from here, and copy it onto your Pi (you'll need a working Raspbian to start from). Then you just need to run: sudo python install.py to download and set up the distro.
This will wipe your SD card, so make sure you've backed up any important files elsewhere before running it. Once the installer's finished running, reboot the system. You'll notice the difference as soon as the graphical environment starts.
This uses the popular XBMC media desktop, which is quite different from LXDE, which you may have experienced on Raspbian. You can play media stored locally or, through the use of add-ons, stream content from the internet. Music and videos can be adde d to your setup either by attaching a USB device, or directly onto the SD card using FTP (username pi, password raspberry). See below for how to find the IP address.
Perhaps the only piece of configuration you will need is to make sure the sound is sent to the right place. Under System > System > Audio Output make sure that Output is set to Analog if you're using the jack, or HDMI if you're using this.
This is all good, but it's a bit impractical to use a mouse and keyboard to control your telly. The good folks at XBMC thought of this and added support for remote controls. The simplest and most general-purpose way of doing this is via RaspBMC's web interface. Using this, any devic e with a web browser that's on the same network as the Pi can control the playback.
This is enabled by default, so all you need to do is find out the IP address of the Pi in System > System Info (you should be able to set this to be a static IP on your router). Then, on any other device attached to a network, open a browser and point it to that IP.
If you're a modern sort, and using an Android or iOS mobile device, then you should find a selection of apps in the various app stores that can do this with a nicer interface than the HTML pages.
We found that Raspbian drew all the current our power supply could deliver. If you attach a few peripherals, and find that it becomes a little unstable, this could be the reason. The best way of mitigating this is to attach all USB devices through a powered hub.
Taking it further
It is possible to take complete control of your TV viewing using Linux, including watching live TV, and recording shows for later. This can be done using MythTV.
You'll need a separate computer with the appropriate cable connections to act as the server. A word of warning, though: MythTV is renowned for its pernickety installation. The stresses of this procedure are responsible for more than a few of LXF editor Graham Morrison's grey hairs.
You can play video files that you have stored on other computers on your network, for example those on a Network Attached Storage (NAS) box. The exact method for doing this will vary depending on how you share the files, but they are configured through the Add Sources buttons. For more information on this, check out the wiki.
Camera controller
Back up your photos using your Pi
The size of the Raspberry Pi means we can use it to take control of other embedded devices. This may seem a little redundant - the embedded devices obviously have some form of controller already - but it means we can script and extend them in ways that aren't possible (or are, at least, very hard) without the extra device.
Almost anything that you can plug in to a normal desktop can be scripted by a Pi, but we're going to look at cameras for a couple of reasons. Firstly, there's support for most in Linux, and secondly there's a range of useful projects you can do once you've grasped the basics.
The best command-line tool for manipulating cameras in Linux is Gphoto2. Get it with:
apt-get install gphoto2
Before getting stuck in to the project, we'll take a look at t his useful tool to see what it can do.
The desktop environment can try to mount the camera, and this can cause Gphoto2 a few problems, so the easiest thing to do is run without it. Open a terminal and run sudo rasbpi-conf, and under Boot Behaviour, select No to not start the windowing system, then reboot.
On our test system, we found that running this way, we could just run everything off the Pi's power supply, but if we tried to use a mouse as well, we needed to upgrade to a powered hub. Obviously, this will depend on the particulars of your peripherals and power supply.
In the new text-only environment, plug in your camera and run:
gphoto2 --auto-detect
This will try to find any cameras attached to the Pi. Hopefully, it will pick up yours. While it does support an impressive array, there are a few cameras that won't work. If yours is one of the unlucky few, you'll need to beg, steal or borrow one from a friend before cont inuing.
Not all supported cameras are equal, and the next step is to see what the camera can do. To list the available actions, run:
gphoto2 --auto-detect --abilities
There are, broadly speaking, two main classes of abilities: capture, and upload/download. The former let you take photos with your scripts, and are present mostly on higher-quality cameras. The latter let you deal with photos stored on the memory card, and are present on most supported cameras. In this project, we'll deal only with the second set of abilities.
The simplest command we can send to the camera is to get all the photos stored on it. This is:
gphoto2 --auto-detect --get-all-files
Running this will download all the files from the camera into the current directory. This would be fine on a normal computer, but you may not want to do it on a Pi, as you run the risk of filling up your memory card pretty quickly. Instead, we'll cop y them onto a USB stick.
To do this in an interactive session, you could simply use a GUI tool to mount the stick then run df -h to see where the USB stick is mounted, and cd to the directory. However, since this will run automatically, we need to know where the device will be.
There are a few ways of doing this, but we'll keep it simple. We'll mount the first partition of the first serial disk, and store the photos there. Here, we're assuming that you're using the default user pi. If you're not, you'll need to adjust the script.
First, we need to create a mount point for the drive. This is just a folder, and can be put anywhere - we're going to spurn convention and put it in our home folder. So before running the script, run:
mkdir /home/pi/pic_mount
With this done, we're ready to go. The script to mount the drive and get the photos is:
#!/bin/bash
if mount /dev/ sda1 /home/pi/pic_mount ; then
echo "Partition mounted"
cd /home/pi/pic_mount
yes 'n' | gphoto2 -- auto-detect --get-all-files
umount /dev/sda1
else
echo "/dev/sda1 could not be mounted"
fi
yes 'n' is a command that simply emits a stream of n characters. This means that when Gphoto2 prompts to overwrite any previously downloaded files, it will decline. The umount is essential, because it ensures that the drive is properly sync'd and can be removed.
We've called the script get-pics.sh, and saved it in Pi's home directory. To make it executable, run:
chmod +x /home/pi/get-pics.sh
You should now be able to run it manually. You'll need to use sudo because it needs to mount the drive.
The final piece of the puzzle is to get the script to run automatically. To do this we add it to the file /etc/rc.local. This script runs when you boot up, and it executes as root, so there's no need to worry about permissions.
Just open the file with a text editor as root. For example, with sudo nano /etc/rc.local, and add the line:
/home/pi/get-pics.sh
///end code///
just before the line exit 0.
Now all you have to do is plug in your camera (making sure it's turned on) and USB stick, and it'll back up your photos when you boot up.
Taking it further
If you want to run the device headless, as will most likely be the case, you could attach LEDs to the GPIO pins, as shown elsewhere in this article, and use these to indicate statuses. As well as saving them to the USB stick, you could upload them to an online service, such as flickr. See the section on wireless networking for information on how to connect your Pi to your phone.
You could include some sort of switch to tell your Pi which photos to upload, and which to store on the USB stick, for example upload low-resolution images, and store high-res ones. Or you could create low-resolution versions of the images, and upload those.
Gphoto2 has far more capabilities than we use here, including bindings for Java and Python. For full details, check out the project website: www.gphoto.org
Of course, you don't have to stop there. If you have a wireless dongle in your Pi, you could use it to run a HTTP server. With some PHP (or other web language) scripting, you should be able to create an interface to GPhoto2 that will allow you to connect from your mobile phone.
Taking it in a different directi on, if your camera supports capture options, you could use your Pi to take photos as well as copy them.
Powering your Pi
The Raspberry Pi gets power from its micro USB port. This supplies 5V, and the Raspberry Pi foundation recommends an available current of at least 700mA. This can easily be delivered via a mains adapter, or a USB cable from a computer.
If you want your Pi to be portable, then there are other options. Four AA batteries should provide enough power, provided you have the appropriate housing and cables to get the power into the micro USB port.
However, we found the best solution was to get a backup power supply for a mobile phone that plugs directly into the Pi.
Ohm's Law
There are two key ways of measuring electricity: voltage and current. Voltage (measured in volts) is the amount of energy a given quantity of electrons has, while current (measured in amps) is the amount of electrons flowing past a point. The two are intimately connected by Ohm's law which states: Voltage = Current * Resistance, or V=IR.
You can use this connection to make sure you don't accidentally toast your Pi by pushing too much current into it. The exact setup of the Pi is a little complex. If you wish to delve into it, Gert van Loo (one of the designers) has put together an explanation, which can be found at: http://bit.ly/Qp4PMl
As a rough rule of thumb, you can expect to draw voltage out of a GPIO pin at 3.3v, and you shouldn't draw more than 16mA, or push more than this into an input pin. This is the maximum current, you should aim to use less.
So, with Ohm's law we know V=IR, so R=V/I. If we put in the data from the Pi, and want to ensure we don't damage it, we know that R must be greater than 3.3/0.016, which is 206.25 Ohms. Remember, this is the smallest amount of resistance it's safe to use with a GPIO output.
You should aim for a margin of safety several times above this unl ess absolutely necessary. In our circuits, we've used 1000Ohms, which gives us a safety factor of almost 5.
Networking
The Raspberry Pi comes with a wired Ethernet connection, which is fine for most occasions, but sometimes the cable just won't reach. You could use a USB wireless dongle. However, if you've got an Android phone, and your carrier hasn't disabled the feature, you can use this as your networking device.
This has an extra advantage of not drawing as much power from the Pi, and so makes it easier when running from batteries. You should be able to share your phone's connection to Wi-Fi as well as 3G, so it won't necessarily eat into your data allowance.
Of course, it's best to check the connection type before downloading large files. To do this, connect your phone to your Pi, and enable tethering in Settings > Wireless and Networks > Tethering and Portable Hotspots (on the phone).
Back on the Pi, if you type sudo ifconfig, you should then see the interface usb0 listed, but it won't have an IP address. Networking interfaces are controlled by the /etc/network/interfaces file. By default, there isn't an entry in here for USB networking, so we need to set one up.
Open the file with your favourite text editor as sudo. For example, with sudo nano /etc/network/interfaces and add the lines:
iface usb0 inet dhcp
nameserver 208.67.220.220
nameserver 208.67.222.222
This used the OpenDNS nameservers, but you could use others if you wish. You can now either restart the interfaces or restart your Pi to pick up the changes. You should have an internet connection up and running.
Sending output
Use the GPIO pins to light up some LEDs
The Raspberry Pi's diminutive size makes it ideal for making your own embedded devices. This can be a great way of creating small computing devices to solve specific problems, like we saw with the camera controller.
However, there is the slight problem that it can be hard to know what's going on inside your Pi without a screen. Fortunately, the designers of the Pi thought of this problem and have added the facility to get information on and off a Pi without the bulk of usual PC peripherals. This is done via General Purpose Input and Output (GPIO).
You may have wondered what the spiky pins near the SD card reader are for - well, you're about to find out. This basic circuit can be used to display information from any source, but here we're going to use it to solve a problem we often have at LXF towers - finding the final byte of the IP address.
T his is useful if you want to remotely access your Pi, but can't configure it with a static IP because, for example, you have to move it between networks. Typically, you can find out the first three bytes from the netmask, but the final one can be elusive unless you have a monitor. We're going to use the gpio program, which is part of WiringPi. You can find out more about this from the WiringPi website.
It comes as source code, so we'll have to unzip it and compile it with:
tar xvf wiringPi.tgz
cd wiringPi/wiringPi
make
sudo make install
cd ../gpio
make
sudo make install
We'll also use bc, so install it with:
sudo apt-get install bc
Now, that's enough about software - on with the hardware! Just a quick word of warning before we start: it is possible to break your Pi by connecting the wron g wires together, so make sure you double check before powering up.
The circuit for this is very simple: you just have to connect each output to the positive leg of an LED, then the negative leg of the LED (shorter) to a 1KOhm resistor, and finally the other leg of the resistor to the common ground. See figures 1, 2 and 3 for details. Once you have your fully-set-up board connected to your Pi, you can make things happen.
To start with, we'll just use the final pin. This is pin 7 (the layout of the pins doesn't follow a numbering pattern). Open up a terminal, and set it to output with:
gpio â"g mode 7 out < /p>
Then you can turn it on with:
gpio â"g write 7 1 and off again with: gpio â"g write 7 0
If you're like us, you'll do that repeatedly until the novelty of it wears off.
Once it has, you're ready to run the script. It contains four parts. The first just sets the pins to the right mode and makes sure they're turned off:
pins="7 8 25 24 23 18 15 14"
for x in $ pins
do
gpio -g mode $ x out
gpio -g write $ x 0
done
The second grabs the IP address from ifconfig, converts it to binary, then pads it out with leading zeros, if necessary.
ipaddress='ifconfig eth0 | grep 'inet ' | awk '{print $ 2}' | cut -f4 -d'.''
binary='echo "ibase=10;obase=2;$ ipaddress" | bc'
paddedBinary='printf %08d $ binary'
The next part uses cut to extract the part we want from this binary string and outputs it to the appropriate pin.
bit=1
for x in $ pins
do
out='echo $ paddedBinary | cut -b$ bit'
gpio -g write $ x $ out
bit=$ ((bit+1))
done
And, finally, we tell the script to sleep for five minutes, then turn the LEDs off.
sleep 5m
for x in $ pins
do
gpio -g write $ x 0
done
That's all there is to it!
Create the showIP.sh script, make it executable with: chmod a+x showIP.sh and type sudo ./showIP.sh to display your IP. To get this run automatically on boot, you just need to add the line:
/home/pi/showIP.sh &
to rc.local. See the Camera Controller section for details on how to do this.
Need more input
We've shown you how to send output via the GPIO, but as the name suggests, they can also receive input. With this, it's even more important to ensure you don't send too much power into the pins.
To get input, just set the mode to input with gpio â"g mode <pin number> in then read the value with gpio â"g read <pin number>.
This hardware can display any eight bits of information, so you don't have to limit it to displaying just IP addresses. For example, you could make a modified version of the camera controller script to use the LEDs to indicate its progress. You can find details on the full selection of GPIO pins here.
The pins we've used are the same in both revision 1 and 2 of the Raspberry Pi, but some have changed between the two versions. If you design your own circuits, or use ones off the web, make sure you use the right pins for your board.
You don't have to limit yourself to just switching pins on and off. The Pi supports a few methods of passing larger amounts of data through the GPIO. The two most common of these are Serial Peripheral Interface bus (SPI) and Inter-Intergrated Circuit (I2C).
There are a number of devices available that use these, and plenty of information online to help get you started. So what's stopping you? Get out your soldering iron and build a robot army.
Gertboards and Arduinos
Connecting directly to your Pi's GPIO pins can provide you with basic input and output control, but there are limitations. There are two additional items that you can obtain to help you interact more precisely with the world around you.
The Gertboard is a fairly complete expansion pack for connecting between your Pi and the real world, including a micro controller, and a range of input and output options. It comes as an unassembled kit, so you will have to get your hands on a soldering iron to put it together.
Meanwhile, the Arduino is a micro controller that can connect to your Pi (or any other computer) via the USB port. Typically, it comes assembled, but kit forms are also available. In its raw form, it has fewer features than the Gertboard (which includes an Arduino microcontroller), but it can be expanded with a huge range of shields.
No comments:
Post a Comment