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 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


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


The decision as to which Raspberry Pi model to use depends very much on what you plan to do with it. Table 1-1 lists some uses and my model recommendations.

Table 1-1. Selecting a model of Raspberry Pi
Usage Suggested model Notes
Desktop computer replacement Raspberry Pi 4 model B (4 GB) You will need the 4 GB of memory if you are web browsing.
Electronics experimentation Raspberry Pi 2 or 3 model B Reasonably up-to-date hardware will minimize software problems. No need for more performance.
Computer vision Raspberry Pi 4 model B (4 GB) Maximum performance required.
Home automation Raspberry Pi 2 or 3 model B Low-power consumption and more than enough power.
Media center Raspberry Pi 3 or 4 For video performance.
Electronic display board Any model A model with WiFi, advantageous for remote access.
Embedded electronics project Raspberry Pi Zero W Low cost and WiFi enabled for remote access.

If you want a good general-purpose Raspberry Pi, I recommend a Raspberry Pi 3 model B+. With four times as much memory as the original Raspberry Pi and a quad-core processor, it will cope with most tasks much better than the Pi Zero, but it doesn’t get as hot or use as much power as the Raspberry Pi 4. The Raspberry Pi 3 model B+ also has the great advantage of having WiFi and Bluetooth built in, so there’s no need for an extra USB WiFi adapter or Bluetooth hardware.

If, on the other hand, you are embedding a Raspberry Pi in a project for a single purpose, using a compact Pi Zero W and saving a few dollars might well be an option.

The Raspberry Pi 3 model A+ is essentially a Raspberry Pi 3 model B+ with the Ethernet connector and three of the four USB ports removed. It also has half the memory of a B+. But it is significantly cheaper ($10 cheaper as of this writing). So it is definitely worth considering if you don’t need the extra ports for your project.


Figure 1-2 shows the Pi Zero W, the Raspberry Pi 3 B, and the Raspberry Pi 4.

Figure 1-2. From left to right, the Raspberry Pi Zero W, the Raspberry Pi 3 B, and the Raspberry Pi 4 B

As you can see from Figure 1-2, the Pi Zero W is roughly half the size of the Pi 3 B or Pi 4 B, and it has a single micro-USB socket for communication, and a second one for power. The Pi Zero W also saves 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 the HDMI port before you can connect standard peripherals. The Raspberry Pi A+ is larger than the Pi Zero and has full-sized USB and HDMI ports.

Table 1-2 summarizes the differences between all the Raspberry Pi models to date, with the most recently released models toward the top.

Table 1-2. Raspberry Pi models
Model RAM Processor
(cores * clock)
USB sockets Ethernet port Notes
4 B 1/2/4 GB 4 * 1.5 GHz 4 (2 x USB3) yes 2 x micro-HDMI video
3 A+ 512 MB 4 * 1.4 GHz 1 no WiFi and Bluetooth
3 B+ 1 GB 4 * 1.4 GHz 4 yes WiFi and Bluetooth
3 B 1 GB 4 * 1.2 GHz 4 yes WiFi and Bluetooth
Zero W 512 MB 1 * 1 GHz 1 (micro) no WiFi and Bluetooth
Zero 512 MB 1 * 1 GHz 1 (micro) no Low cost
2 B 1 GB 4 * 900 MHz 4 yes  
A+ 256 MB 1 * 700 MHz 1 no  
B+ 512 MB 1 * 700 MHz 4 yes Discontinued
A 256 MB 1 * 700 MHz 1 no Discontinued

B rev2

512 MB

1 * 700 MHz




B rev1

256 MB

1 * 700 MHz




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

If you are buying a new Raspberry Pi, I consider the best choice for use as a general-purpose computer to be the Pi 3 B+. If you need maximum power and don’t mind the processor chip becoming hot, consider a Raspberry Pi 4. If you don’t need WiFi or want a smaller device, also consider the 3 B, 2 B, 3 A+, Zero, or Zero W.

See Also

For more information on the Raspberry Pi models, see

The low cost of the Pi Zero and Pi Zero W models makes them ideal for embedding in electronics projects without worrying about the expense. See Recipe 9.21.

1.2 Connecting the System


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


