Chapter 1. Setup and Management

1.0 Introduction

When you buy a Raspberry Pi, you are essentially buying an assembled printed circuit board. It does not even include a power supply or operating system.

The recipes in this chapter are concerned with getting your Raspberry Pi set up and ready for use.

Because the Raspberry Pi just uses standard USB keyboards and mice, most of the setup is pretty straightforward, so you will concentrate only on those tasks that are specific to the Raspberry Pi. 

1.1 Selecting a Model of Raspberry Pi

Problem

There are many models of Raspberry Pi and you are not sure which to use.

Solution

If you want a Raspberry Pi for general use, then you should buy a Raspberry Pi 3 or 2 model B. With four times as much memory and a quad-core processor, it will cope with most tasks much better than the Pi Zero or model A+ with their single processors. The Raspberry Pi 3 model B has the great advantage of having WiFi built in, so there’s no need for an extra USB WiFi adapter.

If, on the other hand, you are embedding a Raspberry Pi in a project for a single purpose, then using a model A+ or Pi Zero and saving a few dollars may well be an option. 

Discussion

Figure 1-1 shows the Pi Zero, model A+, and Raspberry Pi 2 B.

F0101.jpg
Figure 1-1. Raspberry Pi Zero (left), model A+ (center), and Raspberry Pi 2 model B (right)

As you can see from Figure 1-1, the model A+ is smaller than the Pi 2 and has a single USB socket and no RJ45 Ethernet socket. The Pi Zero is even smaller, saving space by using a mini HDMI socket and micro USB on-the-go socket. If you want to connect a keyboard monitor and mouse to a Pi Zero, you will need adapters for both the USB and HDMI ports before you can connect standard peripherals. 

The differences between all the Raspberry Pi models to date are summarized in Table 1-1

Table 1-1. Raspberry Pi models
Model RAM USB sockets Ethernet port Notes
3 B 1 GB 4 yes Includes WiFi
Zero 512 MB 1 (micro) no Low cost
2 B 1 GB 4 yes Quad-core
A+ 256 MB 1 no  
B+ 512 MB 4 yes Discontinued

A

256 MB

1

no

Discontinued

B rev2

512 MB

2

yes

Discontinued

B rev1

256 MB

2

yes

Discontinued

If you have one of the older discontinued Raspberry Pi models, it is still useful. They do not have quite the performance of the latest Raspberry Pi 3 model B, but for many situations, that does not matter.

In Recipe 9.21, the Raspberry Pi Compute module will be introduced. This is designed specifically to allow a Raspberry Pi to be built into a product.

See Also

For more information on the Raspberry Pi models, see http://en.wikipedia.org/wiki/Raspberry_Pi.

The low cost of the Pi Zero makes it ideal for embedding in electronics projects without worrying about the cost. See Recipe 9.22.

1.2 Enclosing a Raspberry Pi

Problem

You need an enclosure for your Raspberry Pi.

Solution

The Raspberry Pi does not come with an enclosure unless you buy one as part of a kit. This makes it a little vulnerable, as there are bare connections on the underside of the circuit board that could easily be short-circuited if the Raspberry Pi is placed on something metal.

It is a good idea to buy some protection for your Raspberry Pi in the form of a case. If you intend to use the Raspberry Pi’s GPIO pins, then the PiBow Coupé shown in Figure 1-2 is a beautiful and practical design.

Discussion

There is a vast array of case styles to choose from, including:

  • Simple, two-part, click-together plastic boxes

  • VESA mountable boxes (for attaching to the back of a monitor or TV)

  • Lego-style boxes

  • 3D-printed box designs

  • Laser-cut, snap-together acrylic designs

F0150.jpg
Figure 1-2. A Raspberry Pi 2 in a PiBow Coupé

The case you buy is very much a matter of personal taste. However, some of the things you need to consider are:

  • Do you need to have access to the GPIO connector? This is important if you plan to attach external electronics to your Raspberry Pi.

  • Is the case well-ventilated? This is important if you plan to overclock your Raspberry Pi (Recipe 1.10) or run it hard playing videos or games, as these will all generate more heat.

