Raspberry Pi Hardware

First of all, we need a Raspberry Pi. The newest model is Raspberry Pi Model 3B, which is what I am using. Here is the specification:

  • A 1.2 GHz 64-bit Quad-core ARMv8 CPU
  • 1GB Memory
  • 100Mbps Ethernet Port
  • 802.11n Wifi
  • Bluetooth 4.1 BLE
  • 4 USB ports
  • Micro SD card slot
  • HDMI output
  • 3.5mm audio output
  • Micro USB power input
  • 40-pin GPIO
  • CPI (Camera) input

Note that there is no hard drive in Raspberry Pi, everything is stored in the Micro SD card. A 8GB Class 4 Micro SD card is sufficient. However for the best performance, a 16GB class 10 Micro SD card is recommended.

Also, a power supply is required. The official power supply is 5V 2.5A Micro USB. Most of the Android mobile chargers are 5V 2A and sufficient.

Last but not least, there are a few options for initial connect and setup. A keyboard (wired or wireless, but not bluetooth) and a monitor (and a HDMI cable) can be used, or simply an ethernet cable connect to the router.

You may also want a case to protect you Raspberry Pi. You can buy one online, or print one with 3D printer. Nevertheless, you can always make one with card board.

Also, a low end Raspberry Pi Zero is available. I recently purchased a Zero W, W for wireless. It has a weaker processor and a lot less connectors, and much smaller in size and cheaper.

Here is what Pi Zero has:

  • A 1 GHz 32-bit ARMv6Z CPU
  • 512MB Memory
  • Micro SD card slot
  • Mini-HDMI output
  • Micro USB power input
  • On-the-go Micro USB (require adaptor)
  • 40-pin GPIO
  • Mini-CPI (Camera) input

In addition, Pi Zero W has a chip for wifi and bluetooth:

  • 802.11n Wifi
  • Bluetooth 4.1 BLE

Any one of these is all we need for hardware. Let’s start our system.

Install the System

There are multiple systems available for Raspberry Pi. Most of them are based on Linux. The official system is Raspbian, a Debian based system designed for Raspberry Pi. The latest disk image is available at https://www.raspberrypi.org/downloads/raspbian.

After download the disk image, it needs to be written into the SD card. Note, the SD card will be formatted and all data will be removed. Please back up your data before continue.

For Windows, Win32 Disk Imager can be downloaded at https://sourceforge.net/projects/win32diskimager.

Insert your SD card, run Win32 Disk Imager as administrator, select the disk image as the source file and your SD card as destination. Click on write, then wait for the writing to complete.

For MacOS, no additional software is required.

First, insert your SD card, open Disk utility. Choose your SD card from the left. The table on the right indicates the device number of “disk1” (or any other number). Record that number. Then unmount the disk (on the top row).

Second, open Terminal. Type in the following command:

  • sudo dd if=<path of raspbian.img> of=/dev/disk<number>

Where the input raspbian.img is the location of your disk image (usually at ~/Downloads/<date>raspbian.img) and the output disk number is the one recorded on the previous step. Hit Enter, and type in your administrator password, then wait for the writing to complete.

Hint: You can type only the first few characters of the image file name, after its directory, and hit on tab to auto select. When you enter your password, unlike when you login to the system, nothing will be shown for security propose. During writing process, you can use control+T to see its progress.

The writing process takes about 30-40 minutes for the full version of system (about 4GB). Eject the SD card and it’s all done!

The First Sight of Raspbian

After the system is installed, insert the SD card to the Raspberry Pi, connect other devices such as mouse, keyboard, monitor and Ethernet cable, we’re ready to boot by plug in the power supply.

There is no on/off button on Raspberry Pi, unlike traditional PC. The Raspberry Pi will automatically boot once it’s connected to power.

After a long booting screen, the command line is ready for us. Raspberry Pi has graphical user interface (Desktop), but it’s not turned on by default.This tutorial is focus on command line, however we can enable desktop by type in “startx”, or we can use raspi-config to allow desktop starts automatically.

If you don’t have a monitor and would like to remain headless, you can access Raspberry Pi by SSH. This is discussed in the later post.

Raspi-config is a basic config tool on Raspbian, and it can be accessed by command:

  • sudo raspi-config

A new screen will pop out. Move the cursor by arrow and select by enter. There are a few things we must configured for first time use.

First, we need to expand file system. Simply hit on Option 1 and wait for the scrolling. This will allow all space on the SD card is allocated to the system.

Second, change the password on Option 2. Again as you type in your password nothing will be shown, but it will be recorded.

Third, go to Option 4, and change all settings under it to your own locale and timezone, except for keyboard layout.

If you’re using Raspberry pi 1 or 2, also turn overclock on to 1GHz, that’s Option 6 – Select Turbo. If you’re on Pi 3 or Pi Zero, overclock is not available as it’s already fast enough.

After these steps, hit on tab and choose finish, then reboot.

We’ve done on basic setup, and moving forward to softwares.

Basic Commands of Debian (Linux)

As we’re working on command line, there are a lot of basic and useful commands that we need to know about. Please note that all commands are case sensitive.

sudo <command>

sudo means execute the following command as root user, which is equivalent to run as administrator in Windows. Root user is the user with the top privilege in linux. There are many commands that can only be executed by root user, the editing of many files also requires root privilege.

If you execute “sudo su”, you’re switching to root user and all commands are executed as root. You can exit root user by command “exit”.