Unless you are embedding your Raspberry Pi in a project or using it as a media center, you need to attach a keyboard, a mouse, a monitor, and possibly a WiFi dongle, if you have an older Raspberry Pi without built-in WiFi.

Figure 1-3 shows a typical Raspberry Pi system. If you have a Raspberry Pi 4, you could (if you really wanted) connect a second monitor. However, if you have just one monitor, connect it to the micro-HDMI connector closest to the USB-C power connector.

Figure 1-3. A typical Raspberry Pi system


The Raspberry Pi is perfectly happy with pretty much any USB keyboard and mouse, wired or wireless.

The Raspberry Pi 4 lets you connect two monitors to your system at the same time. When you do so, you will be able to move your mouse cursor between screens, but Raspbian will need to know where the screens are relative to each other. To enact this, in the Preferences section on the Main menu, open the Screen Configuration tool (Figure 1-4).

Figure 1-4. Arranging multiple screens

You can drag the two boxes labeled HDMI-1 and HDMI-2 around to represent the physical position of the two monitors. So in this case the monitors are side by side, with the monitor connected to HDMI-1 on the left.

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

1.3 Enclosing a Raspberry Pi


You need an enclosure for your Raspberry Pi.


The Raspberry Pi does not come with an enclosure unless you buy one as part of a kit. This makes it a little vulnerable, given that 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 general-purpose input/output (GPIO) pins (the pins that allow you to connect to external electronics), the PiBow Coupé shown in Figure 1-5 is a beautiful and practical design that is available for both the Raspberry Pi 4 and earlier versions.


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

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

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

  • Lego-compatible boxes

  • 3D-printed box designs

  • Laser-cut, snap-together acrylic designs

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

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

  • 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.14) or run it hard playing videos or games, because these will all generate more heat.

  • Finally, make sure you get one that fits your model of Raspberry Pi.

If you have access to a 3D printer, you can also print your own case. Search for Raspberry Pi on Thingiverse or MyMiniFactory, and you will find lots of designs.

You will also find heatsink kits that have tiny self-adhesive heatsinks to attach to the chips on the Raspberry Pi. These can 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.

If you have a Raspberry Pi 4, you can reduce the temperature by fitting a small fan such as the Pimoroni Fan SHIM as shown in Figure 1-6.

Figure 1-6. The Pimoroni Fan SHIM

See Also

Adafruit has a nice range of Raspberry Pi enclosures.

For an enclosure that also has space for a built-in SSD, see Recipe 1.10.

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

1.4 Selecting a Power Supply


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


The basic electrical specification for a power supply suitable for a Raspberry Pi is that it provides 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 on the peripherals attached to it. It is worth getting a power supply that can easily cope with the Raspberry Pi, and you should consider 1A to be a minimum for any model of Raspberry Pi.

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

The Raspberry Pi 4 should be used with a 3A power supply. This is in part because its greater processing power than earlier models requires more electrical power, but also because its two USB3 ports are able to supply up to 1.2A to high-power USB peripherals such as external USB drives.

If you are going to be using WiFi or USB peripherals that require significant amounts of power with pre-Raspberry Pi 4 models, you should get a power supply capable of 1.5A or even 2A. Also beware of very low-cost power supplies that might not provide an accurate or reliable 5V.


The Raspberry Pi 4 is the first Raspberry Pi to switch to using the more modern USB-C connector. Unlike the micro-USB connector used on earlier boards, this connector is reversable (Figure 1-7).

Figure 1-7. Power and video connections for the Raspberry Pi 3 (top) and 4 (bottom)

In Figure 1-7, you can see the USB-C power connector of a Raspberry Pi 4 below the micro-USB connector of a Raspberry Pi 3. As an aside, you can also see the pair of micro-HDMI video ports that replace the single full-sized HDMI connector.

Whether your Raspberry Pi uses a USB-C connector or a micro-USB connector, the power supply and connectors are actually the same as those found in many smartphone chargers. If they terminate in a micro-USB plug, they are almost certainly 5V (but check). The only question, then, is whether they can supply enough current.

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

  • They might become hot and be a potential fire risk.

  • They might simply fail.

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

If you are using a Raspberry Pi 3 or earlier, look for a power supply that says it can supply 1A or more. If it specifies a number of watts (W) rather than A, divide the number of watts by 5 to get the number of amps. So a 5V 10W power supply can supply 2A (2,000mA).

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 takes only as much current as it needs.

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