You will also find heatsink kits that have tiny self-adhesive heatsinks to attach to the chips on the Raspberry Pi. These may be of some use if you are demanding a lot of your Raspberry Pi, say by playing a lot of videos, but generally they are the equivalent of “go-faster” stripes on a car.

See Also

Adafruit has a nice range of Raspberry Pi enclosures.

You will also find many styles of cases at other Raspberry Pi suppliers and on eBay.

1.3 Selecting a Power Supply

Problem

You need to select a power supply for your Raspberry Pi.

Solution

The basic electrical specification for a power supply suitable for a Raspberry Pi is that it supplies a regulated 5V DC (direct current).

The amount of current that the power supply must be capable of providing depends both on the model of Raspberry Pi and the peripherals attached to it. It is worth getting a power supply that can easily cope with the Raspberry Pi and you should consider 700mA to be a minimum.

If you buy your power supply from the same place that you buy the Raspberry Pi, then the seller should be able to tell you if it will work with the Raspberry Pi.

If you are going to be using a WiFi dongle or other USB peripherals that use significant amounts of power, then I would get a power supply capable of 1.5A or even 2A. Also beware of very low-cost power supplies that may not provide an accurate or reliable 5V.

Discussion

The power supply and connector are actually the same as those found in many smartphone chargers. If they are terminated in a micro USB plug, then they are almost certainly 5V (but check). The only question, then, is if they can supply enough current.

If they can’t, then a few bad things can happen:

  • They may get hot and be a potential fire risk.

  • They may just fail.

  • At times of high load (say, when the Pi is using a WiFi dongle), the voltage may dip and the Raspberry Pi may reset itself.

In general, look for a power supply that says it can supply 700mA or more. If it specifies a number of watts (W) rather than mA, divide the number of watts by 5 to get the mA figure. So, a 5V 10W power supply can supply 2A (2000mA).

Using a power supply with, say, a maximum current of 2A will not use any more electricity than a 700mA power supply. The Raspberry Pi will just take as much current as it needs.

In Figure 1-3, I measure the current taken by a Raspberry Pi model B and compare it with a Raspberry Pi 2 model B.

FF0102.png
Figure 1-3. Raspberry Pi current consumption during booting

The newer Raspberry Pis (the A+, B+, or Raspberry Pi 2) are far more power-efficient than the older models, but when the processor is fully occupied and has a lot of peripherals attached, they can still reach similar current requirements. 

In Figure 1-3, you can see that the current rarely gets above 500mA. However, the processor isn’t really doing very much here. Were you to start playing HD video, the current would increase considerably. When it comes to power supplies, it’s usually better to have something in reserve.

See Also

You can buy a module that will turn off the power when the Raspberry Pi shuts down at http://www.pi-supply.com/.

1.4 Selecting an Operating System Distribution

Problem

There are a number of different Raspberry Pi distributions. You are not sure which one to use.

Solution

The answer to this question depends on what you intend to do with your Raspberry Pi.

For general use as a computer or for using in electronic projects, you should use Raspbian, the standard and official distribution for the Raspberry Pi.

If you plan to use your Raspberry Pi as a media center, there are a number of distributions specifically for that purpose (see Recipe 4.1).

In this book, we use the Raspbian distribution almost exclusively, although most of the recipes will work with any Debian-based distribution.

Discussion

MicroSD cards are not expensive, so get a few and try out a few distributions. If you do this, it is a good idea to keep your own files on a USB flash drive so that you don’t have to keep copying them onto each microSD card.

Note that if you are using one of the upcoming recipes to write your own SD card, then you need to have a computer that has an SD card slot (many laptops do), or you can buy an inexpensive USB SD card reader.

1.5 Writing a MicroSD Card with NOOBS

Problem

You want to write a microSD card using NOOBS (New Out of the Box Software).

Solution

NOOBS is by far the easiest way to get an operating system onto your Raspberry Pi.

