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:
- Download the Win32DiskImager installer, which is available at http://sourceforge.net/projects/win32diskimager/files/latest/download and then install it.
- Download the installable version of WinZip, which is available at http://www.winzip.com/prod_down.html, and install it.
- 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.
- Extract the ZIP file using WinZip. The extracted file will be in the
.img
format. - 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.
- 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:
- Toggle the write protection notch and try again. You will see the following message:
- Click on Yes and it will start writing the image file to the microSD card:
- 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:
- Change
#disable_overscan=1
todisable_overscan=1
. - Change
#hdmi_force_hotplug=1
tohdmi_force_hotplug=1
. - Change
#hdmi_group=1
tohdmi_group=2
. - Change
#hdmi_mode=1
tohdmi_mode=16
. - Change
#hdmi_drive=2
tohdmi_drive=2
. - Change
#config_hdmi_boost=4
toconfig_hdmi_boost=4
. - 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:
- Insert the microSD card into the microSD card slot of the Pi.
- Connect the Pi to the HDMI monitor. If you are connecting the VGA monitor, connect it using the HDMI to VGA converter.
- Connect the USB mouse and the USB keyboard.
- Connect the Pi to a power supply using the micro USB power cable. Make sure the power is switched off at this point.
- 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:
- Select Expand Filesystem.
- In Boot Options, select B4 Desktop Autologin, as shown in the following screenshot:
Note
The default username is
pi
and the password israspberry
. We need it when we don't choose any of the preceding autologin options. We can change this password from the second option in theraspi-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. - Go to Internationalisation Options | Change Timezone.
- Go to Internationalisation Options | Change Keyboard Layout | Change it to US (the default is UK).
- Select Enable Camera.
- Select Advanced Options.
- 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:
- 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.
- Run the following command to restart the networking service:
sudo service networking restart
- Make sure that your Raspberry Pi is connected to the Internet by typing the following command:
ping –c4 www.google.com
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.- 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 theupgrade
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.