Figure 1-8. Raspberry Pi current consumption during booting

The newer Raspberry Pis (starting with the A+ and all the way through to the Pi 4) are far more power-efficient than the original Raspberry Pi 1 models, but when the processor is fully occupied and has a lot of peripherals attached, they can still reach similar current requirements, and, in the case of the Raspberry Pi 4, quite a lot more.

As you can see in Figure 1-8, if your Raspberry Pi is going to be on all the time, a Raspberry Pi 2 (and for that matter a Raspberry Pi 3) will run cooler and use a lot less power than the newest Raspberry Pi 4.

In Figure 1-8, you can see that the current rarely exceeds 700mA. 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 always 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

1.5 Selecting an Operating System


There are a number of different Raspberry Pi operating systems, and you are not sure which one to use.


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 (versions of Linux) specifically for that purpose (see Recipe 4.1).

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


If you are interested in trying out some different distributions, you can purchase some microSD cards, which are not expensive, and copy the various distributions onto them. If you do this, it is a good idea to keep your files that you don’t want to lose on a USB flash drive plugged into your Raspberry Pi.

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

1.6 Writing a microSD Card with NOOBS


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


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

Download the NOOBS archive file (the option NOOBS rather than NOOBS Lite), 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 an SD-to-microSD adapter.

After you have downloaded the NOOBS ZIP file, extract it and copy the folder contents onto the microSD card. Note that if the archive extracts to a folder called NOOBS_v2_9_0 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, a window like the one shown in Figure 1-9 will appear. From this screen, select Raspbian, and then click the Install button.

Figure 1-9 shows the options for a Raspberry Pi 4. If you are using NOOBS on a different Raspberry Pi, you will see a different range of options because only the options that will work with your model of Raspberry Pi will be shown.

Figure 1-9. NOOBS first screen

You will see 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-10).

Note this will take quite a long time. At some point, you will also be prompted to connect to your WiFi network.

Figure 1-10. NOOBS overwriting the SD card

When the file copying is complete, you will get the message Image applied successfully. When you press Return, the country information dialog box displays, in which you can chose your location options. Note that the Raspberry Pi, being a British product, this defaults to the UK. The Raspberry Pi will then reboot, and then a few configuration questions will appear. The first (Figure 1-11) prompts you to set the country and other location options.

Figure 1-11. Configuring Raspberry Pi after installation

Clicking Next prompts you to change your password from raspberry to something more secure. You are then asked if you want to check for updates. This requires an internet connection, so it won’t work unless you are connected to your network. If you are connected (either by WiFi or Ethernet), it’s a good idea to check for updates now. If not, you can always check later using Recipe 3.40.

After 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).


Unlike full NOOBS, the NOOBS Lite option does not actually contain Raspbian or any other distributions but rather downloads them after the microSD card is installed in the Raspberry Pi. To do this, your Raspberry Pi must have a connection to the internet. The NOOBS Lite installer (Figure 1-12) lets you chose a WiFi network. As soon as you’re connected, the NOOBS Lite installer will download a list of operating systems from which you can choose.

Figure 1-12. The NOOBs Lite installer

One interesting option here is Raspbian Lite. This is a pared-down version of Raspbian intended for use in headless Raspberry Pis (Recipe 1.9).

For NOOBS to install correctly onto a microSD card, the card must be formatted as FAT32. Most SD and microSD cards as supplied are already formatted in FAT32. If you are reusing an old card and need to format it as FAT32, use your operating system’s tool for formatting removable media. Sometimes, an SD card can stubbornly refuse to format; a good trick is to pop it into a digital camera and use the camera’s format option.

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

See Also

You can find more information on installing an operating system with NOOBS, including information about the different distributions available, at

If you need more control over what goes onto your SD cards, perhaps because you need to download and run a program automatically after installation, take a look at Recipe 1.8.

To create a headless Raspberry Pi with Raspbian Lite using PiBakery, see Recipe 1.9.

1.7 Installing an Operating System Without NOOBS


You want to put the operating system for your Raspberry Pi directly onto an SD card or other removable media.