Download the NOOBS archive file from http://www.raspberrypi.org/downloads, extract it, and place it on a microSD card. To do this, you will need a computer with an SD card slot or a USB adapter and a SD-to-microSD adapter.

Once you have downloaded the NOOBS archive file, extract it and copy the folder contents onto the SD card. Note that if the archive extracts to a folder called NOOBS_v1_3_12 or similar, it is the contents of the folder that should be copied to the root of the microSD card, not the folder itself.

Put the microSD card containing the extracted NOOBS files into your Raspberry Pi and then power up your Raspberry Pi. When it boots, the window shown in Figure 1-4 will appear. From this screen, select Raspbian and then click the Install button.

If you are using NOOBS on an A+, you will see a shorter list of options because only the distributions for that simplified platform will be shown.

F0103
Figure 1-4. NOOBS first screen

You will get a warning message that the SD card will be overwritten (which is fine) and then as the distribution is installed onto the SD card, you will see a progress screen accompanied by helpful information about the distribution (Figure 1-5).

F0104
Figure 1-5. NOOBS overwriting the SD card

Once the file copying is complete, you will get the message Image applied successfully. When you hit return, the Raspberry Pi will reboot and then raspi_config will automatically run so that you can configure the new installation.

Once you are up and running, the first thing you should do is connect your Raspberry Pi to the Internet (Recipes 2.1 and 2.5), open a command line by using LXTerminal (Recipe 3.2), and enter the following command to update your system to the latest version. 

$ sudo apt-get update 
$ sudo apt-get upgrade

This will take some time.

Discussion

For NOOBS to install correctly onto a microSD card, the card must be formatted as FAT32. Most SD and microSD cards are supplied already formatted in FAT32. If you are reusing an old card and need to format it as FAT32, then use your operating system’s tool for formatting removable media.

The type of microSD card that you get will also affect how fast your Raspberry Pi runs once the operating system is installed. Look for a microSD card described as “class 10.”

See Also

You can find further information on installing an operating system with NOOBS, including information about the different distributions available at https://www.raspberrypi.org/help/noobs-setup/.

1.6 Connecting the System

Problem

You have everything that you need for your Raspberry Pi, and you want to connect it all together.

Solution

Unless you are embedding your Raspberry Pi in a project or using it as a media center, you need to attach a keyboard, mouse, monitor, and probably a WiFi dongle, unless you have a Raspberry Pi 3.

Figure 1-6 shows a typical Raspberry Pi system.

F0108
Figure 1-6. A typical Raspberry Pi system

Discussion

The Raspberry Pi is perfectly happy with pretty much any keyboard or mouse, wired or wireless. The exception to this is Bluetooth wireless keyboards and mice, which will not work with the Raspberry Pi.

If you have an older Raspberry Pi or a model A or A+ and run out of USB sockets, then you will also need a USB hub.

1.7 Connecting a DVI or VGA Monitor

Problem

Your monitor does not have an HDMI connector but you want to use it with your Raspberry Pi.

Solution

Many people have been caught out by this problem. Fortunately, it is possible to buy adapters for monitors with a DVI or VGA input but no HDMI connectors.

DVI adapters are the simplest and cheapest. They can be found for less than $5 if you search for “HDMI male to DVI female converter.”

Discussion

Using VGA adapters is more complex because they require some electronics to convert the signal from digital to analog, so beware of leads that do not contain these. The official converter is called Pi-View and is available wherever the Raspberry Pi is sold. Pi-View has the advantage of having been tested and found to work with Raspberry Pi. You may find cheaper alternatives on the Internet, but often these won’t work.

1.8 Using a Composite Video Monitor/TV

Problem

The text on your low-resolution composite monitor is illegible. You need to adjust the resolution of the Raspberry Pi for a small screen.

Solution

The Raspberry Pi has two types of video output: HDMI and composite video from the audio jack, for which you need a special cord. Of these, the HDMI is much better quality. If you’re intending to use a composite video as your main screen, you may want to think again.

