Introduction - Programming the Raspberry Pi: Getting Started with Python (2013)

Programming the Raspberry Pi: Getting Started with Python (2013)

1. Introduction

The Raspberry Pi went on general sale at the end of February 2012 and immediately crashed the websites of the suppliers chosen to take orders for it. So what was so special about this little device and why has it created so much interest?

image

What Is the Raspberry Pi?

The Raspberry Pi, shown in Figure 1-1, is a computer that runs the Linux operating system. It has USB sockets you can plug a keyboard and mouse into and HDMI (High-Definition Multimedia Interface) video output you can connect a TV or monitor into. Many monitors only have a VGA connector, and Raspberry Pi will not work with this. However, if your monitor has a DVI connector, cheap HDMI-to-DVI adapters are available.

image

Figure 1-1 The Raspberry Pi

When Raspberry Pi boots up, you get the Linux desktop shown in Figure 1-2. This really is a proper computer, complete with an office suite, video playback capabilities, games, and the lot. It’s not Microsoft Windows; instead, it is Windows open source rival Linux (Debian Linux), and the windowing environment is called LXDE.

image

Figure 1-2 The Raspberry Pi desktop

Its small (the size of a credit card) and extremely affordable (starting at $25). Part of the reason for this low cost is that some components are not included with the board or are optional extras. For instance, it does not come in a case to protect it—it is just a bare board. Nor does it come with a power supply, so you will need to find yourself a 5V micro-USB power supply, much like you would use to charge a phone (but probably with higher power). A USB power supply and a micro-USB lead are often used for this.

image

What Can You Do with a Raspberry Pi?

You can do pretty much anything on a Raspberry Pi that you can on any other Linux desktop computer, with a few limitations. The Raspberry Pi uses an SD card in place of a hard disk, although you can plug in a USB hard disk. You can edit office documents, browse the Internet, and play games (even games with quite intensive graphics, such as Quake).

The low price of the Raspberry Pi means that it is also a prime candidate for use as a media center. It can play video, and you can just about power it from the USB port you find on many TVs.

image

A Tour of the Raspberry Pi

Figure 1-3 labels the various parts of a Raspberry Pi. This figure takes you on a tour of the Model B Raspberry Pi, which differs from the Model A by virtue of having an RJ-45 LAN connector, allowing it to be connected to a network.

image

Figure 1-3 The anatomy of a Raspberry Pi

The RJ-45 Ethernet connector is shown in the top-left corner of the figure. If your home hub is handy, you can plug your Raspberry Pi directly into your local network. While we are on the subject, it is worth noting that the Raspberry Pi does not have Wi-Fi built in. For wireless networking, you will need to plug in a USB wireless adapter. This may then require some additional work installing drivers.

Immediately below the Ethernet socket you’ll find a pair of USB sockets, one on top of the other. You can plug a keyboard, mouse, or external hard disks into the board, but you’ll fairly rapidly run out of sockets. For this reason, many people use a USB hub to gain a few more USB sockets.

In the bottom-left corner of the figure you’ll find an audio socket that provides a stereo analog signal for headphones or powered speakers. The HDMI connector is also sound capable.

Next to the audio socket is an RCA video connector. You are unlikely to use this connector unless you are using your Raspberry Pi with an older TV. You are far more likely to use the HDMI connector immediately opposite it, shown at the top of the figure. HDMI is higher quality, includes sound, and can be connected to DVI-equipped monitors with a cheap adapter.

To the right of the yellow RCA jack are two rows of pins. These are called GPIO (General Purpose Input/Output) pins, and they allow the Raspberry Pi to be connected to custom electronics. Users of the Arduino and other microcontroller boards will be used to the idea of GPIO pins. Later, in Chapter 11, we will use these pins to enable our Raspberry Pi to be the “brain” of a little roving robot by controlling its motors. In Chapter 10, we will use the Raspberry Pi to make an LED clock.

The Raspberry Pi has an SD card slot underneath the board. This SD card needs to be at least 2GB in size. It contains the computer’s operating system as well as the file system in which you can store any documents you create. The SD card is an optional extra feature when buying your Raspberry Pi. Preparing your own SD card is a little complex to do, and suppliers such as SK Pang, Farnell, and RS Components all sell already-prepared SD cards. Because no disk is built into your Raspberry Pi, this card is effectively your computer, so you could take it out and put it in a different Raspberry Pi and all your stuff would be there.