In general, if you just want to set up a Pi with Raspbian, it’s much easier to follow Recipe 1.6 than this recipe.

However, you might want to write a disk image directly onto an SD card that is ready to run, rather than having to run an installer from the SD card on the Raspberry Pi. This might be because you want to use a nonstandard operating system or, as you will see in this recipe, you want the disk image to be on something other than an SD card—perhaps an external USB drive.

Whatever your reasons, the process of writing the disk image onto the SD card or other media is as follows:

  1. Using a Mac, Windows, or Linux computer (not your Raspberry Pi), download the disk image writing software Etcher.
  2. Still on your computer, download the disk image that you want to install. You will find a selection of official distributions at
  3. Insert the SD card (or whichever medium you want to install the operating system onto) into your computer. It’s also a good idea to disconnect any other removable media so that you don’t accidentally overwrite the wrong device.
  4. Start Etcher (Figure 1-13) and then select the image file. This will have the extension .iso, but you can download it as a ZIP file, which will also work.
  5. Click “Select drive” on Etcher and select the SD card or other removable media. Note that any data on this will be deleted, so be very careful not to select your computer’s main hard disk.
  6. Click Flash and wait while the image file is flashed onto the removable media.
Figure 1-13. Using Etcher to write a disk image

After the SD card or other removable media is prepared, you can connect it to your Raspberry Pi, and when the Raspberry Pi is powered up, it will boot into whatever operating system distribution you installed.


Vendors of hardware sometimes offer their own disk image that has support for their hardware built into it. It’s best to avoid using such images because doing so means that you will not get all the benefits of using a standard Raspbian distribution and all the preinstalled software. It also means that if you have a problem with a piece of software, it will be a lot more difficult to find support because you are using a nonstandard distribution.

See Also

For a good example of using this recipe to run Raspbian from a proper disk rather than an SD card, see Recipe 1.10.

1.8 Using PiBakery to Configure and Write an SD Card


You want to install Raspbian on microSD cards for lots of Raspberry Pis, but you don’t want to have to set up each one individually.


Download the PiBakery tool created by David Ferguson. This useful piece of software runs on your Mac or Windows computer and allows you to write an SD card (like Recipe 1.6) as well as automate the extra steps that usually follow basic installation, such as setting up your WiFi connection (Recipe 2.5) or changing the network name of your Raspberry Pi (Recipe 2.4).

Begin by downloading PiBakery. When you run the installer (Figure 1-14) you can choose either Raspbian Full or Raspbian Lite, or both. Unless you are thinking of using your Raspberry Pi without a keyboard, mouse, or monitor, you probably just need the Full version. Note the file sizes here—these are big!

Figure 1-14. Installing PiBakery

After you’ve installed PiBakery, run the PiBakery application. This opens a window, which you use to specify how you want the Raspbian microSD card to be configured by dragging blocks onto a canvas. In Figure 1-15, you can see how we have added an On Every Boot block from the Startup category and then added three more blocks connected to each other underneath it.

Figure 1-15. Configuring a microSD card image using PiBakery

The first block ensures that virtual network computing (VNC) (Recipe 2.8) is enabled. The second block sets up the WiFi connection, and the final block illustrates how you can enable the I2C interface (Recipe 9.3) for connecting external electronics.

When you are happy with the settings, insert a microSD card into your computer and then click the Write button. A prompt appears, asking you to select the SD card onto which Raspbian will be installed and to select Rasbian Lite or Raspbian Full (you will want Raspbian Full unless you are planning on making your Raspberry Pi headless—see Recipe 1.9). Clicking Start Write starts the process of installing Raspbian on the microSD card. When that has completed, you can eject the microSD card and put it in your Raspberry Pi, which should then boot up all ready for use, without any further setup being required.


PiBakery is great if you have a whole load of Raspberry Pis that you need to set up, perhaps in a classroom setting.

As well as the blocks we used here, there are lots of other blocks that do things like run programs automatically at startup and even download and install packages from the internet.

See Also

In Recipe 1.9, we use PiBakery again to setup a Raspberry Pi as headless (no keyboard, mouse, or monitor).

PiBakery should not be confused with the macOS tool ApplePi-Baker. If you have a Mac, ApplePi-Baker is a great way to back up and restore Raspberry Pi SD cards.

