Search icon CANCEL
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 By Example

You're reading from   Raspberry Pi By Example Start building amazing projects with the Raspberry Pi right out of the box

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher
ISBN-13 9781785285066
Length 294 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Arush Kakkar Arush Kakkar
Author Profile Icon Arush Kakkar
Arush Kakkar
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Introduction to Raspberry Pi and Python FREE CHAPTER 2. Minecraft Pi 3. Building Games with PyGame 4. Working with a Webcam and Pi Camera 5. Introduction to GPIO Programming 6. Creating Animated Movies with Raspberry Pi 7. Introduction to Computer Vision 8. Creating Your Own Motion Detection and Tracking System 9. Grove Sensors and the Raspberry Pi 10. Internet of Things with the Raspberry Pi 11. Build Your Own Supercomputer with Raspberry Pi 12. Advanced Networking with Raspberry Pi 13. Setting Up a Web Server on the Raspberry Pi 14. Network Programming in Python with the Pi A. Newer Raspberry Pi Models Index

Setting up the Raspberry Pi

We need the following hardware to set up a Pi.

  • Raspberry Pi 2 Model B (hereafter, this will be referred only as Pi).
  • Power Supply: A micro USB power supply.

    Considering that we are going for slightly power-intensive usage of our Pi (such as connecting Pi Camera, webcam, and third-party sensors for Pi), a 5V 2A power supply is recommended. The micro USB pin is shown in the following image:

    Setting up the Raspberry Pi

    Note

    You can find a similar one online at http://www.adafruit.com/product/1995.

  • A standard USB keyboard
  • A MicroSD card and a MicroSD to SD card converter

    We need a minimum 4 GB Micro SD card.

  • A USB mouse
  • A monitor

    You can use either an HDMI monitor or a standard VGA monitor.

  • A monitor connection cable and converter

    If you are using HDMI monitor, then an HDMI cable will be sufficient. If you are using a VGA monitor, then you need to use an HDMI to VGA converter with a VGA cable. Some special changes need to be made to the /boot/config.txt file if you're using a VGA monitor, which will be explained in the next section.

    Setting up the Raspberry Pi

    Note

    You can find a similar one online at https://www.adafruit.com/products/1151.

  • A Windows, Linux, or Mac OS computer with a MicroSD card reader and an Internet connection

Preparing MicroSD card manually

This is the original way to install an OS into a MicroSD card, and many users, including me, still prefer it. It allows the SD card to be prepared manually before it is used and it allows easier access to configuration files such as /boot/config.txt, which we might have to modify in a few cases before booting up. The default Raspbian image consists of only two partitions, BOOT and SYSTEM, which will fit into a 2 GB card. However, I recommend that you use a minimum 4 GB card to be on safe side. Choosing an 8 GB card will be adequate for most of the applications we are going to develop in this book.

The following are the instructions for Windows users:

  1. Download the Win32DiskImager installer, which is available at http://sourceforge.net/projects/win32diskimager/files/latest/download and then install it.
  2. Download the installable version of WinZip, which is available at http://www.winzip.com/prod_down.html, and install it.
  3. Go to http://www.raspberrypi.org/downloads and download the latest image of Raspbian. It will be a compressed file in the ZIP format and will need to be extracted.
  4. Extract the ZIP file using WinZip. The extracted file will be in the .img format.
  5. Insert the microSD card into the card reader and plug the card reader into the computer. Many computers nowadays have an inbuilt SD card reader. In this case, you will need to insert the microSD card into the microSD to SD card converter and insert it into the computer's inbuilt SD card reader. MicroSD to SD card converters usually come bundled with microSD cards in the same package. If that's not the case, then you will have to procure it separately.
  6. Run Win32DiskImager.exe and write the image onto the SD card:
    Preparing MicroSD card manually

    You might receive the following message if the card reader's write protection is on:

    Preparing MicroSD card manually
  7. Toggle the write protection notch and try again. You will see the following message:
    Preparing MicroSD card manually
  8. Click on Yes and it will start writing the image file to the microSD card:
    Preparing MicroSD card manually
  9. Once the image is successfully written, it will display the following message:
    Preparing MicroSD card manually

Note

If you are using Linux, then you can find the instructions at https://www.raspberrypi.org/documentation/installation/installing-images/linux.md.

If you are using Mac OS, then you can find the instructions at https://www.raspberrypi.org/documentation/installation/installing-images/mac.md.

If you have an HDMI monitor, then skip this step. This additional step is required only if you are planning to use a VGA monitor in place of an HDMI monitor.

Browse the microSD card on the computer. Locate and open config.txt. We have to edit the file in order to enable proper display on the VGA monitor.