If you are using such a screen—say, because you need a really small screen—then you need to make a few adjustments to fit the video output to the screen. You need to make some changes to the file /boot/config.txt. You can edit it on the Raspberry Pi by issuing the following command in a Terminal session:

$ sudo nano /boot/config.txt

If the text is too small to read and you do not have an HDMI monitor, then you can also edit the file by removing the SD card from the Raspberry Pi and inserting it into your computer. The file will then be in the top-level directory on the SD card, so you can use a text editor on your PC to modify it.

You need to know the resolution of your screen. For a lot of small screens, this will be 320 by 240 pixels. Find the two lines in the file that read:

#framebuffer_width=1280
#framebuffer_height=720

Remove the # from the front of each line and change the two numbers to the width and height of your screen. In the following example, these lines have been modified to be 320 by 240:

framebuffer_width=320
framebuffer_height=240

Save the file and restart your Raspberry Pi. You should find that everything has become a lot easier to read. You will probably also find that there is a big, thick border around the screen. To adjust this, see Recipe 1.9.

Discussion

There are many low-cost CCTV monitors that can make a great companion for the Raspberry Pi when you’re making something like a retro games console (Recipe 4.6). However, these monitors are often very low resolution.

See Also

For another tutorial on using composite monitors, see this Adafruit tutorial.

Also, see Recipes 1.7 and 1.9 to adjust your picture when you’re using the HDMI video output.

1.9 Adjusting the Picture Size on Your Monitor

Problem

When you first connect a Raspberry Pi to a monitor, you may find that some of the text cannot be read because it extends off the screen, or the picture isn’t using all the space available on the screen.

Solution

If your text extends off the screen, use the raspi-config tool to turn overscan off.

To do this, run raspi-config by opening a Terminal session and issuing the command:

$ sudo raspi-config

Then use the cursor keys to scroll down to Advanced Options and then Overscan, and turn overscan off (Figure 1-7).

F0110
Figure 1-7. Selecting the Overscan option

If your problem is that there is a large black border around the picture, then you can reduce this (and possibly eliminate it entirely) by editing the file /boot/config.txt using the command:

$ sudo nano /boot/config.txt

Look for the section dealing with overscan. The four lines you need to change are shown in the middle of Figure 1-8.

rpck 0111
Figure 1-8. Adjusting overscan

For the lines to take effect, you first need to uncomment them by removing the # character from the start of each line.

Then, using trial and error, change the settings until the screen fills as much of the monitor as possible. Note that the four numbers should be negative. Try setting them all to –20 to start with. This will increase the area of the screen that is used.

Discussion

Having to repeatedly restart the Raspberry Pi to see the effects of the changes in resolution is a little tedious. Fortunately, you will only have to do this procedure once. Many monitors and TVs work just fine without any adjustments.

See Also

You can find much more information about the raspi-config tool at http://elinux.org/RPi_raspi-config.

1.10 Maximizing Performance

Problem

Your Raspberry Pi seems to be very slow, so you want to overclock it to make it run faster.

Solution

If you have a Raspberry Pi 2 with its quad-core processor, you are unlikely to find it to be too slow. However, the older single-core Raspberry Pis can be pretty sluggish. 

You can increase the clock frequency of a Raspberry Pi to make it run a little faster. This will make it use a bit more power and run a little hotter (see the Discussion next).

The method of overclocking described here is called dynamic overclocking because it automatically monitors the temperature of the Raspberry Pi and drops the clock speed back down if things start to get too hot.

To make your Pi overclock, run the raspi_config utility by issuing the following command in a Terminal:

$ sudo raspi-config

Select the Overclock option in the menu, and you are presented with the options in Figure 1-9.

F0112
Figure 1-9. Overclocking options

Select an option. If you find that your Raspberry Pi starts to become unstable and hangs unexpectedly, then you may need to choose a more conservative option or turn overclocking off by setting it back to None.

Discussion

The performance improvements from overclocking can be quite dramatic. To measure these, I used a Raspberry Pi model B, revision 2, without a case at an ambient room temperature of 15 degrees C.