1.9 Using PiBakery to Configure a Headless Raspberry Pi


You want to set up a Raspberry Pi (perhaps a Raspberry Pi Zero W) without using a keyboard, mouse, or monitor (headless).


Use PiBakery (Recipe 1.8) to configure the microSD card with Raspbian Lite and WiFi credentials so that you can control your Raspberry Pi remotely using Secure Shell (SSH) (Recipe 2.7).

In this solution, let’s assume that you want to set up a Raspberry Pi Zero W so that it does the following:

  1. Gives itself the network name “PiZero” so that we can find it easily on our network.
  2. Connects to your WiFi and hence to the internet.
  3. Downloads and runs a Python script from the internet.

In reality, you might not need step 3, but it’s useful to know how to do such a thing.

You will also be able to connect to the Pi Zero W using SSH on your regular computer.

Begin by downloading and running PiBakery (see Recipe 1.8).

Next, put together a recipe that looks like Figure 1-16. Don’t forget to put your WiFi settings in. Note that SSH is automatically enabled.

Figure 1-16. Configuring a headless Raspberry Pi using PiBakery

The Python program to be run is from much later in the book (Recipe 10.1) and uses one of the Raspberry Pi’s GPIO pins to blink an LED on and off. To download this file on its own, enter the URL on the “Download file” line, as shown in Figure 1-16 (

PiBakery has a feature that allows you to load (import) and save configurations. You can load the configuration settings shown in Figure 1-16 into PiBakery by clicking the Import button and then navigating to the headless_blink.xml file within the folder PiBakery in the downloads for this book. You can find instructions for getting the book’s downloads at Recipe 3.22.

Notice in Figure 1-16 that two boot blocks are used. On the first boot, the hostname is set to piZero and WiFi is configured. The second block runs after subsequent boots, downloads the blink program, and then runs it.

So, after the microSD card has been put into the Pi Zero and booted up for the first time (just leave it to do its thing for a couple of minutes), unplug it and power it back up again. Now, if you have an LED connected to pin 18 (Figure 1-16), it should start blinking.


PiBakery is a really neat way of setting up a headless Raspberry Pi. However, as well as setting up a microSD card for first use, you can also modify configurations. So if you power down the Raspberry Pi we set up earlier, remove the SD card, and insert it back into your computer, PiBakery will detect that it has been used on the SD card before and offer to load up the configuration. Because a first boot has already taken place, only the on every boot will be shown. But you can edit this and make changes, or even add more blocks without having to write the SD card again.

See Also

For full documentation on PiBakery, see

1.10 Booting from a Real Hard Disk or USB Flash Drive


Your microSD card is too small and/or you are concerned about your entire operating system running on an SD card.


You can connect external USB flash drives, USB hard disks, or high-capacity SSDs, and they should be recognized by your Raspberry Pi for storage. However, if you want your Raspberry Pi to be able to boot from the external hard disk rather than just accessing it as a data store, there are a few steps you need to take:

  1. Invest in a Raspberry Pi 4 B with 4 GB of RAM. It’s worth it to have the extra computing power if you are going to add a proper disk. Also, booting from USB only became an option with the Raspberry Pi 3 and subsequent models.

  2. Start by following Recipe 1.7 to install Raspbian on your Pi using a microSD card.

  3. Unless you have just downloaded NOOBS, make sure your Pi is up to date by following Recipe 3.40.

  4. Configure the Raspberry Pi’s hardware so that it will boot from USB. This involves the use of something called the OTP (One Time Programming). As the name suggests, this makes a permanent change to the Raspberry Pi’s hardware, which you cannot then undo. So it’s particularly important to ensure that your Raspberry Pi is up to date (Recipe 3.40) and to be very careful when typing the following command into a Terminal session. Note the use of the | (bar or pipe symbol) in the middle of the command. You will normally find this symbol next to the left Shift key (Shift and backslash - \) on a Windows keyboard and near the Enter key on a Mac keyboard.


    Throughout the book, I use a $ at the start of each line where you are expected to type a command. The response from the command line will not be prefixed by anything; it will appear just as it does on the Raspberry Pi’s screen.

    $ echo program_usb_boot_mode=1 | sudo tee -a /boot/config.txt

    Shut down your Raspberry Pi (Recipe 1.16) and remove the microSD card.

  5. Attach the USB external disk (or flash drive) to your regular computer and follow Recipe 1.7 to install a disk image on it.

  6. Eject and unplug the USB drive from your regular computer and connect it to your Raspberry Pi.

  7. Power up your Raspberry Pi; it should begin to boot from the USB drive. You will receive a message saying that the root filesystem has been resized, and after a while the usual Pixel desktop interface will appear on the screen.


If you want to switch your Raspberry Pi’s main disk to a real disk while at the same time finding a good enclosure for your Raspberry Pi 2 and 3, one really nice solution is the DIY Pi Desktop kit from Element 14. This kit (shown in Figure 1-17) consists of the following:

  • A USB-to-mSATA (1.8 inch) adapter board that fits on top of your Raspberry Pi
  • A U-shaped USB connector that connects one of the Raspberry Pi’s USB ports to the USB-to-mSATA interface board
  • A heatsink for your Raspberry Pi’s processor chip
  • A stylish and compact case for the Raspberry Pi that includes a push button that acts as a start button to boot the Raspberry Pi

You are also going to need the following items to complete the kit:

  • A 1.8-inch mSATA disk drive. I would go for an SSD because they are more robust and use less power than a magnetic hard disk.
  • A good power supply. Because you are now going to need to power both the Raspberry Pi and a USB disk, you might need to upgrade your power supply. Element 14 recommends the use of a 5V 2.5A power supply (see also Recipe 1.4).
  • Optionally a Pi Camera (Recipe 1.17) if you want your Pi Desktop to have a webcam.
Figure 1-17. Element 14 DIY Pi Desktop kit

The supplied instruction leaflet for assembling the hardware for the kit is easy to follow, but before you get everything boxed up I would strongly recommend following the instructions in “Solution” before you fix the Raspberry Pi into the enclosure, because after it’s in, it’s very difficult to remove the microSD card from the Raspberry Pi.

Note that as of this writing, this case is not available for the Raspberry Pi 4.

See Also

This recipe is based on the Raspberry Pi instructions.

For more information on the DIY Pi Desktop, see

1.11 Connecting a DVI or VGA Monitor


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


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 are available for less than $5 if you search for “HDMI male to DVI female converter.”


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 can find cheaper alternatives on the internet, but often these won’t work.

1.12 Using a Composite Video Monitor/TV


The text on your low-resolution composite monitor is illegible.


You need to adjust the resolution of the Raspberry Pi for a small screen.

The Raspberry Pi has two types of video output: (1) HDMI, and (2) composite video from the audio jack, for which you need a special lead. Of these, the HDMI is by far the better-quality option. If you’re intending to use a composite video as your main screen, you might want to think again.

If you are using a composite video screen—say, because you need a really small screen—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 this file on your laptop or PC by inserting the SD card back into an SD card reader, or you can edit it (without having to remove it from your Raspberry Pi). Editing files on the Raspberry Pi itself is normally done using the nano editor. This is a little tricky, and I suggest you read Recipe 3.7 thoroughly before you try editing your first file. If you are happy to go ahead and edit the file using nano, enter the following command in a Terminal session:

$ sudo nano /boot/config.txt

Note that to save and exit nano, press Ctrl-X, then press Y (to confirm), and then press Enter.

If the text is too small to read, it’s best to remove the SD card from the Raspberry Pi and insert it into your computer. The file will then be in the top-level directory on the SD card, and you can use a text editor on your PC (such as Notepad++) to modify it.

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


Remove the # from the front of each line and change the two numbers to the width and height of your screen. Removing the # enables the line. In the following example, the lines have been modified to be 320 by 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.13.


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.4). 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.11 and 1.13 to adjust your picture when you’re using the HDMI video output.

