Tuesday, 9 April 2013

Fire up your Raspberry Pi

I was loaned a Raspberry Pi.  It was brand new and still in its box when I got it.  This post is about how I breathed life into it. There are two main jobs to do: preparing an SD Card to be the Raspberry Pi's hard-drive and complete the initial setup.

I used my MacBook Air to do this so the process is Mac specific in some areas i.e. using Terminal.

There are five steps to this tutorial:Get Wheezy Raspbian operating systemPrepare the SD Card
Write the Operating System to the SD Card
Power up the Raspberry Pi
Initial Configuration

Step 1 Get Wheezy Raspbian operating system

Wheezy Raspbian is a version of the Linux operating system for the Raspberry Pi and although there are others this is a nice general version to get going with.  It can be downloaded for free but must be written to the SD Card in a special way - a bit like writing a CD.

I downloaded 2013-02-09-wheezy-raspbian.zip.torrent from http://www.raspberrypi.org/downloads and matched the checksum listed on the website to this local download found by running in Mac Terminal:
shasum <path>/2013-02-09-wheezy-raspbian.zip 

The downloaded file is an image which must be written to the SD Card. I followed instructions at http://elinux.org/RPi_Easy_SD_Card_Setup 'Copying an image to the SD card in Mac OS X (command line)'.

Unzip the ~500MB file to produce the ~2GB image that you need to install on the SD card.

Step 2 Prepare the SD card

Before you insert your SD Card into your Mac we need to list disks already present. Then we plug in the SD card (via a card reader if necessary) and list the disks again to identify the name of the SD Card with absolute certainty. The following shows how I did this using the command df in Mac Terminal:
sh-3.2#df -h
Filesystem       Size   Used  Avail Capacity  Mounted on

/dev/disk0s2     112Gi   95Gi   17Gi    85%   /
devfs            185Ki  185Ki    0Bi   100%   /dev
map -hosts       0Bi    0Bi    0Bi   100%     /net
map auto_home    0Bi    0Bi    0Bi   100%     /home
localhost:/fiqGThwQn7mZ2TDK9XyPN2  112Gi  112Gi    0Bi   100%    /Volumes/MobileBackups

sh-3.2#df -h
Filesystem       Size   Used  Avail Capacity  Mounted on
/dev/disk0s2     112Gi   95Gi   17Gi    85%   /
devfs  187Ki     187Ki    0Bi   100%          /dev
map -hosts       0Bi    0Bi    0Bi   100%     /net
map auto_home    0Bi    0Bi    0Bi   100%     /home
localhost:/fiqGThwQn7mZ2TDK9XyPN2 112Gi  112Gi    0Bi   100%    /Volumes/MobileBackups
/dev/disk3s1     15Gi  2.3Mi   15Gi     1%    /Volumes/NO NAME 1

This shows the SD card's logical name, in this case is /dev/disk3s1 (disk3 is the whole disk, s1 is a partition). Make a very careful note of this name and then disconnect the SD card from your computer to prepare for writing by unmounting it - a bit like ejecting a USB stick in Windows/OSX:
diskutil unmount /dev/disk3s1
Volume NO NAME on disk3s1 unmounted

Step 3 Write the Operating System to the SD Card

Now to write the image file to the SD Card using the dd command from the directory where the .img file was put. The dd command is explained well http://en.wikipedia.org/wiki/Dd_(Unix). Wait for a few minutes for the image to be written to the SD card, then eject it.  The commands you need are as follows, notice the output file ('of') has changed now the SDCard has been unmounted, before it was disk3s1, now it is rdisk3. Do a similar conversion with whatever name your SDCard was given.

sh-3.2# dd bs=1m if=2013-02-09-wheezy-raspbian.img of=/dev/rdisk3
1850+0 records in
1850+0 records out
1939865600 bytes transferred in 372.939214 secs (5201560 bytes/sec)

sh-3.2# diskutil eject /dev/rdisk3
Disk /dev/rdisk3 ejected

Step 4 Power up the Raspberry Pi

The SD card you have just written is now a Raspberry Pi 'hard-drive' with a Linux operating system installed. Inserted the card into the slot on the bottom of the Raspberry Pi.  Connect a keyboard, mouse, a screen (HDMI or Composite video) and finally power from a micro-USB cable to your Raspberry Pi.  I found a good guide to this process here http://elinux.org/RPi_Hardware_Basic_Setup

Sit back and watch your monitor as the RPi boots up for the first time! Only one more step to go...

Step 5 Initial configuration

The first time you power up the raspi-config tool is loaded automatically.  This allows you to set up some useful attributes http://elinux.org/RPi_raspi-config is a good source for help.  The important settings are expand_rootfs to allow the Raspberry Pi to use the whole SD Card, overscan set to enabled, locale and timezone, and finally update if you are connected to the internet.

Once finished you are asked if OK to reboot the RPi. This time it boots through to the Linux command line and asks for your login.
raspberrypi login: pi
Password: raspberry (this text is hidden and the cursor doesn't move when you type it in)

Some useful commands

Find IP address, MAC (HWaddr) address type ifconfig into a Terminal
To restart raspi-config issue the command sudo raspi-config.
To shutdown the raspberry pi: sudo shutdown -halt
To reboot the raspberry pi: sudo reboot
To start the Raspbian graphical desktop: startx

That's it, my Raspberry Pi was now up and running a waiting for some inspiration to put it to good use.