Above the SD card is a micro-USB socket. This is only used to supply power to the Raspberry Pi. Therefore, you will need a power supply with a micro-USB connector on the end. This is the same type of connector used by many mobile phones, including most Android phones. Do, however, check that it is capable of supplying at least 700mA; otherwise, your Raspberry Pi may behave erratically.

For those interested in technical specs, the big square chip in the center of the board is where all the action occurs. This is Broadcom’s “System on a Chip” and includes 256MB of memory as well as the graphics and general-purpose processors that drive the Raspberry Pi.

You may also have noticed flat cable connectors next to the SD card and between the Ethernet and HDMI connectors. These are for LCD displays and a camera, respectively. Look for camera and LCD display modules becoming available for the Pi in the near future.

image

Setting Up Your Raspberry Pi

You can make your life easier by buying a prepared SD card and power supply when you buy your Raspberry Pi, and for that matter you may as well get a USB keyboard and mouse (unless you have them lurking around the house somewhere). Let’s start the setup process by looking at what you will need and where to get it from.

Buying What You Need

Table 1-1 shows you what you will need for a fully functioning Raspberry Pi system. At the time of writing, the Raspberry Pi itself is sold through two worldwide distributors based in the UK: Farnell (and the related U.S. company Newark) and RS Components, which is not to be confused with RadioShack.

image

Table 1-1 A Raspberry Pi Kit

Power Supply

Figure 1-4 show a typical USB power supply and USB-A-to-micro-USB lead.

image

Figure 1-4 USB power supply

You may be able to use a power supply from an old MP3 player or the like, as long as it is 5V and can supply enough current. It is important not to overload the power supply because it could get hot and fail (or even be a fire hazard). Therefore, the power supply should be able to supply at least 700mA, but 1A would give the Raspberry Pi a little extra when it comes to powering the devices attached to its USB ports.

If you look closely at the specs written on the power supply, you should be able to determine its current supply capabilities. Sometimes its power-handling capabilities will be expressed in watts (W); if that’s the case, it should be at least 3W. If it indicates 5W, this is equivalent to 1A.

Keyboard and Mouse

The Raspberry Pi will work with pretty much any USB keyboard and mouse. You can also use most wireless USB keyboards and mice—the kind that come with their own dongle to plug into the USB port. This is quite a good idea, especially if they come as a pair. That way, you are only using up one of the USB ports. This will also come in quite handy in Chapter 10 when we use a wireless keyboard to control our Raspberry Pi–based robot.

Display

Including an RCA video output on the Raspberry Pi is, frankly, a bit puzzling because most people are going to go straight to the more modern HDMI connector. A low-cost 22-inch LCD TV will make a perfectly adequate display for the Pi. Indeed, you may just decide to use the main family TV, just plugging the Pi into the TV when you need it.

If you have a computer monitor with just a VGA connector, you are not going to be able to use it without an expensive converter box. On the other hand, if your monitor has a DVI connector, an inexpensive adapter will do the job well.

SD Card

You can use your own SD card in the Raspberry Pi, but it will need to be prepared with an operating system disk image. This is a little fiddly, so you may just want to spend a dollar or two more and buy an SD card that is already prepared and ready to go.

You can also find people at Raspberry Pi meet-ups who will be happy to help you prepare an SD card. The prepared SD cards supplied by Farnell and RS Components are overpriced. Look around on the Internet to find suppliers (such as SK Pang) who sell prepared cards, with the latest operating system distribution, for less than you would pay for an SD card in a supermarket. If you indeed want to “roll your own” SD card, refer to the instructions found at www.raspberrypi.org/downloads.

To prepare your own card, you must have another computer with an SD card reader. The procedure is different depending on whether your host computer is a Windows, Mac, or Linux machine. However, various people have produced useful tools that try to automate the process as much as possible.

If you decide to roll your own, be sure to follow the instructions carefully—with some tools, it is quite easy to accidentally reformat a hard disk attached to your computer if the tool mistakes it for the SD card! Fortunately, this process is getting better all the time as easier-to-use software tools become available.