1.13 Adjusting the Picture Size on Your Monitor


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


If your problem is that there is a large black border around the picture, you can make the screen fill the whole area of the monitor using the Raspberry Pi’s desktop Configuration tool (Figure 1-18). To open this, go to the Raspberry Pi menu (the one with the Raspberry Pi icon), select Preferences, and then click Raspberry Pi Configuration.

Figure 1-18. Using the Raspberry Pi Configuration tool to control underscan

Click the Disabled radio button next to Underscan. Note that the change will not take effect until you have clicked OK and then rebooted your Raspberry Pi.

If you have the opposite problem and your text extends off the edges of the screen, click Enabled for Underscan.

In a second step, you now need to edit the file /boot/config.txt. You can do this either by removing the SD card and mounting it on your PC or Mac or by editing the SD card on the Raspberry Pi. Editing files on the Raspberry Pi itself is normally done using the nano editor. This is a little tricky; I suggest you read Recipe 3.7 thoroughly before you try editing your first file. If you are happy to go ahead and edit the file using nano, enter the following command in a Terminal session:

$ 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-19, each beginning with #overscan.

rpck 0111
Figure 1-19. Adjusting overscan

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

To save and exit nano, press Ctrl-X, then press Y (to confirm), and then press Enter.

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 decrease the area of the screen that is used.


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

