Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

5 (7 reviews total)
By Tim Cox , Dr. Steven Lawrence Fernandes
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    Getting Started with a Raspberry Pi 3 Computer

About this book

Raspberry Pi 3 Cookbook for Python Programmers – Third Edition begins by guiding you through setting up Raspberry Pi 3, performing tasks using Python 3.6, and introducing the first steps to interface with electronics. As you work through each chapter, you will build your skills and apply them as you progress. You will learn how to build text classifiers, predict sentiments in words, develop applications using the popular Tkinter library, and create games by controlling graphics on your screen. You will harness the power of a built in graphics processor using Pi3D to generate your own high-quality 3D graphics and environments.

You will understand how to connect Raspberry Pi’s hardware pins directly to control electronics, from switching on LEDs and responding to push buttons to driving motors and servos. Get to grips with monitoring sensors to gather real-life data, using it to control other devices, and viewing the results over the internet. You will apply what you have learned by creating your own Pi-Rover or Pi-Hexipod robots. You will also learn about sentiment analysis, face recognition techniques, and building neural network modules for optical character recognition.

Finally, you will learn to build movie recommendations system on Raspberry Pi 3.

Publication date:
April 2018


Chapter 1. Getting Started with a Raspberry Pi 3 Computer

In this chapter, we will cover the following recipes:

  • Connecting peripherals to Raspberry Pi
  • Using NOOBS to set up your Raspberry Pi SD card
  • Networking and connecting your Raspberry Pi to the internet via the LAN connector
  • Using built-in Wi-Fi and Bluetooth on Raspberry Pi
  • Configuring your network manually
  • Networking directly to a laptop or computer
  • Networking and connecting your Raspberry Pi to the internet via a USB Wi-Fi dongle
  • Connecting to the internet through a proxy server
  • Connecting remotely to Raspberry Pi over the network using VNC
  • Connecting remotely to Raspberry Pi over the network using SSH (and X11 forwarding)
  • Sharing the home folder of Raspberry Pi with SMB
  • Keeping Raspberry Pi up to date


This chapter introduces Raspberry Pi and the process of setting it up for the first time. We will connect Raspberry Pi to a suitable display, power, and peripherals. We will install an operating system on an SD card. This is required for the system to boot. Next, we will ensure that we can connect successfully to the internet through a local network.

Finally, we will make use of the network to provide ways to remotely connect to and/or control Raspberry Pi from other computers and devices, as well as to ensure that the system is kept up to date.

Once you have completed the steps within this chapter, your Raspberry Pi will be ready for you to use for programming. If you already have your Raspberry Pi set up and running, ensure that you take a look through the following sections, as there are many helpful tips.

Introducing Raspberry Pi

The Raspberry Pi is a single-board computer created by the Raspberry Pi Foundation, a charity formed with the primary purpose of re-introducing low-level computer skills to children in the UK. The aim was to rekindle the microcomputer revolution of the 1980s, which produced a whole generation of skilled programmers.

Even before the computer was released at the end of February 2012, it was clear that Raspberry Pi had gained a huge following worldwide and, at the time of writing this book, has sold over 10 million units. The following image shows several different Raspberry Pi models:

The Raspberry Pi Model 3B, Model A+, and Pi Zero

What's with the name?

The name, Raspberry Pi, was a combination of the desire to create an alternative computer with a fruit-based name (such as Apple, BlackBerry, and Apricot) and a nod to the original concept of a simple computer that could be programmed using Python (shortened to Pi).

In this book, we will take this little computer, find out how to set it up, and then explore its capabilities chapter by chapter, using the Python programming language.

Why Python?

It is often asked, "Why has Python been selected as the language to use on Raspberry Pi?" The fact is that Python is just one of the many programming languages that can be used on Raspberry Pi.

There are many programming languages that you can choose, from high-level graphical block programming, such as Scratch, to traditional C, right down to BASIC, and even the raw machine code assembler. A good programmer often has to be code multilingual to be able to play to the strengths and weaknesses of each language to best meet the needs of their desired application. It is useful to understand how different languages (and programming techniques) try to overcome the challenge of converting what you want into what you get, as this is what you are trying to do as well while you program.

Python has been selected as a good place to start when learning about programming, as it provides a rich set of coding tools while still allowing simple programs to be written without fuss. This allows beginners to gradually be introduced to the concepts and methods on which modern programming languages are based without requiring them to know it all from the start. It is very modular with lots of additional libraries that can be imported to quickly extend the functionality. You will find that, over time, this encourages you to do the same, and you will want to create your own modules that you can plug into your own programs, thus taking your first steps into structured programming.

Python addresses formatting and presentation concerns. As indentation will add better readability, indents matter a lot in Python. They define how blocks of code are grouped together. Generally, Python is slow; since it is interpreted, it takes time to create a module while it is running the program. This can be a problem if you need to respond to time-critical events. However, you can precompile Python or use modules written in other languages to overcome this.

It hides the details; this is both an advantage and a disadvantage. It is excellent for beginners but can be difficult when you have to second-guess aspects such as datatypes. However, this in turn forces you to consider all the possibilities, which can be a good thing.

Python 2 and Python 3

A massive source of confusion for beginners is that there are two versions of Python on Raspberry Pi (Version 2.7 and Version 3.6), which are not compatible with each other, so code written for Python 2.7 may not run with Python 3.6 (and vice versa).

The Python Software Foundation is continuously working to improve and move forward with the language, which sometimes means they have to sacrifice backward compatibility to embrace new improvements (and, importantly, remove redundant and legacy ways of doing things).


Supporting Python 2 and Python 3There are many tools that will ease the transition from Python 2 to Python 3, including converters such as2to3, which will parse and update your code to use Python 3 methods. This process is not perfect, and in some cases you'll need to manually rewrite sections and fully retest everything. You can write the code and libraries that will support both. Theimport __future__statement allows you to import the friendly methods of Python 3 and run them using Python 2.7.

Which version of Python should you use?

Essentially, the selection of which version to use will depend on what you intend to do. For instance, you may require Python 2.7 libraries, which are not yet available for Python 3.6. Python 3 has been available since 2008, so these tend to be older or larger libraries that have not been translated. In many cases, there are new alternatives to legacy libraries; however, their support can vary.

In this book, we have used Python 3.6, which is also compatible with Python 3.5 and 3.3.

The Raspberry Pi family – a brief history of Pi

Since its release, Raspberry Pi has come in various iterations, featuring both small and large updates and improvements to the original Raspberry Pi Model B unit. Although it can be confusing at first, there are three basic types of Raspberry Pi available (and one special model).

The main flagship model is called Model B. This has all the connections and features, as well as the maximum RAM and the latest processor. Over the years, there have been several versions, most notably Model B (which had 256 MB and then 512 MB RAM) and then Model B+ (which increased the 26-pin GPIO to 40 pins, switched to using a microSD card slot, and had four USB ports instead of two). These original models all used the Broadcom BCM2835 system on chip (SOC), consisting of a single core 700 MHz ARM11 and VideoCore IV graphical processing unit (GPU).

The release of Raspberry Pi 2 Model B (also referred to as 2B) in 2015 introduced a new Broadcom BCM2836 SOC, providing a quad-core 32-bit ARM Cortex A7 1.2 GHz processor and GPU, with 1 GB of RAM. The improved SOC added support for Ubuntu and Windows 10 IoT. Finally, we had the latest Raspberry Pi 3 Model B, using another new Broadcom BCM2837 SOC, which provides a quad-core 64-bit ARM Cortex-A53 and GPU, alongside on-board Wi-Fi and Bluetooth.

Model A has always been targeted as a cut-down version. While having the same SOC as Model B, there are limited connections consisting of a single USB port and no wired network (LAN). Model A+ again added more GPIO pins and a microSD slot. However, the RAM was later upgraded to 512 MB of RAM and again there was only a single USB port/no LAN. The Broadcom BCM2835 SOC on Model A has not been updated so far (so is still a single core ARM11); however, a Model 3A (most likely using the BCM2837).

The Pi Zero is an ultra-compact version of Raspberry Pi intended for embedded applications where cost and space are a premium. It has the same 40-pin GPIO and microSD card slot as the other models, but lacks the on-board display (CSI and DSI) connection. It does still have HDMI (via a mini-HDMI) and a single micro USB on-the-go (OTG) connection. Although not present in the first revision of the Pi Zero, the most recent model also includes a CSI connection for the on-board camera.


Pi Zero was famously released in 2015 and was given away with Raspberry Pi foundation's magazine The MagPi, giving the magazine the benefit of being the first magazine to give away a computer on its cover! This did make me rather proud since (as you may have read in my biography at the start of this book) I was one of the founders of the magazine.