A big advantage of making your own SD card is that you can actually choose from a range of operating system distributions. Table 1-2 shows the most popular ones available at the time of writing. Check on the Raspberry Pi Foundation’s website for newer distributions.

image

Table 1-2 Raspberry Pi Linux Distributions

Of course, nothing is stopping you from buying a few SD cards and trying out the different distributions to see which you prefer. However, if you are a Linux beginner, you should stick to the standard Raspbian Wheezy distribution.

Case

The Raspberry Pi does not come in any kind of enclosure. This helps to keep the price down, but also makes it rather vulnerable to breakage. Therefore, it is a good idea to either make or buy a case as soon as you can. Figure 1-5 shows a few of the ready-made cases currently available.

image

Figure 1-5 Commercial Raspberry Pi cases

The cases shown are supplied by (a) Adafruit (www.adafruit.com), (b) SK Pang (www.skpang.co.uk/), and (c) ModMyPi (www.modmypi.com). The case you choose will depend on what you plan to do with your Raspberry Pi. If you have access to a 3D printer, you can also use the following open source designs:

• www.thingiverse.com/thing:23446

• www.thingiverse.com/thing:24721

You can also find a folded card design called the Raspberry Punnet at www.raspberrypi.org/archives/1310.

People are having a lot of fun building their Raspberry Pi into all sorts of repurposed containers, such as vintage computers and games consoles. One could even build a case using Legos. My first case for a Raspberry Pi was made by cutting holes in a plastic container that used to hold business cards (see Figure 1-6).

image

Figure 1-6 A homemade Raspberry Pi case

Wi-Fi

Neither of the Raspberry Pi models has support for Wi-Fi. Therefore, to wirelessly connect your Raspberry Pi to the network, you have just two options. The first is to use a USB wireless adapter that just plugs into a USB socket (see Figure 1-7). With any luck, Linux should recognize it and immediately allow you to connect (or show what you need to do to connect).

image

Figure 1-7 Wi-Fi adapter

The Wi-Fi adapters in the list referenced in Table 1-1 are purported to work with the Raspberry Pi. However, there are sometimes problems with Wi-Fi drivers, so be sure to check the Raspberry Pi forum and wiki for up-to-date information on compatible devices.

The second option for Wi-Fi is to use a Wi-Fi bridge with a Model B Raspberry Pi. These devices are usually USB powered and plug into the Ethernet socket on the Raspberry Pi. They are often used by the owners of game consoles that have an Ethernet socket but no Wi-Fi. This setup has the advantage in that the Raspberry Pi does not require any special configuration.

USB Hub

Because the Raspberry Pi has just two USB ports available, you will rapidly run out of sockets. The way to obtain more USB ports is to use a USB hub (see Figure 1-8).

image

Figure 1-8 A USB hub

These hubs are available with anywhere from three to eight ports. Make sure that the port supports USB 2. It is also a good idea to use a “powered” USB hub so that you do not draw too much power from the Raspberry Pi.

Connecting Everything Together

Now that you have all the parts you need, let’s get it all plugged together and boot your Raspberry Pi for the first time. Figure 1-9 shows how everything needs to be connected.

image

Figure 1-9 A Raspberry Pi system

Insert the SD card, connect the keyboard, mouse, and monitor to the Pi, attach the power supply, and you are ready to go.

image

Booting Up

The first time you boot your Raspberry Pi, it will not immediately boot into the kind of graphical environment you would normally see in, say, a Windows computer. Instead, it will stop to allow a first-time configuration (see Figure 1-10). It is a good idea to make a number of the configuration changes shown here.

image

Figure 1-10 Configuration screen

First, if your SD card is larger than 2GB, the Raspberry Pi will only make use of the first 2GB unless you select the option to expand_rootfs. Select this option using the UP and DOWN ARROW keys and ENTER.

Another change well worth making is the boot_behaviour option. If this is not set to Boot Straight to Desktop, you will be forced to log in and start the windowing environment manually each time you power up your Raspberry Pi (see Figure 1-11).

image

Figure 1-11 Boot-to-desktop option

image

Summary

Now that we have set up our Raspberry Pi and it is ready to use, we can start exploring some of its features and get a grip on the basics of Linux.