See Also

You can also configure underscanning using the raspi-config tool. For more information on this tool, go to

1.14 Maximizing Performance


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


If you have a Raspberry Pi 2, 3, or 4 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 consume a bit more power and run a little hotter (see the Discussion section that follows).

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 begin to get too hot. This is called throttling.

The easiest way make your Pi overclock is to use the Raspberry Pi Configuration tool. To open this, go to the Raspberry Pi menu, select Preferences, and then click Raspberry Pi Configuration. Next, click on the Performance tab (Figure 1-20).

Figure 1-20. Configuring overclocking using the Raspberry Pi Configuration tool

The overclock options available to you will vary depending on which model of Raspberry Pi you have. Select one of the options and then click OK. Changes will take effect when you reboot.

If you are running your Raspberry Pi without a monitor (Recipe 1.9), you can still change the overclocking, but you will need to jump around and learn about SSH in Recipe 2.7. After you are able to connect with SSH, run the raspi-config utility by issuing the following command in an SSH Terminal:

$ sudo raspi-config

Select the Overclock option. You are then presented with the options shown in Figure 1-21.

Figure 1-21. Configuring overclocking with the raspi-config tool from the command line

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


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

The test program was the following Python script. This just hammers the processor (that is, makes it work really hard) 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. However, 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
    return n * factorial(n-1)

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

print(after_time - before_time)

Note that we are jumping ahead a lot here, so if you are not familiar with Python, come back to this when you have read through Chapter 5.

Check out the results of the test in Table 1-3. The current and temperature were measured using test equipment.

Table 1-3. 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

1.15 Changing Your Password


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


After you install Raspbian onto your SD card, you are prompted to change your password, but you can skip this step. You can change the password any time using the Raspberry Pi Configuration utility. To open this, go to the Raspberry Pi menu, select Preferences, and then click Raspberry Pi Configuration. Click the System tab. There, you will find the Change Password option (Figure 1-22).

Figure 1-22. Changing your password with the Raspberry Pi Configuration tool

Changing your password is one setting for which you do not need to restart your Raspberry Pi for the change to take effect.


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 also change your password using the raspi-config tool (see

1.16 Shutting Down Your Raspberry Pi


You want to shut down your Raspberry Pi.


In the upper lefthand corner of the desktop, click the Raspberry menu. A dialog box opens, offering three shutdown options (Figure 1-23).


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


Reboots the Raspberry Pi.

Logs you out and displays a prompt to enter your login credentials so that you can log back in.
Figure 1-23. Shutting down your Raspberry Pi

You can also reboot using the Terminal by issuing the following command:

$ sudo reboot

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

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


It is better to shut down your Raspberry Pi as described than to simply pull out the power plug because your Raspberry Pi might 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

In Recipe 1.10 you will find an enclosure for Raspberry Pi that includes a power button.

For information on adding a Start button to your Raspberry Pi, see Recipe 12.14.

1.17 Installing the Raspberry Pi Camera Module


You want to use the Raspberry Pi Camera Module.


The Raspberry Pi Camera Module (Figure 1-25) is attached to a Raspberry Pi by a ribbon cable.

rpck 0117
Figure 1-25. The Raspberry Pi Camera Module

There are actually two basic versions of the Pi Camera: the original version 1 (as shown in Figure 1-25) and the newer higher-resolution version 2.

The ribbon cable attaches to a special connector between the audio and HDMI sockets on a Raspberry Pi 2, 3, or 4. To fit the cable onto your Pi, gently pull up the levers on either side of the connector so that they unlock, and then press the cable into the slot with the shiny metal 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-26).

