Chapter 1
Ge t t i ng U p and R u nni ng
WELCOME TO Learning Python with Raspberry Pi. In this book, you’ll learn how to unlock
the full power of the tiny computer, from 3D graphics to games programming to controlling
electronics to tweeting. You’ll see what’s going on under the hood and learn how to create
programs that take advantage of every feature of this minuscule computer.
Setting Up Your Raspberry Pi
To follow this book, you’ll need a few bits of equipment:
■ Raspberry Pi
■ USB keyboard
■ USB mouse
■ SD card
■ Monitor
■ Power supply
There are also a few optional bits of kit that may help:
■ Powered USB hub (highly recommended)
■ Camera module
■ USB webcam
■ USB WiFi dongle
It is possible to do everything in this book with a model A Raspberry Pi. The real advantage
of a model B as far as programming is concerned is the network port. This port will make it
easier to connect to the Internet, which you’ll need to do to install some software.
6 LEARNING PYTHON WITH RASPBERRY PI
Any USB keyboard and mouse should work fine. Most SD cards should work, although there
are a few that will cause problems. If you’re unsure, buy one from a Raspberry Pi online shop
(there are links to a few on [Link]
The Raspberry Pi has a HDMI (high-definition multimedia interface) video output, but most
monitors have VGA or DVI input. If at all possible, use a monitor that has DVI or HDMI
input. A HDMI-to-DVI converter should cost only a few pounds/dollars and shouldn’t
detract from the image quality. HDMI-to-VGA converters are available, but they’re more
expensive and can cause problems, so use them only if you have no other option.
Most micro USB power supplies from reputable manufacturers should work; however, some
cheap ones from no-name companies have caused problems, so if possible, don’t skimp too
much on this. You could use a USB cable from a normal computer to power your Pi.
Powered USB hubs are recommended for the power-related problems described later in this
chapter. Not all USB hubs are powered, so make sure that whatever one you get plugs into
the mains electricity to get extra power.
We talk more about camera options in Chapter 9 on multimedia. The only thing to say here
is that if you do choose to get a USB webcam, make sure it’s compatible with the Raspberry
Pi. There’s a partial list of working web cams at [Link]
You’ll need to connect your Pi to the Internet to install the software you need in this book.
You can do this either by plugging your Pi into your router with a network cable or by using
a USB wireless dongle, which will add WiFi connectivity.
Solving Problems
The most common problems with the Raspberry Pi are power-related issues. Not all micro
USB power sources can provide enough power, and it becomes more of a problem as you con-
nect peripherals to your Pi, or when you overclock it (see Chapter 5 for more details). Power-
related problems will usually manifest themselves as the computer crashing, so if you find
that your Pi becomes unstable, this is the best place to start. A good way to get around such
issues is to connect your Pi to one power source and connect all the peripherals (keyboard,
mouse, and so on) via a powered USB hub.
The second most common cause of problems with Pis is the SD card. These issues can be
caused by power supply problems, or they can be problems with the cards themselves. It’s
important to take preventative measures here to ensure that your data is safe, and that
means backups! You can use a service such as Google Drive (although this runs slowly on the
Pi), or you can simply keep extra copies of any work on a USB memory stick. SD card issues
will usually manifest themselves by the Pi displaying error messages when you try to start it.
Most of the time you can solve the problem by reinstalling Raspbian, but if this doesn’t work,
you’ll need to get a new SD card.
CHAPTER 1 GETTING UP AND RUNNING 7
If neither of these help, then you’ll need to dig a little deeper. The most useful places to look
are the kernel buffer and the system log file. The kernel buffer is usually best if you’re having
problems with hardware, such as a USB device not working. If you open LXTerminal and type:
dmesg
It will output all the messages from the Linux Kernel. The last ones are the most recent and
should show any problems.
The system log file (often called syslog) can be displayed with:
cat /var/log/syslog
Again, the most recent messages will be at the end. The information in both of these can be
somewhat cryptic. If you still can’t work out the problem after reading these, the best place to
go is the Raspberry Pi forums at [Link]/phpBB3/. There’s a community
of helpful people who should be able to point you in the right direction.
A Quick Tour of Raspbian
This is a book about programming, not about generally using Raspbian, so we won’t dwell on
it too much, but you’ll find it useful to know a bit about what’s going on.
There are a few operating systems available for the Raspberry Pi, but the instructions in this
book are all based on Raspbian, which is the default operating system, and the best choice for
a new user. If you have some experience with Linux, you could use Arch or Fedora if you like,
but you’ll have to change the apt-get commands to ones suitable for your package manager.
The easiest way to install Raspbian on your Pi is using NOOBS, which is available from www.
[Link]/downloads. You’ll also find a quick start guide at that website that
will tell you everything you need to know to get up and running.
There are two different ways of interacting with Raspbian—from the terminal and using the
graphical system (LXDE).
Using LXDE (Lightweight X11 Desktop Environment)
The Lightweight X11 Desktop Environment is the standard windowing system for Raspbian.
Its basic setup is the same as most versions of Windows pre-Windows 8. There’s a button in
the bottom-left side of the screen that opens an applications menu, and currently running
applications are displayed in the bar along the bottom (see Figure 1-1).
8 LEARNING PYTHON WITH RASPBERRY PI
Figure 1-1: The LXDE desktop with the menu open.
If you get a black screen with white text asking you to log in when you boot up your Pi, it
means that you haven’t set it up to start LXDE automatically. Don’t worry; just log in with
the username pi and the password raspberry, and then type the following:
startx
You can set it up to boot into LXDE automatically using raspi-config (see the next section).
Using the Terminal
LXDE is great for many tasks, but sometimes you’ll need to use the command line. This is an
incredibly powerful interface that’s accessed through the terminal. In LXDE, that means
opening the LXTerminal application.
When you open LXTerminal, you should see the following line:
pi@raspberrypi~$
This signifies that you are using the username pi on a computer called raspberrypi, and
you are in a directory called ~.
In Linux, all directories start from / or root. This is the base of the directory tree and every
directory is located in some subdirectory of this. You can move between directories using the
cd (change directory) command. Start by moving to this root directory with:
CHAPTER 1 GETTING UP AND RUNNING 9
cd /
You should now seen that the command prompt has changed to
pi@raspberrypi/$
You can list the contents of this directory with the command ls. One of the subdirectories is
called home. This is where every user on the system has his home directory. Move into it and
view its contents with:
cd home
ls
There should only be one directory called pi. The command prompt should now have
changed to show that you’re in the directory /home. Move into the only subdirectory with:
cd pi
Now the command prompt will have reverted to:
pi@raspberrypi~$
This is because the character ~ is a shorthand for the current user’s home directory. When
you type ~ in the terminal, the computer converts it to /home/pi.
There is much more to learn about the command line. So much so that it would take another
book this size to cover it with any semblance of completeness. However, you don’t need to
know everything to start using it, and whenever we tell you to use LXTerminal, we tell you
exactly what to type.
If you are interested in learning more about the Raspberry Pi, or Linux in general, the command
TIP
line is an excellent place to start, and there’s loads of information about it both online and in
print. The Linux command-line book, which you can browse for free online, is an excellent
place to start. See [Link]
We’ll leave you with two pieces of advice. Firstly, don’t be afraid of the terminal. It can be a
bit daunting at first, but the only way to learn how to use it is to use it. Secondly, almost all
commands have built-in help that you can access using the flag ––help. For example, if you
want to learn more about how to use the command ls, you can enter:
ls --help