The test program was the following Python script. This just hammers the processor and is not really representative of the other things that go on in a computer, such as writing to the SD card, graphics, and so on. But it does give a good indication of raw CPU performance if you want to test the effect of overclocking on your Raspberry Pi.

import time

def factorial(n):
  if n == 0:
    return 1
  else:
    return n * factorial(n-1)

before_time = time.clock()
for i in range(1, 10000):
  factorial(200)
after_time = time.clock()

print(after_time - before_time)

Check out the results of the test in Table 1-2.

Table 1-2. Overclocking
  Speed test Current Temperature (degrees C)
700 MHz 15.8 seconds 360mA 27
1 GHz 10.5 seconds 420mA 30

As you can see, the performance has increased by 33% but at a cost of drawing more current and a slightly higher temperature.

A well-ventilated enclosure will help to keep your Raspberry Pi running at full speed. There have also been some efforts to add water-cooling to the Raspberry Pi. Frankly, this is just silly.

See Also

You can find much more information about the raspi-config tool at http://elinux.org/RPi_raspi-config.

1.11 Changing Your Password

Problem

By default, the password for a Raspberry Pi will be raspberry. You want to change this.

Solution

You can use the raspi-config tool to change your password. Run the raspi_config utility by issuing the following command in a Terminal (see Recipe 3.2):

$ sudo raspi-config

Then select the change_pass option in the menu and follow the prompts shown in Figure 1-10.

F0113
Figure 1-10. Changing the password

Changing your password is one occasion where you do not have to restart your Raspberry Pi for the changes to take effect.

Discussion

You can also change the password from a Terminal session simply by using the passwd command as follows:

$ passwd
Changing password for pi.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

See Also

You can find much more information about the raspi-config tool at http://elinux.org/RPi_raspi-config.

1.12 Setting the Pi to Boot Straight into a Windowing System

Problem

Every time you reboot your Raspberry Pi, you have to log in and then start the desktop manually. You want to make this automatic.

Solution

You can use the raspi-config tool to change the boot behavior so that the Raspberry Pi automatically logs you in and starts the desktop. Run the raspi_config utility by issuing the following command in a Terminal:

$ sudo raspi-config

Then select the Enable Boot to Desktop/Scratch option and then “Desktop Log in as user pi”(Figure 1-11).

After you change the boot option, you are prompted to restart your Raspberry Pi for the changes to take effect.

F0114
Figure 1-11. Automatic boot into a desktop

Discussion

Clearly there are security implications in allowing the Raspberry Pi to log you in to the windowing environment automatically. But because the Raspberry Pi will generally be used as a personal computer, rather than being shared, the convenience usually outweighs any such disadvantages.

See Also

You can find much more information about the raspi-config tool at http://elinux.org/RPi_raspi-config.

1.13 Shutting Down Your Raspberry Pi

Problem

You want to shut down your Raspberry Pi.

Solution

Click on the Raspberry menu in the top-left corner of the desktop. This will display a number of options (Figure 1-12).

Shutdown

Shuts down the Raspberry Pi. You will need to unplug the power and plug it in again to get the Raspberry Pi to boot up again.

Reboot

Reboots the Raspberry Pi.

Logout

Logs you out and displays a prompt to enter your login credentials so that you can log back in.

F0115
Figure 1-12. Shutting down your Raspberry Pi

You can also reboot from the command line by issuing the command:

sudo reboot

You may have to do this after installing some software. When you do reboot, you see the message shown in Figure 1-13, which illustrates the multiuser nature of Linux and warns all users connected to the Pi.

rpck 0116
Figure 1-13. Shutting down your Raspberry Pi from the Terminal

Discussion

It is better to shut your Raspberry Pi down as described above than to simply pull out the power plug, because the Raspberry Pi may be in the middle of writing to the microSD card as you power it down. This could lead to file corruption.

Unlike shutting down most computers, shutting down a Raspberry Pi does not actually turn off the power. It goes into a low-power mode—and it is a pretty low-power device anyway (but the Raspberry Pi hardware has no control over its power supply).