Figure 1-26. Attaching a Raspberry Pi Camera Module to a Raspberry Pi 3 model B

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.

Note that the Raspberry Pi Zero requires a special cable or adapter because its Camera connector is smaller than that of a full-sized Raspberry Pi (see “Modules”).

The Camera Module requires some software configuration. The easiest way to configure it is to use the Raspberry Pi Configuration tool. To open this, go to the Raspberry Pi main menu, select Preferences, and then click Raspberry Pi Configuration. Click the Interfaces tab, and then set the Camera option to Enabled (Figure 1-27).

Figure 1-27. Enabling the camera using the Raspberry Pi Configuration tool

If you are using your Raspberry Pi remotely using SSH (see Recipe 2.7), you can still enable the camera by using raspi-config. To run raspi-config, enter the following command into a Terminal session:

$ sudo raspi-config

Select Interfacing Options, and you will see the Camera option (Figure 1-28).

Figure 1-28. Enabling the camera by using raspi-config from the command line

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

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 at the end of the line is the recording duration in milliseconds—in this case, 10 seconds.


Both raspistill 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.

The Raspberry Pi Camera version 2 has a resolution of 3280 x 2464 pixels and supports 1080p30, 720p60, and 640x480p90 video.

You can also buy a No Infra red (NoIR) version of the camera that has the infrared filter removed from the camera module to allow it to work at night under infrared illumination.

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

See Also

The RaspiCam documentation includes raspistill and raspivid.

1.18 Using Bluetooth


You want to use Bluetooth with your Raspberry Pi.


If you have a Raspberry Pi 3 or 4, the good news is that along with WiFi, you also get Bluetooth hardware. If you have an older Raspberry Pi, you can attach a USB Bluetooth adapter to it. However, in both cases, the software that you need for Bluetooth is now included in Raspbian.

If you have an older Raspberry Pi, be aware that 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-29 shows a Raspberry Pi 2 equipped with both a USB Bluetooth adapter (nearest to the camera) and a USB WiFi adapter.

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

Bluetooth features are integrated into the Raspbian Pixel desktop in much the same way as on a Mac. In the upper-right corner of the screen, you will see the Bluetooth icon (circled in (Figure 1-30). Click this to open a menu of Bluetooth options.

Figure 1-30. The Raspbian Bluetooth menu

If you want to connect a Bluetooth peripheral such as a keyboard, click Add Device. The Add New Device dialog box opens, showing a list of available devices with which you can connect, or “pair” (Figure 1-31).

Figure 1-31. Pairing a Bluetooth device

You can then select the device that you want to pair with and then follow the instructions that appear on your Raspberry Pi and the device you are pairing with.


You can pair phones, Bluetooth speakers, keyboards, and mice to your Raspberry Pi. I find that connecting a new Bluetooth device doesn’t always work the first time. So if you initially have a problem pairing with a device, try a few more times before you give up.

Most of the time, using the desktop interface to add Bluetooth devices to your Raspberry Pi system is convenient; however, you can also pair Bluetooth devices using the command-line interface.

To run Bluetooth commands from the command line, use the bluetoothctl command:

$ bluetoothctl
[NEW] Controller B8:27:EB:50:37:8E raspberrypi [default]
[NEW] Device 51:6D:A4:B8:D1:AA 51-6D-A4-B8-D1-AA
[NEW] Device E8:06:88:58:B2:B5 si’s keyboard #1

This scans for Bluetooth devices and also provides a pair command that will allow you to pair with the device using its ID—for example:

[bluetooth]# pair E8:06:88:58:B2:B5

See Also

For a list of Bluetooth adapters that are compatible with the Raspberry Pi, see

The bluedot software for Android phones allows you to control hardware attached to your Raspberry Pi using Bluetooth and your mobile phone. You will find an example of this in Recipe 10.7.

If you pair your Raspberry Pi with a Bluetooth speaker, you also need to set the speaker to be the output for sound (Recipe 15.2).

Get Raspberry Pi Cookbook, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.