Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi for  Python Programmers  Cookbook

You're reading from   Raspberry Pi for Python Programmers Cookbook Over 60 recipes that harness the power of the Raspberry Pi together with Python programming and create enthralling and captivating projects

Arrow left icon
Product type Paperback
Published in Oct 2016
Publisher
ISBN-13 9781785288326
Length 510 pages
Edition 2nd Edition
Languages
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with a Raspberry Pi Computer FREE CHAPTER 2. Starting with Python Strings, Files, and Menus 3. Using Python for Automation and Productivity 4. Creating Games and Graphics 5. Creating 3D Graphics 6. Using Python to Drive Hardware 7. Sense and Display Real-World Data 8. Creating Projects with the Raspberry Pi Camera Module 9. Building Robots 10. Interfacing with Technology A. Hardware and Software List
Index

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 onto 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 Mac OS 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 ready

Get 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 the 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 the 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:
    How to do it…

    NOOBS files extracted onto the SD card

    Note

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

  3. 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 up the 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 the Raspberry Pi will start to boot into the 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 the Raspberry Pi powers up, it loads some special code contained within the GPU's internal memory (commonly referred to as binary blob by the 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:

How it works…

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

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

If you later add, remove, or reinstall 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 the Raspberry Pi for the first time, it will start directly with the desktop. You can now configure the system settings using the Raspberry Pi Configuration program (under the Preferences menu on the Desktop or via the sudo raspi-config command), which will allow you to perform changes to your SD card and set up your general preferences.

There's more…

Raspberry Pi Configuration program

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:

Changing the default user password

Setting a new password for the pi user

This gives 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 the 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 the 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 the 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).

    Note

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

  7. 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:
    Preparing an SD card manually

    Manually write operating system images to the SD card using Disk Imager

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
    
  3. If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
  4. 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.
  5. Ensure that all the partitions on the SD card are unmounted using the umount /dev/sdXn command for each partition, where sdXn is the partition being unmounted.
  6. Write the image file to the SD card with the following command:
    sudo dd if=filename.img of=/dev/sdX bs=4M
    
  7. The process will take some time to write to the SD card, returning to the terminal prompt when complete.
  8. 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
    
  3. If you haven't already done so, insert your SD card into your card reader and plug it into your computer.
  4. 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.

    Note

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

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

    Refer to the following image:

    Preparing an SD card manually

    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:

Expanding the system to fit in your SD card

The Raspberry Pi Configuration tool

Accessing the RECOVERY/BOOT partition

Windows and Mac OS 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.

Note

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 the Raspberry Pi. To view the currently mounted partitions, use df, as shown in the following screenshot:

Accessing the RECOVERY/BOOT partition

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:
    Accessing the RECOVERY/BOOT partition

    The partition table of a NOOBS install of Raspbian and data partition

    mmcblk0p1

    (vfat) RECOVERY

    mmcblk0p2

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

    mmcblk0p5

    (ext4) root

    mmcblk0p6

    (vfat) BOOT

    mmcblk0p7

    (ext4) SETTINGS

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

  2. Create a folder and set it as the mount point for the partition, as follows:
    • For the RECOVERY partition, use the following command:
      mkdir ~/recovery
      sudo 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.local
    sudo mount -t vfat /dev/mmcblk0p1 /home/pi/recovery
    
  2. Save and exit by pressing Ctrl + X, Y, and Enter.

Note

Commands added to /etc/rc.local will be run for any user who logs on to the 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 the 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 the Raspberry Pi and select the new storage device, as shown in the following screenshot:

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

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
    
You have been reading a chapter from
Raspberry Pi for Python Programmers Cookbook - Second Edition
Published in: Oct 2016
Publisher:
ISBN-13: 9781785288326
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image