The special model is known as the compute module. This takes the form of a 200-pin SODIMM card. It is intended for industrial use or within commercial products, where all the external interfaces would be provided by a host/motherboard, into which the module would be inserted. Example products include the Slice Media Player (http://fiveninjas.com) and the OTTO camera. The current module uses the BCM2835, although an updated compute module (CM3).

The Raspberry Pi Wikipedia page provides a full list of the all different variants and their specifications:https://en.wikipedia.org/wiki/Raspberry_Pi#Specifications

Also, the Raspberry Pi product page gives you the details about the models available and the accessories' specifications:https://www.raspberrypi.org/products/

Which Pi to choose?

All sections of this book are compatible will all current versions of Raspberry Pi, but Model 3B is recommended as the best model to start with. This offers the best performance (particularly useful for the GPU examples in Chapter 7, Creating 3D Graphics, and the OpenCV examples used in Chapter 6, Detecting Edges and Contours in Images), lots of connections, and built-in Wi-Fi, which can be very convenient.

Pi Zero is recommended for projects where you want low power usage or reduced weight/size but do not need the full processing power of Model 3B. However, due to its ultra-low cost, Pi Zero is ideal for deploying a completed project after you have developed it.


Connecting to Raspberry Pi

There are many ways to wire up Raspberry Pi and use the various interfaces to view and control content. For typical use, most users will require power, display (with audio), and a method of input such as a keyboard and mouse. To access the internet, refer to the Networking and connecting your Raspberry Pi to the internet via the LAN connector or Using built-in Wi-Fi and Bluetooth on Raspberry Pi recipes.

Getting ready

Before you can use your Raspberry Pi, you will need an SD card with an operating system installed or with the New Out Of Box System (NOOBS) on it, as discussed in the Using NOOBS to set up your Raspberry Pi SD card recipe.

The following section will detail the types of devices you can connect to Raspberry Pi and, importantly, how and where to plug them in.

As you will discover later, once you have your Raspberry Pi set up, you may decide to connect remotely and use it through a network link, in which case you only need power and a network connection. Refer to the following sections: Connecting remotely to Raspberry Pi over the Network using VNC and Connecting Remotely to Raspberry Pi over the Network using SSH (and X11 Forwarding).

How to do it...

The layout of Raspberry Pi is shown in the following diagram:

The Raspberry Pi connection layout (Model 3 B, Model A+, and Pi Zero)

More information about the preceding figure is listed as follows:

  • Display: The Raspberry Pi supports the following three main display connections; if both HDMI and composite video are connected, it will default to HDMI only:
    • HDMI: For best results, use a TV or monitor that has an HDMI connection, thus allowing the best resolution display (1080p) and also digital audio output. If your display has a DVI connection, you may be able to use an adapter to connect through the HDMI. There are several types of DVI connection; some support analogue (DVI-A), some digital (DVI-D), and some both (DVI-I). Raspberry Pi is only able to provide a digital signal through the HDMI, so an HDMI-to-DVI-D adapter is recommended (shown with a tick mark in the following screenshot). This lacks the four extra analogue pins (shown with a cross mark in the following screenshot), thus allowing it to fit into both DVI-D and DVI-I type sockets:

HDMI-to-DVI connection (DVI-D adaptor)

If you wish to use an older monitor (with a VGA connection), an additional HDMI-to-VGA converter is required. Raspberry Pi also supports a rudimentary VGA adaptor (VGA Gert666 Adaptor), which is driven directly off of the GPIO pins. However, this does use up all but four pins of the 40-pin header (older 26-pin models will not support the VGA output):

HDMI-to-VGA adapter

    • Analogue: An alternative display method is to use the analogue composite video connection (via the phono socket); this can also be attached to an S-Video or European SCART adapter. However, the analogue video output has a maximum resolution of 640 x 480 pixels, so it is not ideal for general use:

3.5 mm phono analogue connections

When using the RCA connection or a DVI input, audio has to be provided separately by the analogue audio connection. To simplify the manufacturing process (by avoiding through-hole components), the Pi Zero does not have analogue audio or an RCA socket for analogue video (although they can be added with some modifications):

    • Direct Display DSI: A touch display produced by Raspberry Pi Foundation will connect directly into the DSI socket. This can be connected and used at the same time as the HDMI or analogue video output to create a dual display setup.
  • Stereo analogue audio (all except Pi Zero): This provides an analogue audio output for headphones or amplified speakers. The audio can be switched via Raspberry Pi configuration tool on the desktop between analog (stereo socket) and digital (HDMI), or via the command line using amixer or alsamixer.


To find out more information about a particular command in the Terminal, you can use the following man command before the terminal reads the manual (most commands should have one): man amixer Some commands also support the --help option for more concise help, shown as follows: amixer --help

  • Network (excluding models A and Pi Zero): The network connection is discussed in the Networking and connecting your Raspberry Pi to the internet via the LAN connector recipe later in this chapter. If we use the Model A Raspberry Pi, it is possible to add a USB network adapter to add wired or even wireless networking (refer to the Networking and connecting your Raspberry Pi to the internet via a USB Wi-Fi dongle recipe).
  • Onboard Wi-Fi and Bluetooth (Model 3 B only): The Model 3 B has built-in 802.11n Wi-Fi and Bluetooth 4.1; see the Using the built-in Wi-Fi and Bluetooth on Raspberry Pi recipe.
  • USB (1x Model A/Zero, 2x Model 1 B, 4x Model 2 B and 3 B): Using a keyboard and mouse:
    • Raspberry Pi should work with most USB keyboards and mice. You can also use wireless mice and keyboards, which use RF dongles. However, additional configuration is required for items that use the Bluetooth dongles.
    • If there is a lack of power supplied by your power supply or the devices are drawing too much current, you may experience the keyboard keys appearing to stick, and, in severe cases, corruption of the SD card.


USB power can be more of an issue with the early Model B revision 1 boards that were available prior to October 2012. They included additional Polyfuses on the USB output and tripped if an excess of 140 mA was drawn. The Polyfuses can take several hours or days to recover completely, thus causing unpredictable behavior to remain even when the power is improved. You can identify a revision 1 board, as it lacks the four mounting holes that are present in the later models.

    • Debian Linux (upon which Raspbian is based) supports many common USB devices, such as flash storage drives, hard-disk drives (external power may be required), cameras, printers, Bluetooth, and Wi-Fi adapters. Some devices will be detected automatically, while others will require drivers to be installed.
  • Micro USB power: The Raspberry Pi requires a 5V power supply that can comfortably supply at least 1,000 mA (1,500 mA or more is recommended, particularly with the more power-hungry Model 2 and Model 3) with a micro USB connection. It is possible to power the unit using portable battery packs, such as the ones suitable for powering or recharging tablets. Again, ensure that they can supply 5V at 1,000 mA or over.

You should aim to make all other connections to Raspberry Pi before connecting the power. However, USB devices, audio, and networks may be connected and removed while it is running, without problems.

There's more...

In addition to the standard primary connections you would expect to see on a computer, Raspberry Pi also has a number of other connections.

Secondary hardware connections

Each of the following connections provides additional interfaces for Raspberry Pi:

  • 20 x 2 GPIO pin header (Model A+, B+, 2 B, 3 B, and Pi Zero): This is the main 40-pin GPIO header of Raspberry Pi used for interfacing directly with hardware components. We use this connection in Chapters 6, Detecting Edges and Contours in Images, Chapter 7Creating 3D Graphics, Chapter 9, Using Python to Drive Hardware, and Chapter 10, Sensing and Displaying Real-world Data. The recipes in this book are also compatible with older models of Raspberry Pi that have a 13 x 2 GPIO pin header.
  • P5 8 x 2 GPIO pin header (Model 1 B revision 2.0 only): We do not use this in the book.
  • Reset connection: This is present on later models (no pins fitted). A reset is triggered when Pin 1 (reset) and Pin 2 (GND) are connected together. We use this in the A controlled shutdown button recipe in Chapter 9, Using Python to Drive Hardware.
  • GPU/LAN JTAG: The Joint Test Action Group (JTAG) is a programming and debugging interface used to configure and test processors. These are present on newer models as surface pads. A specialist JTAG device is required to use this interface. We do not use this in the book.
  • Direct camera CSI: This connection supports Raspberry Pi Camera Module. Note that the Pi Zero has a smaller CSI connector than the other models, so it requires a different ribbon connector.
  • Direct Display DSI: This connection supports a directly connected display, such as a 7-inch 800 x 600 capacitive touch screen.

Using NOOBS to set up your Raspberry Pi SD card

The Raspberry Pi requires the operating system to be loaded onto an SD card before it starts up. The easiest way to set up the SD card is to use NOOBS; you may find that you can buy an SD card with NOOBS already loaded on it.

NOOBS provides an initial start menu that provides options to install several of the available operating systems on to your SD card.

Getting ready

Since NOOBS creates a RECOVERY partition to keep the original installation images, an 8 GB SD card or larger is recommended. You will also need an SD card reader (experience has shown that some built-in card readers can cause issues, so an external USB type reader is recommended).

If you are using an SD card that you have used previously, you may need to reformat it to remove any previous partitions and data. NOOBS expects the SD card to consist of a single FAT32 partition.

If using Windows or macOS X, you can use the SD Association's formatter, as shown in the following screenshot (available at https://www.sdcard.org/downloads/formatter_4/):

Getting rid of any partitions on the SD card, using SD formatter

From the Option Setting dialog box, set FORMAT SIZE ADJUSTMENT. This will remove all the SD card partitions that were created previously.

If using Linux, you can use gparted to clear any previous partitions and reformat it as a FAT32 partition.

The full NOOBS package (typically just over 1 GB) contains Raspbian, the most popular Raspberry Pi operating system image built in. A lite version of NOOBS is also available that has no preloaded operating systems (although a smaller initial download of 20 MB and a network connection on Raspberry Pi are required to directly download the operating system you intend to use).

NOOBS is available at http://www.raspberrypi.org/downloads, with the documentation available at https://github.com/raspberrypi/noobs.

How to do it...

By performing the following steps, we will prepare the SD card to run NOOBS. This will then allow us to select and install the operating system we want to use:

  1. Get your SD card ready.
  2. On a freshly formatted or new SD card, copy the contents of the NOOBS_vX.zip file. When it has finished copying, you should end up with something like the following screenshot of the SD card:

NOOBS files extracted onto the SD card


The files may vary slightly with different versions of NOOBS, and the icons displayed may be different on your computer.

  1. You can now put the card into your Raspberry Pi, connect it to a keyboard and display, and turn the power on. Refer to the Connecting to Raspberry Pi recipe for details on what you need, and how to do this.

By default, NOOBS will display via the HDMI connection. If you have another type of screen (or you don't see anything), you will need to manually select the output type by pressing 1, 2, 3, or 4, according to the following functions:

  • Key 1 stands for the Standard HDMI mode (the default mode)
  • Key 2 stands for the Safe HDMI mode (alternative HDMI settings if the output has not been detected)
  • Key 3 stands for Composite PAL (for connections made via the RCA analogue video connection)
  • Key 4 stands for Composite NTSC (again, for connections via the RCA connector)

This display setting will also be set for the installed operating system.

After a short while, you will see the NOOBS selection screen that lists the available distributions (the offline version only includes Raspbian). There are many more distributions that are available, but only the selected ones are available directly through the NOOBS system. Click on Raspbian, as this is the operating system being used in this book.

Press Enter or click on Install OS, and confirm that you wish to overwrite all the data on the card. This will overwrite any distributions previously installed using NOOBS but will not remove the NOOBS system; you can return to it at any time by pressing Shift when you turn the power on.

It will take around 20 to 40 minutes to write the data to the card depending on its speed. When it completes and the Image Applied Successfully message appears, click on OK, and Raspberry Pi will start to boot into Raspberry Pi Desktop.

How it works...

The purpose of writing the image file to the SD card in this manner is to ensure that the SD card is formatted with the expected filesystem partitions and files required to correctly boot the operating system.

When Raspberry Pi powers up, it loads some special code contained within the GPU's internal memory (commonly referred to as binary blob by Raspberry Pi Foundation). The binary blob provides the instructions required to read the BOOT partition on the SD card, which (in the case of a NOOBS install) will load NOOBS from the RECOVERY partition. If at this point Shift is pressed, NOOBS will load the recovery and installation menu. Otherwise, NOOBS will begin loading the OS as specified by the preferences stored in the SETTINGS partition.

When loading the operating system, it will boot via the BOOT partition, using the settings defined in config.txt and options in cmdline.txt to finally load to the desktop on the root partition. Refer to the following diagram:

NOOBS creates several partitions on the SD card to allow the installation of multiple operating systems and to provide recovery

NOOBS allows the user to optionally install multiple operating systems on the same card and provides a boot menu to choose between them (with an option to set a default value in the event of a time-out period).

If you later add, remove, or re-install an operating system, ensure first that you make a copy of any files, including system settings you wish to keep, as NOOBS may overwrite everything on the SD card.

There's more...

When you power up Raspberry Pi for the first time directly, the desktop will be loaded. You can configure the system settings using the Raspberry Pi Configuration menu (under the Preferences menu on the Desktop or via the sudo raspi-config command). With this menu, you can make changes to your SD card or set up your general preferences:

Changing the default user password

Ensure that you change the default password for the pi user account once you have logged in, as the default password is well known. This is particularly important if you connect to public networks. You can do this with the passwd command, as shown in the following screenshot:

Setting a new password for the Pi user

This provides greater confidence because if you later connect to another network, only you will be able to access your files and take control of your Raspberry Pi.

Ensuring that you shut down safely

To avoid any data corruption, you must ensure that you correctly shut down Raspberry Pi by issuing a shutdown command, as follows:

sudo shutdown -h now

Or, use this one:

sudo halt

You must wait until this command completes before you remove power from Raspberry Pi (wait for at least 10 seconds after the SD card access light has stopped flashing).

You can also restart the system with the reboot command, as follows:

sudo reboot

Preparing an SD card manually

An alternative to using NOOBS is to manually write the operating system image to the SD card. While this was originally the only way to install the operating system, some users still prefer it. It allows the SD cards to be prepared before they are used in Raspberry Pi. It can also provide easier access to startup and configuration files, and it leaves more space available for the user (unlike NOOBS, a RECOVERY partition isn't included).

The default Raspbian image actually consists of two partitions, BOOT and SYSTEM, which will fit into a 2 GB SD card (4 GB or more is recommended).

You need a computer running Windows/Mac OS X/Linux (although it is possible to use another Raspberry Pi to write your card; be prepared for a very long wait).

Download the latest version of the operating system you wish to use. For the purpose of this book, it is assumed you are using the latest version of Raspbian available at http://www.raspberrypi.org/downloads.

Perform the following steps depending on the type of computer you plan to use to write to the SD card (the .img file you need is sometimes compressed, so before you start, you will need to extract the file).

The following steps are for Windows:

  1. Ensure that you have downloaded the Raspbian image, as previously detailed, and extracted it to a convenient folder to obtain an .img file.
  2. Obtain the Win32DiskImager.exe file available at http://www.sourceforge.net/projects/win32diskimager.
  3. Run Win32DiskImager.exe from your downloaded location.
  4. Click on the folder icon and navigate to the location of the .img file and click on Save.
  5. If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
  6. Select the Device drive letter that corresponds to your SD card from the small drop-down box. Double-check that this is the correct device (as the program will overwrite whatever is on the device when you write the image).


The drive letter may not be listed until you select a source image file.

  1. Finally, click on the Write button and wait for the program to write the image to the SD card, as shown in the following screenshot:

Manually writing operating system images to the SD card, using Disk Imager

  1. Once completed, you can exit the program. Your SD card is ready.

The following steps should work for the most common Linux distributions, such as Ubuntu and Debian:

  1. Using your preferred web browser, download the Raspbian image and save it in a suitable place.
  2. Extract the file from the file manager or locate the folder in the terminal and unzip the .img file with the following command:
unzip filename.zip
  1. If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
  2. Use the df -h command and identify the sdX identifier for the SD card. Each partition will be displayed as sdX1, sdX2, and so on, where X will be a, b, c, d, and so on for the device ID.
  3. Ensure that all the partitions on the SD card are unmounted using theumount /dev/sdXn command for each partition, where sdXn is the partition being unmounted.
  4. Write the image file to the SD card, with the following command:
sudo dd if=filename.img of=/dev/sdX bs=4M
  1. The process will take some time to write to the SD card, returning to the Terminal prompt when complete.
  2. Unmount the SD card before removing it from the computer, using the following command:
umount /dev/sdX1

The following steps should work for most of the versions of OS X:

  1. Using your preferred web browser, download the Raspbian image and save it somewhere suitable.
  2. Extract the file from the file manager or locate the folder in the terminal and unzip the .img file, with the following command:
unzip filename.zip
  1. If you haven't already done so, insert your SD card into your card reader and plug it into your computer.


  1. Use the diskutil list command and identify the disk# identifier for the SD card. Each partition will be displayed as disk#s1, disk#s2, and so on, where # will be 1, 2, 3, 4, and so on, for the device ID.


If rdisk# is listed, use this for faster writing (this uses a raw path and skips data buffering).

  1. Ensure that the SD card is unmounted using the unmountdisk /dev/diskX command, where diskX is the device being unmounted.
  2. Write the image file to the SD card, with the following command:
sudo dd if=filename.img of=/dev/diskX bs=1M
  1. The process will take some time to write to the SD card, returning to the Terminal prompt when complete.
  2. Unmount the SD card before removing it from the computer, using the following command:
unmountdisk /dev/diskX

Refer to the following diagram:

The boot process of a manually installed OS image

Expanding the system to fit in your SD card

A manually written image will be of a fixed size (usually made to fit the smallest-sized SD card possible). To make full use of the SD card, you will need to expand the system partition to fill the remainder of the SD card. This can be achieved using the Raspberry Pi Configuration tool.

Select Expand Filesystem, as shown in the following screenshot:

Raspberry Pi Configuration tool

Accessing the RECOVERY/BOOT partition

Windows and macOS X do not support the ext4 format, so when you read the SD card, only the File Allocation Table (FAT) partitions will be accessible. In addition, Windows only supports the first partition on an SD card, so if you've installed NOOBS, only the RECOVERY partition will be visible. If you've written your card manually, you will be able to access the BOOT partition.

The data partition (if you installed one via NOOBS) and the root partition are in ext4 format and won't usually be visible on non-Linux systems.


If you do need to read files from the SD card using Windows, a freeware program, Linux Reader (available at www.diskinternals.com/linux-reader) can provide read-only access to all of the partitions on the SD card.

Access the partitions from Raspberry Pi. To view the currently mounted partitions, use df, as shown in the following screenshot:

The result of the df command

To access the BOOT partition from within Raspbian, use the following command:

cd /boot/

To access the RECOVERY or data partition, we have to mount it by performing the following steps:

  1. Determine the name of the partition as the system refers to it by listing all the partitions, even the unmounted ones. The sudo fdisk -l command lists the partitions, as shown in the following screenshot:

NOOBS installation and data partition

The following table shows the names of partitions and their meanings

Partition name





(Extended partition) contains (root, data, BOOT)


(ext4) root






If you have installed additional operating systems on the same card, the partition identifiers shown in the preceding table will be different.

  1. Create a folder and set it as the mount point for the partition; for the RECOVERYpartition, use the following command:
mkdir ~/recoverysudo mount -t vfat /dev/mmcblk0p1 ~/recovery

To ensure that they are mounted each time the system is started, perform the following steps:

  1. Add the sudo mount commands to /etc/rc.local before exit 0. If you have a different username, you will need to change pi to match:
sudo nano /etc/rc.localsudo mount -t vfat /dev/mmcblk0p1 /home/pi/recovery
  1. Save and exit by pressing Ctrl + X, Y, and Enter.


Commands added to /etc/rc.local will be run for any user who logs on to Raspberry Pi. If you only want the drive to be mounted for the current user, the commands can be added to .bash_profile instead.

If you have to install additional operating systems on the same card, the partition identifiers shown here will be different.

Using the tools to back up your SD card in case of failure

You can use Win32 Disk Imager to make a full backup image of your SD card by inserting your SD card into your reader, starting the program, and creating a filename to store the image in. Simply click on the Read button instead to read the image from the SD card and write it to a new image file.

To make a backup of your system, or to clone to another SD card using Raspberry Pi, use the SD Card Copier (available from the desktop menu via the Accessories | SD Card Copier).

Insert an SD card into a card reader into a spare USB port of Raspberry Pi and select the new storage device, as shown in the following screenshot:

SD Card Copier program

Before continuing, the SD Card Copier will confirm that you wish to format and overwrite the target device and, if there is sufficient space, make a clone of your system.

The dd command can similarly be used to back up the card, as follows:

  • For Linux, replacing sdX with your device ID, use this command:
sudo dd if=/dev/sdX of=image.img.gz bs=1M
  • For OS X, replacing diskX with your device ID, use the following command:
sudo dd if=/dev/diskX of=image.img.gz bs=1M
  • You can also use gzip and split to compress the contents of the card and split them into multiple files, if required, for easy archiving, as follows:
sudo dd if=/dev/sdX bs=1M | gzip -c | split -d -b 2000m - image.img.gz
  • To restore the split image, use the following command:
sudo cat image.img.gz* | gzip -dc | dd of=/dev/sdX bs=1M

Networking and connecting your Raspberry Pi to the internet via an Ethernet port, using a CAT6 Ethernet cable

The simplest way to connect Raspberry Pi to the internet is by using the built-in LAN connection on the Model B. If you are using a Model A Raspberry Pi, a USB-to-LAN adapter can be used (refer to the There's more... section of the Networking and connecting your Raspberry Pi to the internet via a USB Wi-Fi dongle recipe for details of how to configure this).

Getting ready

You will need access to a suitable wired network, which will be connected to the internet, and a standard network cable (with an RJ45 type connector for connecting to Raspberry Pi).

How to do it...

Many networks connect and configure themselves automatically using the Dynamic Host Configuration Protocol (DHCP), which is controlled by the router or switch. If this is the case, simply plug the network cable into a spare network port on your router or network switch (or wall network socket if applicable).

Alternatively, if a DHCP server is not available, you shall have to configure the settings manually (refer to the There's more... section for details).

You can confirm this is functioning successfully with the following steps:

  1. Ensure that the two LEDs on either side of Raspberry Pi light up (the left orange LED indicates a connection and the green LED on the right shows activity by flashing). This will indicate that there is a physical connection to the router and that the equipment is powered and functioning.
  2. Test the link to your local network using the ping command. First, find out the IP address of another computer on the network (or the address of your router, perhaps, often or Now, on the Raspberry Pi Terminal, use the ping command (the -c 4 parameter is used to send just four messages; otherwise, press Ctrl + C to stop) to ping the IP address, as follows:
sudo ping -c 4
  1. Test the link to the internet (this will fail if you usually connect to the internet through a proxy server) as follows:
sudo ping www.raspberrypi.org -c 4
  1. Finally, you can test the link back to Raspberry Pi by discovering the IP address using hostname -I on Raspberry Pi. You can then use the ping command on another computer on the network to ensure it is accessible (using Raspberry Pi's IP address in place of www.raspberrypi.org). The Windows version of the ping command will perform five pings and stop automatically, and will not need the -c 4 option.

If the aforementioned tests fail, you will need to check your connections and then confirm the correct configuration for your network.

There's more...

If you find yourself using your Raspberry Pi regularly on the network, you won't want to have to look up the IP address each time you want to connect to it.

On some networks, you may be able to use Raspberry Pi's hostname instead of its IP address (the default is raspberrypi). To assist with this, you may need some additional software, such as Bonjour, to ensure hostnames on the network are correctly registered. If you have macOS X, you will have Bonjour running already.

On Windows, you can either install iTunes (if you haven't got it), which also includes the service, or you can install it separately (via the Apple Bonjour Installer available from https://support.apple.com/kb/DL999). Then you can use the hostname, raspberrypi or raspberrypi.local, to connect to Raspberry Pi over the network. If you need to change the hostname, then you can do so with the Raspberry Pi configuration tool, shown previously.

Alternatively, you may find it helpful to fix the IP address to a known value by manually setting the IP address. However, remember to switch it back to use DHCP when connecting to another network.

Some routers will also have an option to set a Static IP DHCP address, so the same address is always given to Raspberry Pi (how this is set will vary depending on the router itself).

Knowing your Raspberry Pi's IP address or using the hostname is particularly useful if you intend to use one of the remote access solutions described later on, which avoids the need for a display.


Using built-in Wi-Fi and Bluetooth on Raspberry Pi

Many home networks provide a wireless network over Wi-Fi; if you have Raspberry Pi 3, then you can make use of the on-board Broadcom Wi-Fi to connect to it. Raspberry Pi 3 also supports Bluetooth, so you can connect most standard Bluetooth devices and use them like you would on any other computer.

This method should also work for any supported USB Wi-Fi and Bluetooth devices; see the Networking and connecting your Raspberry Pi to the internet via a USB Wi-Fi dongle recipe for extra help on identifying devices and installing firmware (if required).

Getting ready

The latest version of Raspbian includes helpful utilities to quickly and easily configure your Wi-Fi and Bluetooth through the graphical interface.


Note: If you need to configure the Wi-Fi via the command line, then see the Networking and connecting your Raspberry Pi to the internet via a USB Wi-Fi dongle recipe for details.

Wi-Fi and Bluetooth configuration applications

You can use the built-in Bluetooth to connect a wireless keyboard, a mouse, or even wireless speakers. This can be exceptionally helpful for projects where additional cables and wires are an issue, such as robotic projects, or when Raspberry Pi is installed in hard-to-reach locations (acting as a server or security camera).

How to do it...

Here are the various methods.

Connecting to your Wi-Fi network

To configure your Wi-Fi connection, click on the networking symbol to list the local available Wi-Fi networks:

Wi-Fi listing of the available access points in the area

Select the required network (for example, Demo) and, if required, enter your password (also known as a Pre Shared Key):

Providing the password for the access point

After a short while, you should see that you have connected to the network and the icon will change to a Wi-Fi symbol. If you encounter problems, ensure you have the correct password/key:

Successful connection to an access point

That is it; it's as easy as that!

You can now test your connection and ensure it is working by using the web browser to navigate to a website or by using the following command in the terminal:

sudo ping www.raspberrypi.com

Connecting to Bluetooth devices

To start, we need to put the Bluetooth device into discoverable mode by clicking on the Bluetooth icon and selecting Make Discoverable. You will also need to make the device you want to connect to discoverable and ready to pair; this may vary from device to device (such as pressing a pairing button):

Setting the Bluetooth up as discoverable

Next, select Add Device... and select the target device and Pair:

Selecting and pairing the required device

The pairing process will then start; for example, the BTKB-71DB keyboard will need the pairing code 467572 to be entered onto the keyboard for the pairing to complete. Other devices may use default pairing codes, often set to 0000, 1111, 1234, or similar:

Following the instructions to pair the device with the required pairing code

Once the process has completed, the device will be listed and will connect automatically each time the devices are present and booted.


Configuring your network manually

If your network does not include a DHCP server or it is disabled (typically, these are built into most modern ADSL/cable modems or routers), you may need to configure your network settings manually.

Getting ready

Before you start, you will need to determine the network settings for your network.

You will need to find out the following information from your router's settings or another computer connected to the network:

  • IPv4 address: This address will need to be selected to be similar to other computers on the network (typically, the first three numbers should match, that is, 192.168.1.X if netmask is, but it should not already be used by another computer. However, avoid x.x.x.255 as the last address, since this is reserved as a broadcast address.
  • Subnet mask: This number determines the range of addresses the computer will respond to (for a home network, it is typically, which allows up to 254 addresses). This is also sometimes referred to as the netmask.
  • Default gateway address: This address is usually your router's IP address, through which the computers connect to the internet.
  • DNS servers: The Domain Name Service (DNS) server converts names into IP addresses by looking them up. Usually, they will already be configured on your router, in which case you can use your router's address. Alternatively, your Internet Service Provider (ISP) may provide some addresses, or you can use Google's public DNS servers at the addresses and These are also called nameservers in some systems.

For Windows, you can obtain this information by connecting to the internet and running the following command:

ipconfig /all

Locate the active connection (usually called Local Area Connection 1 or similar if you are using a wired connection, or if you are using Wi-Fi, it is called a wireless network connection) and find the information required, as follows:

The ipconfig/all command shows useful information about your network settings

For Linux and macOS X, you can obtain the required information with the following command (note that it is ifconfig rather than ipconfig):


The DNS servers are called nameservers and are usually listed in the resolv.conf file. You can use the less command as follows to view its contents (press Q to quit when you have finished viewing it):

less /etc/resolv.conf

How to do it...

To set the network interface settings, edit /etc/network/interfaces using the following code:

sudo nano /etc/network/interfaces

Now perform the following steps:

  1. We can add the details for our particular network, the IP address number we want to allocate to it, the netmask address of the network, and the gateway address, as follows:
iface eth0 inet static  address  netmask  gateway
  1. Save and exit by pressing Ctrl + X, Y, and Enter.
  2. To set the name servers for DNS, edit /etc/resolv.conf using the following code:
sudo nano /etc/resolv.conf
  1. Add the addresses for your DNS servers as follows:
  1. Save and exit by pressing Ctrl + X, Y, and Enter.

There's more...

You can configure the network settings by editing cmdline.txt in the BOOT partition and adding settings to the startup command line with ip.

The ip option takes the following form:

  • The client-ip option is the IP address you want to allocate to Raspberry Pi
  • The gw-ip option will set the gateway server address if you need to set it manually
  • The netmask option will directly set the netmask of the network
  • The hostname option will allow you to change the default raspberrypi hostname
  • The device option allows you to specify a default network device if more than one network device is present
  • The autoconf option allows the automatic configuration to be switched on or off

Networking directly to a laptop or computer

It is possible to connect Raspberry Pi LAN port directly to a laptop or computer using a single network cable. This will create a local network link between the computers, allowing all the things you can do if connected to a normal network without the need for a hub or a router, including connection to the internet, if Internet Connection Sharing (ICS) is used, as follows:


Make use of Raspberry Pi, with just a network cable, a standard imaged SD card, and power.

ICS allows Raspberry Pi to connect to the internet through another computer. However, some additional configuration is required for the computers to communicate across the link, as Raspberry Pi does not automatically allocate its own IP address.

We will use the ICS to share a connection from another network link, such as a built-in Wi-Fi on a laptop. Alternatively, we can use a direct network link (refer to the Direct network link section under the There's more... section) if the internet is not required or if the computer has only a single network adapter.


Although this setup should work for most computers, some setups are more difficult than the others. For additional information, see www.pihardware.com/guides/direct-network-connection.

Getting ready

You will need Raspberry Pi with power and a standard network cable.


Raspberry Pi Model B LAN chip includes Auto-MDIX (Automatic Medium-Dependent Interface Crossover). Removing the need to use a special crossover cable (a special network cable wired so that the transmit lines connect to receive lines for direct network links), the chip will decide and change the setup as required automatically.

It may also be helpful to have a keyboard and monitor available to perform additional testing, particularly if this is the first time you have tried this.

To ensure that you can restore your network settings to their original values, you should check whether it has a fixed IP address or the network is configured automatically.

To check the network settings on Windows 10, perform these steps:

  1. Open Settings from the start menu, then select Network and Internet, then Ethernet, and click on Change adapter options from the list of Related Settings.

To check the network settings on Windows 7 and Vista, perform the following steps:

  1. Open Network and Sharing Center from the Control Panel and click on Change adapter settings on the left-hand side.
  2. To check the network settings on Windows XP, open Network Connections from the Control Panel.
  3. Find the item that relates to your wired network adapter (by default, this is usually called Ethernet or Local Area Connection, as shown in the following screenshot):

Locating your wired network connection

  1. Right-click on its icon and click on Properties. A dialog box will appear, as shown in this screenshot:

Selecting the TCP/IP properties and checking the settings

  1. Select the item called Internet Protocol (TCP/IP) or Internet Protocol Version 4 (TCP/IPv4) if there are two versions (the other is Version 6), and click on the Properties button.
  2. You can confirm that your network is set by using automatic settings or a specific IP address (if so, take note of this address and the remaining details as you may want to revert the settings at a later point).

To check the network settings on Linux, perform the following steps:

  1. Open the Network Settings dialog box and select Configure Interface. Refer to the following screenshot:

Linux Network Settings dialog box

  1. If any settings are manually set, ensure you take note of them so that you can restore them later if you want.

To check the network settings on macOS X, perform the following steps:

  1. Open System Preferences and click on Networks. You can then confirm whether the IP address is allocated automatically (using DHCP) or not.


  1. Ensure that if any settings are manually set you take note of them so you can restore them later if you want to. Refer to the following screenshot:

OS X Network Settings dialog box

If you just need to access or control Raspberry Pi without an internet connection, refer to the Direct network link section in the There's more...section.

How to do it...

First, we need to enable ICS on our network devices. In this case, we will be sharing the internet, which is available on Wireless Network Connection through the Ethernet connection to Raspberry Pi.

For Windows, perform these steps:

  1. Return to the list of network adapters, right-click on the connection that links to the internet (in this case, the WiFi or Wireless Network Connection device), and click on Properties:

Locating your wired network connection

  1. At the top of the window, select the second tab (in Windows XP, it is called Advanced; in Windows 7 and Windows 10, it is called Sharing), as shown in the following screenshot:

Selecting the TCP/IP properties and noting the allocated IP address

  1. In the Internet Connection Sharing section, check the box for Allow other network users to connect through this computer's Internet connection (if present, use the drop-down box to select the Home networking connection: option as Ethernet or Local Area Connection). Click on OK and confirm whether you previously had a fixed IP address set for Local Area Connection.

For macOS X, to enable the ICS, perform the following steps:

  1. Click on System Preferences and then click on Sharing.
  2. Click on Internet Sharing and select the connection from which we want to share the internet (in this case, it will be the Wi-Fi AirPort). Then select the connection that we will connect Raspberry Pi to (in this case, Ethernet).

For Linux to enable the ICS, perform the following steps:

  1. From the System menu, click on Preferences and then on Network Connections. Select the connection you want to share (in this case, Wireless) and click on Edit or Configure. In the IPv4 Settings tab, change the Method option to Shared to other computers.

The IP address of the network adapter will be the Gateway IP address to be used on Raspberry Pi, and will be assigned an IP address within the same range (it will all match, except the last number). For instance, if the computer's wired connection now has, the Gateway IP of Raspberry Pi will be and its own IP address might be set to

Fortunately, thanks to updates in the operating system, Raspbian will now automatically allocate a suitable IP address to join the network and set the gateway appropriately. However, unless we have a screen attached to Raspberry Pi or scan for devices on our network, we do not know what IP address Raspberry PI has given itself.

Fortunately (as mentioned in the Networking and connecting your Raspberry Pi to the internet via the LAN connector recipe in the There's more... section), Apple's Bonjour software will automatically ensure hostnames on the network are correctly registered. As stated previously, if you have a Mac OS X, you will have Bonjour running already. On Windows, you can either install iTunes, or you can install it separately (available from https://support.apple.com/kb/DL999). By default, the hostname raspberrypi can be used.

We are now ready to test the new connection, as follows:

  1. Connect the network cable to Raspberry Pi and the computer's network port, and then power up Raspberry Pi, ensuring that you have re-inserted the SD card if you previously removed it. To reboot Raspberry Pi, if you edited the file there, use sudo reboot to restart it.
  2. Allow a minute or two for Raspberry Pi to fully power up. We can now test the connection.


  1. From the connected laptop or computer, test the connection by pinging with the hostname of Raspberry Pi, as shown in the following command (on Linux or OS X, add -c 4 to limit to four messages or press Ctrl + C to exit):
ping raspberrypi

Hopefully, you will find you have a working connection and receive replies from the Raspberry Pi.

If you have a keyboard and a screen connected to Raspberry Pi, you can perform the following steps:

  1. You can ping the computer in return (for example, from Raspberry Pi Terminal as follows:
sudo ping -c 4
  1. You can test the link to the internet by using ping to connect to a well-known website as follows, assuming you do not access the internet through a proxy server:
sudo ping www.raspberrypi.org -c 4

If all goes well, you will have full internet available through your computer to Raspberry Pi, allowing you to browse the web as well as update and install new software.

If the connection fails, perform the following steps:

  1. Repeat the process, ensuring that the first three sets of numbers match with Raspberry Pi and the network adapter IP addresses.
  2. You can also check that when Raspberry Pi powers up, the correct IP address is being set using the following command:
hostname -I
  1. Check your firewall settings to ensure your firewall is not blocking internal network connections.

How it works...

When we enable ICS on the primary computer, the operating system will automatically allocate a new IP address to the computer. Once connected and powered up, Raspberry Pi will set itself to a compatible IP address and use the primary computer IP address as an Internet Gateway.

By using Apple Bonjour, we are able to use raspberrypi hostname to connect to Raspberry Pi from the connected computer.

Finally, we check whether the computer can communicate over the direct network link to Raspberry Pi, back the other way, and also through to the internet.

There's more...

If you do not require the internet on Raspberry Pi, or your computer has only a single network adapter, you can still connect the computers together through a direct network link. Refer to the following diagram:

Connecting and using Raspberry Pi with just a network cable, a standard imaged SD card, and power

Direct network link

For a network link to work between two computers, they need to be using the same address range. The allowable address range is determined by the subnet mask (for example, or would mean all IP addresses should be the same except for the last two, or just the last number in the IP address; otherwise, they will be filtered).

To use a direct link without enabling ICS, check the IP settings of the adapter you are going to connect to and determine whether it is automatically allocated or fixed to a specific IP address.

Most PCs connected directly to another computer will allocate an IP address in the range 169.254.X.X (with a subnet mask of However, we must ensure that the network adaptor is set to Obtain an IP address automatically.

For Raspberry Pi to be able to communicate through the direct link, it needs to have an IP address in the same address range, 169.254.X.X. As mentioned before, Raspberry Pi will automatically give itself a suitable IP address and connect to the network.

Therefore, assuming we have Apple Bonjour (mentioned previously), we only need to know the hostname given to Raspberry Pi (raspberrypi).

See also

If you don't have a keyboard or screen connected to Raspberry Pi, you can use this network link to remotely access Raspberry Pi just as you would on a normal network (just use the new IP address you have set for the connection). Refer to the Connecting remotely to Raspberry Pi over the network using VNC and Connecting remotely to Raspberry Pi over the network using SSH (and X11 Forwarding) recipes.

There is lots of additional information available on my website, https://pihw.wordpress.com/guides/direct-network-connection, including additional troubleshooting tips and several other ways to connect to your Raspberry Pi without needing a dedicated screen and keyboard.


Networking and connecting your Raspberry Pi to the internet via a USB Wi-Fi dongle

By adding a USB Wi-Fi dongle to Raspberry Pi's USB port, even models without built-in Wi-Fi can connect to and use the Wi-Fi network.

Getting ready

You will need to obtain a suitable USB Wi-Fi dongle, and, in some cases, you may require a powered USB hub (this will depend on the hardware version of Raspberry Pi you have and the quality of your power supply). General suitability of USB Wi-Fi dongles will vary depending on the chipset that is used inside and the level of Linux support available. You may find that some USB Wi-Fi dongles will work without installing additional drivers (in which case you can jump to configuring it for the wireless network).

A list of supported Wi-Fi adapters is available at http://elinux.org/RPi_USB_Wi-Fi_Adapters.

You will need to ensure that your Wi-Fi adapter is also compatible with your intended network; for example, it supports the same types of signals 802.11bgn and the encryptions WEP, WPA, and WPA2 (although most networks are backward compatible).

You will also need the following details of your network:

  • Service set identifier (SSID): This is the name of your Wi-Fi network and should be visible if you use the following command:
sudo iwlist scan | grep SSID
  • Encryption type and key: This value will be None, WEP, WPA, or WPA2, and the key will be the code you normally enter when you connect your phone or laptop to the wireless network (sometimes, it is printed on the router).

You will require a working internet connection (that is, wired Ethernet) to download the required drivers. Otherwise, you may be able to locate the required firmware files (they will be the .deb files) and copy them to Raspberry Pi (that is, via a USB flash drive; the drive should be automatically mounted if you are running in desktop mode). Copy the file to a suitable location and install it, using the following command:

sudo apt-get install firmware_file.deb

How to do it...

This task has two stages: first, we identify and install firmware for the Wi-Fi adapter, and then we need to configure it for the wireless network.

We will try to identify the chipset of your Wi-Fi adapter (the part that handles the connection); this may not match the actual manufacturer of the device.

An approximate list of supported firmware can be found with this command:

sudo apt-cache search wireless firmware

This will produce results similar to the following output (disregarding any results without firmware in the package title):

atmel-firmware - Firmware for Atmel at76c50x wireless networking chips.firmware-atheros - Binary firmware for Atheros wireless cardsfirmware-brcm80211 - Binary firmware for Broadcom 802.11 wireless cardsfirmware-ipw2x00 - Binary firmware for Intel Pro Wireless 2100, 2200 and 2915firmware-iwlwifi - Binary firmware for Intel PRO/Wireless 3945 and 802.11n cardsfirmware-libertas - Binary firmware for Marvell Libertas 8xxx wireless cardsfirmware-ralink - Binary firmware for Ralink wireless cardsfirmware-realtek - Binary firmware for Realtek wired and wireless network adapterslibertas-firmware - Firmware for Marvell's libertas wireless chip series (dummy package)zd1211-firmware - Firmware images for the zd1211rw wireless driver

To find out the chipset of your wireless adapter, plug the Wi-Fi-adapter into Raspberry Pi, and from the terminal, run the following command:

dmesg | grep 'Product:|Manufacturer:'


This command stitches together two commands into one. First, dmesg displays the message buffer of the kernel (this is an internal record of system events that have occurred since power on, such as detected USB devices). You can try the command on its own to observe the complete output. The | (pipe) sends the output to the grep command; grep 'Product:|Manufacturer' checks it and only returns lines that contain Product or Manufacturer (so we should get a summary of any items that are listed as Product and Manufacturer). If you don't find anything or want to see all your USB devices, try the grep 'usb' command instead.

This should return something similar to the following output—in this case, I've got a ZyXEL device, which has a ZyDAS chipset (a quick Google search reveals that zd1211-firmware is for ZyDAS devices):

[    1.893367] usb usb1: Product: DWC OTG Controller[    1.900217] usb usb1: Manufacturer: Linux 3.6.11+ dwc_otg_hcd[    3.348259] usb 1-1.2: Product: ZyXEL G-202[    3.355062] usb 1-1.2: Manufacturer: ZyDAS

Once you have identified your device and the correct firmware, you can install it as you would any other package available through apt-get (where zd1211-firmware can be replaced with your required firmware). This is shown in the following command:

sudo apt-get install zd1211-firmware

Remove and reinsert the USB Wi-Fi dongle to allow it to be detected and the drivers loaded. We can now test whether the new adapter is correctly installed with ifconfig. The output is shown as follows:

wlan0     IEEE 802.11bg  ESSID:off/any          Mode:Managed  Access Point: Not-Associated   Tx-Power=20 dBm          Retry  long limit:7   RTS thr:off   Fragment thr:off          Power Management:off

The command will show the network adapters present on the system. For Wi-Fi, this is usually wlan0 or wlan1 and so on if you have installed more than one. If not, double-check the selected firmware and perhaps try an alternative or check on the site for troubleshooting tips.

Once we have the firmware installed for the Wi-Fi adapter, we will need to configure it for the network we wish to connect to. We can use the GUI as shown in the previous recipe, or we can manually configure it through the Terminal, as shown in the following steps:

  1. We will need to add the wireless adapter to the list of network interfaces, which is set in /etc/network/interfaces, as follows:
sudo nano -c /etc/network/interfaces 

Using the previous wlan# value in place of wlan0 if required, add the following command:

allow-hotplug wlan0iface wlan0 inet manualwpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

When the changes have been made, save and exit by pressing Ctrl + X, Y, and Enter.

  1. We will now store the Wi-Fi network settings of our network in the wpa_supplicant.conf file (don't worry if your network doesn't use the wpa encryption; it is just the default name for the file):
sudo nano -c /etc/wpa_supplicant/wpa_supplicant.conf

It should include the following:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 

The network settings can be written within this file as follows (that is, if the SSID is set as theSSID):

    • If no encryption is used, use this code:
    • With the WEP encryption (that is, if the WEP key is set as theWEPkey), use the following code:
    • For the WPA or WPA2 encryption (that is, if the WPA key is set as theWPAkey), use the following code:
  1. You can enable the adapter with the following command (again, replace wlan0 if required):
sudo ifup wlan0

Use the following command to list the wireless network connections:


You should see your wireless network connected with your SSID listed, as follows:

wlan0     IEEE 802.11bg  ESSID:"theSSID"          Mode:Managed  Frequency:2.442 GHz  Access Point: 
       00:24:BB:FF:FF:FF          Bit Rate=48 Mb/s   Tx-Power=20 dBm          Retry  long limit:7   RTS thr:off   Fragment thr:off          Power Management:off          Link Quality=32/100  Signal level=32/100          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0          Tx excessive retries:0  Invalid misc:15   Missed beacon:0

If not, adjust your settings and use sudo ifdown wlan0 to switch off the network interface, and then sudo ifup wlan0 to switch it back on. This will confirm that you have successfully connected to your Wi-Fi network.

  1. Finally, we will need to check whether we have access to the internet. Here, we have assumed that the network is automatically configured with DHCP and no proxy server is used. If not, refer to the Connecting to the internet through a proxy server recipe.

Unplug the wired network cable, if still connected, and see whether you can ping the Raspberry Pi website, as follows:

sudo ping www.raspberrypi.org


If you want to quickly know the IP address currently in use by Raspberry Pi, you can use hostname -I, or to find out which adapter is connected to which IP address, use ifconfig.

There's more...

The Model A version of Raspberry Pi does not have a built-in network port, so to get a network connection, a USB network adapter will have to be added (either a Wi-Fi dongle, as explained in the preceding section, or a LAN-to-USB adapter, as described in the following section).

Using USB wired network adapters

Just like USB Wi-Fi, the adapter support will depend on the chipset used and the drivers available. Unless the device comes with Linux drivers, you may have to search the internet to obtain the suitable Debian Linux drivers.

If you find a suitable .deb file, you can install it with the following command:

sudo apt-get install firmware_file.deb

Also, check using ifconfig, as some devices will be supported automatically, appear as eth1 (or eth0 on Model A), and be ready for use immediately.


Connecting to the internet through a proxy server

Some networks, such as ones within workplaces or schools, often require you to connect to the internet through a proxy server.

Getting ready

You will need the address of the proxy server you are trying to connect to, including the username and password, if one is required.

You should confirm that Raspberry Pi is already connected to the network and that you can access the proxy server.

Use the ping command to check this, as follows:

ping proxy.address.com -c 4

If this fails (you get no responses), you will need to ensure your network settings are correct before continuing.

How to do it...

  1. Create a new file using nano as follows (if there is already some content in the file, you can add the code at the end):
sudo nano -c ~/.bash_profile
  1. To allow basic web browsing through programs such as Midori while using a proxy server, you can use the following script:
function proxyenable { 
# Define proxy settings 
# Login name (leave blank if not required): 
# Login Password (leave blank to prompt): 
#If login specified - check for password 
if [[ -z $LOGIN_USER ]]; then 
  #No login for proxy 
  #Login needed for proxy Prompt for password -s option hides input 
  if [[ -z $LOGIN_PWD ]]; then 
    read -s -p "Provide proxy password (then Enter):" LOGIN_PWD 
  PROXY_FULL=$LOGIN_USER:[email protected]$PROXY_ADDR 
#Web Proxy Enable: http_proxy or HTTP_PROXY environment variables 
export http_proxy="http://$PROXY_FULL/" 
export HTTP_PROXY=$http_proxy 
export https_proxy="https://$PROXY_FULL/" 
export HTTPS_PROXY=$https_proxy 
export ftp_proxy="ftp://$PROXY_FULL/" 
export FTP_PROXY=$ftp_proxy 
#Set proxy for apt-get 
sudo cat <<EOF | sudo tee /etc/apt/apt.conf.d/80proxy > /dev/null 
Acquire::http::proxy "http://$PROXY_FULL/"; 
Acquire::ftp::proxy "ftp://$PROXY_FULL/"; 
Acquire::https::proxy "https://$PROXY_FULL/"; 
#Remove info no longer needed from environment 
echo Proxy Enabled 
function proxydisable { 
#Disable proxy values, apt-get and git settings 
unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY 
unset ftp_proxy FTP_PROXY 
sudo rm /etc/apt/apt.conf.d/80proxy 
echo Proxy Disabled 
  1. Once done, save and exit by pressing Ctrl + X, Y, and Enter.


The script is added to the user's own .bash_profile file, which is run when that particular user logs in. This will ensure that the proxy settings are kept separately for each user. If you want all users to use the same settings, you can add the code to /etc/rc.local instead (this file must have exit 0 at the end).

How it works...

Many programs that make use of the internet will check for the http_proxy or HTTP_PROXY environment variables before connecting. If they are present, they will use the proxy settings to connect through. Some programs may also use the HTTPS and FTP protocols, so we can set the proxy setting for them here too.


If a username is required for the proxy server, a password will be prompted for. It is generally not recommended to store your passwords inside scripts unless you are confident that no one else will have access to your device (either physically or through the internet).

The last part allows any programs that execute using the sudo command to use the proxy environment variables while acting as the super user (most programs will try accessing the network using normal privileges first, even if running as a super user, so it isn't always needed).

There's more...

We also need to allow the proxy settings to be used by some programs, which use superuser permissions while accessing the network (this will depend on the program; most don't need this). We need to add the commands into a file stored in /etc/sudoers.d/ by performing the following steps:

  1. Use the following command to open a new sudoer file:
sudo visudo -f /etc/sudoers.d/proxy
  1. Enter the following text in the file (on a single line):
Defaults env_keep += "http_proxy HTTP_PROXY https_proxy HTTPS_PROXY ftp_proxy FTP_PROXY"
  1. Once done, save and exit by pressing Ctrl + X, Y, and Enter; don't change the proxy.tmp filename (this is normal for visudo; it will change it to proxy when finished).
  2. If prompted What now?, there is an error in the command. Press X to exit without saving and retype the command.
  3. After a reboot (using sudo reboot), you will be able to use the following commands to enable and disable the proxy respectively:


It is important to use visudo here, as it ensures the permissions of the file are created correctly for the sudoers directory (read only by the root user).


Connecting remotely to Raspberry Pi over the network using VNC

Often, it is preferable to remotely connect to and control Raspberry Pi across the network, for instance, using a laptop or desktop computer as a screen and keyboard, or while Raspberry Pi is connected elsewhere, perhaps even connected to some hardware it needs to be close to.

VNC is just one way in which you can remotely connect to Raspberry Pi. It will create a new desktop session that will be controlled and accessed remotely. The VNC session here is separate from the one that may be active on Raspberry Pi's display.

Getting ready

Ensure that your Raspberry Pi is powered up and connected to the internet. We will use the internet connection to install a program using apt-get. This is a program that allows us to find and install applications directly from the official repositories.

How to do it...

  1. First, we need to install the TightVNC server on Raspberry Pi with the following commands. It is advisable to run an update command first to get the latest version of the package you want to install, as follows:
sudo apt-get updatesudo apt-get install tightvncserver
  1. Accept the prompt to install and wait until it completes. To start a session, use the following command:
vncserver :1
  1. The first time you run this, it will ask you to enter a password (of no more than eight characters) to access the desktop (you will use this when you connect from your computer).

The following message should confirm that a new desktop session has been started:

New 'X' desktop is raspberrypi:1

If you do not already know the IP address of Raspberry Pi, use hostname -I and take note of it.

Next, we need to run a VNC client. VNC Viewer is suitable program, which is available at http://www.realvnc.com/ and should work on Windows, Linux, and OS X.

When you run VNC Viewer, you will be prompted for the Server address and Encryption type. Use the IP address of your Raspberry Pi with :1. That is, for the IP address, use the address.

You can leave the Encryption type as Off or Automatic.

Depending on your network, you may be able to use the hostname; the default is raspberrypi, that is raspberrypi:1.

You may have a warning about not having connected to the computer before or having no encryption. You should enable encryption if you are using a public network or if you are performing connections over the internet (to stop others from being able to intercept your data).

There's more...

You can add options to the command line to specify the resolution and also the color depth of the display. The higher the resolution and color depth (can be adjusted to use 8-bits to 32-bits per pixel to provide low or high color detail), the more data has to be transferred through the network link. If you find the refresh rate a little slow, try reducing these numbers as follows:

vncserver :1 -geometry 1280x780 -depth 24

To allow the VNC server to start automatically when you switch on, you can add the vncserver command to .bash_profile (this is executed each time Raspberry Pi starts).

Use the nano editor as follows (the -c option allows the line numbers to be displayed):

sudo nano -c ~/.bash_profile

Add the following line to the end of the file:

vncserver :1

The next time you power up, you should be able to remotely connect using VNC from another computer.


Connecting remotely to Raspberry Pi over the network using SSH (and X11 forwarding)

An Secure Shell (SSH) is often the preferred method for making remote connections, as it allows only the Terminal connections and typically requires fewer resources.

An extra feature of SSH is the ability to transfer the X11 data to an X Windows server running on your machine. This allows you to start programs that would normally run on Raspberry Pi desktop, and they will appear in their own Windows on the local computer, as follows:

X11 forwarding on a local display

X11 forwarding can be used to display applications which are running on Raspberry Pi on a Windows computer.

Getting ready

If you are running the latest version of Raspbian, SSH, and X11 forwarding will be enabled by default (otherwise, double-check the settings explained in the How it works... section).

How to do it...

Linux and OS X have built-in support for X11 forwarding, but if you are using Windows, you will need to install and run the X Windows server on your computer.

Download and run xmingfrom the Xming site (http://sourceforge.net/projects/xming/).

Install xming, following the installation steps, including the installation of PuTTY if you don't have it already. You can also download PuTTY separately from http://www.putty.org/.

Next, we need to ensure that the SSH program we use has X11 enabled when we connect.

For Windows, we shall use PuTTY to connect to Raspberry Pi.

In the PuTTY Configurationdialog box, navigate to Connection | SSH | X11 and tick the checkbox for EnableX11 forwarding. If you leave the X display location option blank, it will assume the default Server 0:0 as follows (you can confirm the server number by moving your mouse over the Xming icon in the system tray when it is running):

Enabling X11 forwarding within the PuTTY configuration

Enter the IP address of Raspberry Pi in the Session settings (you may also find that you can use Raspberry Pi's hostname here instead; the default hostname is raspberrypi).

Save the setting using a suitable name, RaspberryPi, and click on Open to connect to your Raspberry Pi.

You are likely to see a warning message pop up stating you haven't connected to the computer before (this allows you to check whether you have everything right before continuing):

Opening an SSH connection to Raspberry Pi using PuTTY

For OS X or Linux, click on Terminal to open a connection to Raspberry Pi.

To connect with the default pi username, with an IP address of, use the following command; the -X option enables X11 forwarding:

ssh -X [email protected]

All being well, you should be greeted with a prompt for your password (remember the default value for the pi user is raspberry).

Ensure that you have Xming running by starting the Xming program from your computer's Start menu. Then, in the Terminal window, type a program that normally runs within Raspberry Pi desktop, such as leafpad or scratch. Wait a little while and the program should appear on your computer's desktop (if you get an error, you have probably forgotten to start Xming, so run it and try again).

How it works...

X Windows and X11 is what provides the method by which Raspberry Pi (and many other Linux-based computers) can display and control graphical Windows as part of a desktop.

For X11 forwarding to work over a network connection, we need both SSH and X11 forwarding enabled on Raspberry Pi. Perform the following steps:

  1. To switch on (or off) SSH, you can access Raspberry Pi Configuration program under the Preferences menu on the Desktop and click on SSH within the Interfaces tab, as shown in the following screenshot (SSH is often enabled by default for most distributions to help allow remote connections without needing a monitor to configure it):

The advanced settings menu in the raspi-config tool

  1. Ensure that X11 forwarding is enabled on Raspberry Pi (again, most distributions now have this enabled by default).
  2. Use nano with the following command:
sudo nano /etc/ssh/sshd_config
  1. Look for a line in the /etc/ssh/sshd_config file that controls X11 forwarding and ensure that it says yes (with no # sign before it), as follows:
X11Forwarding yes
  1. Save if required by pressing Ctrl + X, Y, and Enter and reboot (if you need to change it) as follows:
sudo reboot

There's more...

SSH and X11 forwarding is a convenient way to control Raspberry Pi remotely; we will explore some additional tips on how to use it effectively in the following sections.

Running multiple programs with X11 forwarding

If you want to run an X program, but still be able to use the same Terminal console for other stuff, you can run the command in the background with & as follows:

leafpad &

Just remember that the more programs you run, the slower everything will get. You can switch to the background program by typing fg and check for background tasks with bg.

Running as a desktop with X11 forwarding

You can even run a complete desktop session through X11, although it isn't particularly user friendly and VNC will produce better results. To achieve this, you have to use lxsession instead of startx (in the way you would normally start the desktop from the Terminal).

An alternative is to use lxpanel, which provides the program menu bar from which you can start and run programs from the menu as you would on the desktop.

Running Pygame and Tkinter with X11 forwarding

You can get the following error (or similar) when running the Pygame or Tkinter scripts:

_tkinter.TclError: couldn't connect to display "localhost:10.0"

In this case, use the following command to fix the error:

sudo cp ~/.Xauthority ~root/

Sharing the home folder of Raspberry Pi with SMB

When you have Raspberry Pi connected to your network, you can access the home folder by setting up file sharing; this makes it much easier to transfer files and provides a quick and easy way to back up your data. Server Message Block (SMB) is a protocol that is compatible with Windows file sharing, OS X, and Linux.

Getting ready

Ensure that you have Raspberry Pi powered and running with a working connection to the internet.

You will also need another computer on the same local network to test the new share.

How to do it...

First, we need to install samba, a piece of software that handles folder sharing in a format that is compatible with Windows sharing methods:

  1. Ensure that you use update as follows to obtain the latest list of available packages:
sudo apt-get updatesudo apt-get install samba

The install will require around 20 MB of space and take a few minutes.

  1. Once the installation has completed, we can make a copy of the configuration file as follows to allow us to restore defaults if needed:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backupsudo nano /etc/samba/smb.conf

Scroll down and find the section named Authentication; change the # security = user line to security = user.

As described in the file, this setting ensures that you have to enter your username and password for Raspberry Pi in order to access the files (this is important for shared networks).

Find the section called Share Definitions and [homes], and change the read only = yes line to read only = no.

This will allow us to view and also write files to the shared home folder. Once done, save and exit by pressing Ctrl + X, Y, and Enter.


If you have changed the default user from pi to something else, substitute it in the following instructions.

  1. Now, we can add pi (the default user) to use samba:
sudo pdbedit -a -u pi
  1. Now, enter a password (you can use the same password as your login or select a different one, but avoid using the default Raspberry password, which would be very easy for someone to guess). Restart samba to use the new configuration file, as follows:
sudo /etc/init.d/samba restart[ ok ] Stopping Samba daemons: nmbd smbd.[ ok ] Starting Samba daemons: nmbd smbd.
  1. To test, you will need to know either Raspberry Pi's hostname (the default hostname is raspberrypi) or its IP address. You can find both of these with the following command:
  1. For the IP address, add -I:
hostname -I

On another computer on the network, enter the \raspberrypipi address in the explorer path.

Depending on your network, the computer should locate Raspberry Pi on the network and prompt for a username and password. If it can't find the share using the hostname, you can use the IP address directly, where should be changed to match the IP address \


Keeping Raspberry Pi up to date

The Linux image used by Raspberry Pi is often updated to include enhancements, fixes, and improvements to the system, as well as adding support for new hardware or changes made to the latest board. Many of the packages that you install can be updated too.

This is particularly important if you plan on using the same system image on another Raspberry Pi board (particularly a newer one), as older images will lack support for any wiring changes or alternative RAM chips. New firmware should work on older Raspberry Pi boards, but older firmware may not be compatible with the latest hardware.

Fortunately, you need not reflash your SD card every time there is a new release, since you can update it instead.

Getting ready

You will need to be connected to the internet to update your system. It is always advisable to make a backup of your image first (and at a minimum, make a copy of your important files).

You can check your current version of firmware with the uname -a command, as follows:

Linux raspberrypi 4.4.9-v7+ #884 SMP Fri May 6 17:28:59 BST 2016 armv7l GNU/Linux

The GPU firmware can be checked using the /opt/vc/bin/vcgencmd version command, as follows:

   May  6 2016 13:53:23Copyright (c) 2012 Broadcomversion 0cc642d53eab041e67c8c373d989fef5847448f8 (clean) (release)

This is important if you are using an older version of firmware (pre-November 2012) on a newer board, since the original Model B board was only 254 MB RAM. Upgrading allows the firmware to make use of the extra memory if available.

The free -h command will detail the RAM available to the main processor (the total RAM is split between the GPU and ARM cores) and will give the following output:

             total       used       free     shared    buffers     cachedMem:          925M       224M       701M       7.1M        14M       123M-/+ buffers/cache:        86M       839MSwap:          99M         0B        99M

You can then recheck the preceding output following a reboot to confirm that they have been updated (although they may have already been the latest).

How to do it...

  1. Before running any upgrades or installing any packages, it is worth ensuring you have the latest list of packages in the repository. The update command gets the latest list of available software and versions:
sudo apt-get update
  1. If you just want to obtain an upgrade of your current packages, upgrade will bring them all up to date:
sudo apt-get upgrade
  1. To ensure that you are running the latest release of Raspbian, you can run dist-upgrade (be warned: this can take an hour or so depending on the amount that needs to be upgraded). This will perform all the updates that upgrade will perform but will also remove redundant packages and clean up:
sudo apt-get dist-upgrade

Both methods will upgrade the software, including the firmware used at boot and startup (bootcode.bin and start.elf).

  1. To update the firmware, the following command can be used:
sudo rpi-update

There's more...

You will often find that you will want to perform a clean installation of your setup, however, this will mean you will have to install everything from scratch. To avoid this, I developed the Pi-Kitchen project (https://github.com/PiHw/Pi-Kitchen), based on the groundwork of Kevin Hill. This aims to provide a flexible platform for creating customized setups that can be automatically deployed to an SD card:

Pi Kitchen allows Raspberry Pi to be configured before powering up

The Pi-Kitchen allows a range of flavors to be configured, which can be selected from the NOOBS menu. Each flavor consists of a list of recipes, each providing a specific function or feature to the final operating system. Recipes can range from setting up custom drivers for Wi-Fi devices, to mapping shared drives on your network, to providing a fully functional web server out of the box, all combining to make your required setup.

This project is in beta, developed as a proof of concept, but once you have everything configured, it can be incredibly useful to deploy fully working setups directly onto an SD card. Ultimately, the project could be combined with Kevin Hill's advanced version of NOOBS, called PINN Is Not NOOBS (PINN), which aims to allow extra features for advanced users, such as allowing operating systems and configurations to be stored on your network or on an external USB memory stick.

About the Authors

  • Tim Cox

    Tim Cox lives in England with his wife and two young daughters and works as a software engineer. His passion for programming stems from a Sinclair Spectrum that sparked his interest in computers and electronics. At university, he earned a BEng in Electronics and Electrical Engineering, and into a career in developing embedded software for a range of industries.

    Supporting the vision behind the Raspberry Pi, to encourage a new generation of engineers, Tim co-founded the MagPi magazine (the official magazine for the Raspberry Pi) and produces electronic kits through his site PiHardware.

    Browse publications by this author
  • Dr. Steven Lawrence Fernandes

    Dr. Steven Lawrence Fernandes holds a bachelor's degree in electronics and communication engineering, a master's degree in microelectronics, and a Ph.D. in computer vision and machine learning. His Ph.D work "Match composite sketch with drone images" has received patent notification (Patent Application Number: 2983/CHE/2015). has post the doctoral research experience working in deep learning at the University of Alabama at Birmingham, USA. He received the prestigious US award from the Society for Design and Process Science for his outstanding service contributions in 2017 and Young Scientist Award by Vision Group on Science and Technology in 2014. He has also received a research grant from the Institution of Engineers.

    Browse publications by this author

Latest Reviews

(7 reviews total)
Alles Bestens! Wird sich beizeiten bewähren.
Muy buen libro, con ejemplos muy practicos.
Python is a language found in many places.

Recommended For You

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
Unlock this book and the full library FREE for 7 days
Start now