By default, the commented options (which have # at the beginning) are disabled. We are enabling this option by uncommenting this line, that is, by removing # from the beginning of the commented line. This is what you need to do:

  1. Change #disable_overscan=1 to disable_overscan=1.
  2. Change #hdmi_force_hotplug=1 to hdmi_force_hotplug=1.
  3. Change #hdmi_group=1 to hdmi_group=2.
  4. Change #hdmi_mode=1 to hdmi_mode=16.
  5. Change #hdmi_drive=2 to hdmi_drive=2.
  6. Change #config_hdmi_boost=4 to config_hdmi_boost=4.
  7. Save the file.

Booting up our Pi for the first time

Let's boot up our Pi for the first time with the microSD card:

  1. Insert the microSD card into the microSD card slot of the Pi.
  2. Connect the Pi to the HDMI monitor. If you are connecting the VGA monitor, connect it using the HDMI to VGA converter.
  3. Connect the USB mouse and the USB keyboard.
  4. Connect the Pi to a power supply using the micro USB power cable. Make sure the power is switched off at this point.
  5. Check all the connections once and then switch on the power supply of the Pi.

At this stage, our Pi will start booting up. You will see a green light on the Pi board blinking. This means that it's working! Now, there are few more things we need to do before we can really start using our Pi. Once it boots up, it will show the desktop as follows:

Booting up our Pi for the first time

Once the desktop is visible, go to Menu | Accessories | lxterminal. Then, type sudo raspi-config. A text-based menu, such as the following, will appear:

Booting up our Pi for the first time

Perform the following steps. We need to use arrow keys and the Enter key to select options in the text-based menu. Press Enter to select a menu item. Also, we can use the Tab key to directly go to the Select and Finish buttons:

  1. Select Expand Filesystem.
  2. In Boot Options, select B4 Desktop Autologin, as shown in the following screenshot:
    Booting up our Pi for the first time

    Note

    The default username is pi and the password is raspberry. We need it when we don't choose any of the preceding autologin options. We can change this password from the second option in the raspi-config menu.

    We can also choose to boot to the console by selecting any of the first two options in the preceding menu. The default shell of Raspbian is Bash. We can confirm it by typing the following command:

    echo $SHELL
    

    We can always go to the graphical desktop from the Command Prompt by typing the startx command in the console.

  3. Go to Internationalisation Options | Change Timezone.
  4. Go to Internationalisation Options | Change Keyboard Layout | Change it to US (the default is UK).
    Booting up our Pi for the first time
  5. Select Enable Camera.
  6. Select Advanced Options.
  7. Under this option, select Memory Split and enter 64MB for GPU.

This option decides how much RAM is used by the GPU (Graphics Processor Unit). The more RAM is allocated to the GPU, the more intensive graphics processing can be done. 64 MB is a good value for most graphics purposes.

Once all these options are modified, select Finish. This will prompt for a reboot of the Pi. Choose Yes and let it reboot. Once rebooted, it will automatically take us to the Raspbian Desktop again.

You can always invoke the raspi-config tool from Command Prompt with the following command and change the settings:

sudo raspi-config

Shutting down and rebooting Pi safely

In the Raspbian menu, there are options to shut down and reboot the Pi.

If we click on the following Menu button on the desktop, it will display multiple options:

Shutting down and rebooting Pi safely

The following image shows the last option:

Shutting down and rebooting Pi safely

If we click on the preceding option, the following window will appear:

Shutting down and rebooting Pi safely

Also, from Command Prompt LXTerminal, we can shut down Pi safely by issuing the following command:

sudo shutdown –h now

An alternative command for this is as follows:

sudo halt

You can reboot Pi with the following command:

sudo reboot

Updating the Pi

Now we have a working Pi running on the Raspbian OS. Let's update our Pi. Make sure you have a working wired or wireless Internet connection with reasonable speed for this activity:

  1. Connect your Pi to an Internet modem or router with an Ethernet cable or plug in the Wi-Fi dongle to one of the USB ports.
  2. Run the following command to restart the networking service:
    sudo service networking restart
    
  3. Make sure that your Raspberry Pi is connected to the Internet by typing the following command:
    ping –c4 www.google.com
    
  4. apt (Advanced Package Tool) is the utility used to install and remove software in Debian and its variants. We need to use it to update our Pi software.
  5. Run the following commands in a sequence:
    • sudo apt-get update: This command synchronizes the package list from the source. Indexes of all the packages are refreshed. This command must be issued before we issue the upgrade command.
    • sudo apt-get upgrade: This command will install the newest versions of all the already installed software. Any obsolete packages/utilities are not removed automatically. If any software is in its newest version, then it's left as it is.
    • sudo rpi-update: This command is used to upgrade the firmware. The kernel and firmware are installed as a Debian package, and so they will also get updates. These packages are updated infrequently after extensive testing.
    • sudo reboot: This will reboot the computer.
You have been reading a chapter from
Raspberry Pi By Example
Published in: Apr 2016
Publisher:
ISBN-13: 9781785285066
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 €18.99/month. Cancel anytime