Book Image

Raspberry Pi By Example

By : Arush Kakkar
Book Image

Raspberry Pi By Example

By: Arush Kakkar

Overview of this book

Want to put your Raspberry Pi through its paces right out of the box? This tutorial guide is designed to get you learning all the tricks of the Raspberry Pi through building complete, hands-on hardware projects. Speed through the basics and then dive right in to development! Discover that you can do almost anything with your Raspberry Pi with a taste of almost everything. Get started with Pi Gaming as you learn how to set up Minecraft, and then program your own game with the help of Pygame. Turn the Pi into your own home security system with complete guidance on setting up a webcam spy camera and OpenCV computer vision for image recognition capabilities. Get to grips with GPIO programming to make a Pi-based glowing LED system, build a complete functioning motion tracker, and more. Finally, get ready to tackle projects that push your Pi to its limits. Construct a complete Internet of Things home automation system with the Raspberry Pi to control your house via Twitter; turn your Pi into a super-computer through linking multiple boards into a cluster and then add in advanced network capabilities for super speedy processing!
Table of Contents (22 chapters)
Raspberry Pi By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
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:

    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.

    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:

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

  7. Toggle the write protection notch and try again. You will see the following message:

  8. Click on Yes and it will start writing the image file to the microSD card:

  9. Once the image is successfully written, it will display the following message:

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:

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:

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:

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

  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:

The following image shows the last option:

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

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.