halt

halt is the same as shutdown. This command requires sudo. To boot Raspberry Pi again, unplug the power and plug it back in.

reboot

reboot is the same as restart. This command also requires sudo.

man <command>

man is short for manual. It can show all usages and options and the explanation of a specific command.

Also, “-h” or “–help” option of a command would normally output a short summary of the manual.

Files and Directories

ls <path>

ls is short for list files, under current directory by default if no path is given. In Linux, file names are case sensitive. Also a directory is considered as a file, most files don’t have a extension.

Linux treats all files and directories have the name starts with . (a single dot) as hidden files/directories. If you would like to see the hidden files, use “ls -a”.

cd <path>

cd is short for change directory. We’re moving from one directory to another. Path can be type in absolute path (full path) or relative path (under current directory). If the path starts with /, it’s considered to be an absolute path. Otherwise it’s a relative path.

For long directory name, you can type in the first few characters, and hit tab, the system would automatically fill out the rest for you. If there are multiple matches, all matches will be listed and you can type in the one you want. This also works on flies name and some commands.

There are a few more expressions about path and directory in linux:

  • (a single dot) represents current directory.
  • .. (two dots) represents the parent directory of the current directory.
  • / (forward slash) represents the top directory. It doesn’t have a parent directory.
  • ~ (tilde) represents the home directory of the current user.

pwd

pwd is short for print working directory. It will output the absolute path of your current location.

mkdir <path>

mkdir is short for make directory. The path can be either absolute path or relative path. However you cannot make a directory that has the same name with another file or directory, under the same parent directory.

rmdir <path>

rmdir is short for remove directory. Again the path can be absolute of relative. Unlike other systems, you can only do rmdir on empty directory. If there are other files in the directory and you would like to delete them all, use “rm -r”.

cp <source files> <destination path>

cp means copy. Copy the source files to the destination. If you would like to copy a directory, use “cp -r”.

Multiple source files are accepted, and the last input is always the destination. * (wildcard) can also be used to select all matched files.

mv <source files> <destination path>

mv means move. Move the source files to the destination. If you would like to move a directory, use “mv -r”.

rm <files>

rm means remove files, or delete files. To delete a directory, use “rm -r”. This works on a empty directory and  a directory contains files. Note that there’s no trash on command line and all delete are permanent and cannot undo.

chown <owner:group> <files>

chown means change owner. Each file has an owner and a file group, as well as access permission control. Change the owner of a file would allow the new owner takes control of the file. root user can usually override the access control settings and change any files.

If you change the owner for a directory, it would only apply to the directory itself. If you would also like to change the owner for all files under it, use “chown -r”.

chmod <permission> <files>

chmod means change access permission. Access permission is a three digit number and each digit is range 0-7. The first digit is the owner’s permission. Second digit is the file group users’ permission. And the last digit represents all other users’ permission. There are three permissions, read permission is notated by number 4, write permission is number 2 and execute permission is number 1. Where multiple permission is gained, the corresponded numbers add up.

If you change access permission for a directory, it would only apply to the directory itself. If you would also like to change the access permission to all files under it, use “chmod -r”.

-r/-R option

-r/-R means recursive.  Recursion means to execute the command not only to the directory itself, but also to all files under it. Some commands require capital and some others use lowercase.

Read and Edit Files

cat <files>

cat is to output the content of a file, or read the file.

head <files>

head is similar to cat. However it outputs only the first few lines of the file.

tail <files>

tail is also similar to cat, but it outputs the bottom few lines only.

grep <pattern> <files>

grep is to search for a specific string (pattern) in the input file. Only the lines that contain given string will be outputted.

nano <files>

nano is a basic and simple editor for binary files. It can either edit an existing file or create a new file if the input file doesn’t exist. The ^ symbol for keyboard command is control key.

There are also other editors pre-installed, such as vim. But nano is always the easiest to use.

 

These are the basic commands we’re interested in at the moment. As we explore, we will be using more command to speed up our work. We can now start to work on softwares and packages.

 

Softwares on Raspberry Pi

Softwares are often referred as packages in Linux. There are a few ways to install new packages. Some packages can be downloaded on websites, but most are done by apt-get. Many popular packages are listed on the source lists and can be downloaded directly over command line. apt-get can also update existing packages and deal with dependency.

Right after install the system, it’s always good to check any updates available. This is also recommended before you install any packages for the first time of the day. The update command for all apt-get packages is:

  • sudo apt-get update
  • sudo apt-get upgrade

Note that these two commands are not the same. Update is to update the source list only (check for newer version) and upgrade is the actual installation of newer version of packages. Both lines need to be executed and in order, i.e. first update then upgrade.

However, upgrade will not deal with dependency, so sometimes it would say a few packages are not upgraded. This means there are new packages require to install before upgrade existing ones. If that’s the case and you really want to upgrade, use dist-upgrade:

  • sudo apt-get dist-upgrade

To install new packages, execute the following command:

  • sudo apt-get install <package name>

And to uninstall packages, execute the following command:

  • sudo apt-get remove <package name>

Make sure the package name is as exact or you will receive an error. If you’re not sure, type in the first few characters and hit tab to see all matches.

Apart from apt-get, curl and wget are sometimes used to install as well. They are more like a command line downloader. For the use of curl and wget, it’s better to copy the command from the developer’s website to avoid errors.