See Also

You can buy a module that will turn off the power when the Raspberry Pi shuts down at http://www.pi-supply.com/.

1.14 Installing the Raspberry Pi Camera Module

Problem

You want to use the Raspberry Pi camera module (see Figure 1-14).

Solution

The Raspberry Pi camera module (Figure 1-14) is attached to a Raspberry Pi by a ribbon cable.

rpck 0117
Figure 1-14. The Raspberry Pi camera module

This cable attaches to a special connector between the audio and HDMI sockets on a Raspberry Pi 2. On an original  Raspberry Pi model B, the connector is just behind the Ethernet socket. To fit it, pull up the levers on either side of the connector so that they unlock, and then press the cable into the slot with the connector pads of the cable facing away from the Ethernet socket. Press the two levers of the connector back down to lock the cable in place (Figure 1-15).

rpck 0118
Figure 1-15. Fitting a Raspberry Pi camera module attached to a Raspberry Pi model B
Warning

The camera module packaging states that it is sensitive to static. Before handling it, ground yourself by touching something grounded like the metal case of a PC.

The camera module requires some software configuration. The easiest way to configure it is to use raspi_config. To run raspi-config, enter the following command into a Terminal session:

$ sudo raspi-config

You will see the Enable Camera option (Figure 1-16).

Two commands are available for capturing still images and videos: raspiStill and raspivid.

F0120
Figure 1-16. The updated raspi-config configuration tool

To capture a single still image, use the raspiStill command as shown here:

$ raspistill -o image1.jpg

A preview screen displays for about five seconds and then takes a photograph and stores it in the file image1.jpg in the current directory.

To capture video, use the command raspivid:

$ raspivid -o video.h264 -t 10000

The number on the end is the recording duration in milliseconds—in this case, 10 seconds.

Discussion

Both raspstill and raspivid have a large number of options. If you type either command without any parameters, help text displays options that are available.

The camera module is capable of high-resolution stills and video recording.

Here are some of the key features of the camera:

  • 5-megapixel sensor

  • Fixed focus f/2 lens

  • Still resolution 1920×1080

  • Video 1080p, 30fps

An alternative to the camera module is to use a USB webcam (see Recipe 8.2).

See Also

The RaspiCam documentation includes raspstill and raspivid.

1.15 Using Bluetooth

Problem

I want to use Bluetooth with my Raspberry Pi.

Solution

Attach a USB Bluetooth adapter to the Raspberry Pi and install the supporting Bluetooth software.

Not all Bluetooth adapters are compatible with the Raspberry Pi. Most are, but to be sure, buy one that is advertised as working with the Raspberry Pi. Figure 1-17 shows a Raspberry Pi 2 equipped with both a USB Bluetooth adapter (nearest to the camera) and a USB WiFi adapter.

F0130
Figure 1-17. Raspberry Pi 2 with USB Bluetooth and WiFi adapters

To install the software needed to support Bluetooth, enter the following commands:

$ sudo apt-get update
$ sudo apt-get install bluetooth bluez-utils blueman bluez  
$ sudo usermod -G bluetooth -a pi 

These commands should work for all Bluetooth adapters supported by the Raspberry Pi.

Plug your Bluetooth adapter in and then reboot your Raspberry Pi (Recipe 1.13).

You will now find a new entry on the Raspbian Start menu under the Preferences section called Bluetooth Manager. Open this utility and click Search to look for nearby Bluetooth devices (Figure 1-18). Make sure that there are some Bluetooth devices set to be discoverable.

F0131
Figure 1-18. The Bluetooth Manager

Discussion

From the Bluetooth Manager, you can pair with other Bluetooth devices, send them files, and configure the visibility of your Raspberry Pi to other Bluetooth devices.

See Also

For a list of Bluetooth adapters that are compatible with the Raspberry Pi, see http://elinux.org/RPi_USB_Bluetooth_adapters.

Get Raspberry Pi Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.