Monday, June 5, 2017

Raspberry Pi

An Introduction

I have several projects that I am currently working on involving Raspberry Pis, and every time I start to write up a blog post I realize that many people might be lost from the beginning without knowing what a Pi is, and how to do the initial set-up for it.  In addition I have some links to information that I need to store for future reference, and this seems like as good of a place as any.  So, let's get started.

What is a Raspberry Pi?


I once told a friend that was stopping by that I was working on a Raspberry Pi, and he was disappointed when he arrived and wasn't offered a slice for dessert.  The Raspberry Pi is a great little single board computer that can be purchased for around $35, depending on the model.  It can do almost anything a modern desktop computer can do, albeit a little slower.  Not only is it inexpensive, but its small size and low power consumption make it ideal for a variety of projects.  It's biggest drawback is that it uses an ARM based processor instead of an X86/X64 processor, so it is limited in that it can only run operating systems that have been developed for or ported over to work on ARM based processors.  That being said, there are many flavors of Linux that have been made to run on the Pi, such as Ubuntu, Kali, and Rasbian (which was written specifically for the Pi), as well as other specialty Linux operating systems such as Kodi (media center), OpenWRT (routing), and Retropie (old video game emulator).  Even Microsoft has made an Internet-of-Things version of Windows 10 that runs on the Pi.  I could make an entire post about the Raspberry Pi and it's capabilities, but so many others have already done this that I will suggest that anyone interested in learning more about the Raspberry Pi let Google be their guide down that rabbit hole.  I have put just a few links below to get you started.

Specs: http://elinux.org/RPi_Hardware
Lots of info from the people that make them: https://www.raspberrypi.org/
More Information: http://elinux.org/RPi_Hub

I have a Raspberry Pi, now what?

Once you have a Raspberry Pi you will need to provide it with an operating system.  The Pi does not come with a built-in hard drive, but instead uses a micro SD card (sold separately) as it's hard drive.  Eight GB should be plenty of space, but I typically use the 16 GB cards since there is not much difference in price.  It is also advised to stick to reputable brands and get a class 10 card for faster write speeds.  The nice thing about using an SD card as a hard drive is that if you want to experiment with different operating systems, switching from one to the next is as easy as swapping out the card.

Now you will need to decide which operating system to use, which all comes down to what you want to do with your Pi.  If you are new to this, and just want to see what the Pi is capable of, I suggest starting with Raspbian, which is a Debian based Linux distribution designed specifically for the Pi.  You can use Raspbian with or without a GUI (called PIXEL), but for new users I would definitely suggest using PIXEL.  If you are a more advanced user, or if you have a specific application in mind, you may want to try a more specialized operating system.  For example, maybe you want to build a portable penetration testing rig, in which case you would want to use Kali Linux.  If you want to build a media center computer then I would recommend LibreELEC or OSMC which are both based on the Kodi Media Player (formally XMBC).  Below I have listed the operating systems that I have tried, and a link to where you can download them.  More comprehensive lists can be found here: https://fabiololix.blogspot.it/2016/05/raspberry-pi-distribution-list.html

Raspbian (Linux Desktop Specialized for RPi):  https://www.raspberrypi.org/downloads/raspbian/
Ubuntu MATE (Ubuntu Linux ported to work with RPi):  https://ubuntu-mate.org/download/
Kali (Linux Desktop specialized for Penetration Testing):  https://www.offensive-security.com/kali-linux-arm-images/
LibreELEC (Media Center):  https://libreelec.tv/downloads/
OSMC (Media Center):  https://osmc.tv/download/
Retropie (Classic Video Game Emulator):  https://retropie.org.uk/download/

For anyone asking "What about NOOBS?", I'm not a big fan, so I intentionally left it out.  If you are interested you can find more info here:  https://www.raspberrypi.org/downloads/noobs/


Installing the Operating System

Once you have decided on an operating system and downloaded the disk image you will need to verify the image and write it to your micro SD card.  This will have to be done from another computer.  If you are using a Linux machine then it can all be done from the command line.  You can find information on that here:  http://elinux.org/RPi_Easy_SD_Card_Setup#Using_the_Linux_command_line
If you are using a Mac then I know nothing about the world you live in, but you can find info on how to do it here:  http://elinux.org/RPi_Easy_SD_Card_Setup#Flashing_the_SD_card_using_Mac_OS_X
The only computer that I have with an SD card slot is a Windows machine, so I will give brief instructions here on how to set up the SD card using Windows.

When a file is downloaded from the internet, typically an SHA1 or MD5 checksum is provided.  The purpose of this is to verify that the file was not tampered with prior to downloading and that it was not corrupted during the download.  I have had success using the following two tools to verify SHA1 and MD5 checksums in Windows.  FCIV (Microsoft File Checksum Integrity Verifier) is a command line utility that is easy to use if you are comfortable with using a Windows terminal.  It can be downloaded from here:  https://www.microsoft.com/en-us/download/details.aspx?id=11533.  Once installed, read the readme.txt document that comes with it for further instructions.  If you would rather use a graphical tool then I recommend Igorware Hasher.  It  is a free utility and can be downloaded here: http://www.igorware.com/hasher

Once you have computed the checksum and compared it to the checksum provided on the download page to verify that your disk image is not corrupt, you are ready to write the image to the SD card.  I use Win32 Disk Imager, which can be downloaded here:  https://sourceforge.net/projects/win32diskimager/
It is pretty simple to use, just select the operating system disk image that you have downloaded for your Pi, select your SD card as the destination, and click "Write".  Just be 100% sure that the drive letter that you select is indeed your SD card, or you could end up overwriting a partition on your computer.

And that's it.  With your newly minted SD card containing the operating system for your Raspberry Pi you are ready to boot it up.  Don't forget, you will need to have a keyboard, mouse, and display in order for you to do the initial configuration on the Raspberry Pi, even if you plan on setting it up for headless operation or remote connectivity.  Below I'll go through some basic hardware options for the Pi, the initial configuration for a Pi running Raspbian, and some basic information regarding the GPIO pins.

Raspberry Pi Hardware Options

I won't spend much time on the Hardware options, just because there are so many of them, and new hardware is being developed all the time.  As I mentioned earlier, you will need a USB keyboard and mouse, as well as a monitor and HDMI cable to do the initial set-up on the Pi.  Depending on the function of the Pi, you can opt to run it headless after that, and just connect to it remotely via SSH or VNC.  If you are building a media center computer then your TV will be your monitor, and I would highly recommend a wireless keyboard/mouse combo with a USB dongle.  You will also need a 5V micro-USB power supply that can provide a minimum of 2 amps.  Most non-Apple phone chargers will work for this purpose, just check the rating on them and make sure that they provide at least 2 amps of current.  If you don't have one handy they can be purchased fairly inexpensively.


The only other piece of hardware that I would consider necessary is a case for your Pi.  Because the circuitry is exposed on all sides, you really want to have something to protect it from coming into contact with anything conductive while it is running.  I've tried many different cases, and I've even made my own.  From my experience the case made by CanaKit is the best, but there are plenty of other good ones out there.  If you are looking to buy a Pi, you can get a kit from CanaKit with the Pi, case, and power supply included, as well as a couple of heat-sinks for your processors.  This is the current link on Amazon for the kit:  https://www.amazon.com/dp/B01C6EQNNK/ref=wl_mb_recs_5_title



As far as other hardware, they sky is the limit.  The Pi has a dedicated camera socket which can be used to connect a camera for high definition pictures and video.  There are also plenty of small touchscreen monitors marketed for the Pi.  If you want to play with the GPIO pins there are breakout boards and cables made specifically for the Pi.  For additional functionality, a wide variety of "HATs" have been developed for the Pi.  These HATs typically connect to the two rows of GPIO pins, making them sit directly on top of the Pi, like a hat (hence the name.)  These can be used for a wide range of things, from driving motors and servos to reading analog sensors, and even adding a GPS module.  If you are interested, take a look at Adafruit's selection of Pi Hats at the following link:  https://www.adafruit.com/category/286
If that's not enough, just Google Raspberry Pi HATs or Pi accessories.  There are new accessories being developed for the Raspberry Pi every day.


Raspbian Initial Configuration

OK, I got off on a little bit of a tangent there regarding the hardware options.  Lets get back to actually using the Pi.  So you've got your Pi, your case, your power supply, and a micro SD card with an operating system installed on it.  What next?  Hook-up your monitor, mouse, keyboard, and Ethernet cable for internet (if you have to use Wifi, that's not a problem, but I won't be covering that here.)  Go ahead and insert the SD card and lets power this baby up.  For the purpose of brevity (if it's not too late for that), I'll only go over the initial set-up for Raspbian.  Other operating systems will likely be similar, and will probably have their own documentation regardless.

If you have installed Raspbian-lite you will be booted into a terminal.  If you have installed Raspbian with Pixel (the graphical desktop interface) you will boot into a desktop environment, similar to Windows.  Either way, you will have to log in. The default user is "pi", and the default password is "raspberry".  Once you are logged in we will need a terminal window, so if you are in the desktop environment click on the black square icon for the terminal in the upper left hand corner of the screen.  Once you are in the terminal, type the following command "sudo raspi-config".  This will bring you to the Configuration Tool.  Typically I'm an advocate of doing everything directly from the command line, just to stay familiar with the Linux terminal, but to keep things easy we'll use the built-in tool.  Below is the main menu for the configuration tool.


As Raspbian gets updated, this menu changes from time to time, so if your menu doesn't look exactly like this don't worry, you should still be able to find the options you need.

The first thing we need to do is to change the default password for the user "pi" (option 1).  This is done for security reasons, so that anyone with knowledge of the default login can't gain access to your system.  Personally I like to go in and create a new user with root permissions, and then delete the user pi altogether, but this is a bit more complicated and not completely necessary, so I won't be covering that here.

Option 2 allows us to change the hostname, which is how the Pi will identify itself on the network.  This step is optional, but I like to make a habit of giving all machines in my network unique hostnames.

The next thing we need to do is set the localization options.  By default the Pi assumes you are in Great Britain, because that is were the Pi originates.  If you are from the other side of the pond in the States, like I am, you will need to notify the Pi.  Select option 4, and proceed through all the sub-menus in order.  For the first menu item, "Change Locale", you will be looking for something like "en_US.UTF-8 UTF-8".  Once you have highlighted this option, use the space-bar to select it, then hit return.  The rest of the menu options, Change Timezone and Change Keyboard Layout, should be fairly self-explanatory.

Option 5, "Interfacing Options," has several sub-menu options, but the only ones I will address here are "Enable SSH" and briefly "Enable VNC"  Both of these options will open the Pi for remote connectivity.  SSH is a protocol that will allow you text-based terminal access from another network computer.  If you are turning the Pi into a server, or you just don't want to leave the keyboard and monitor connected, you will want to enable SSH access.  If you are using a Linux machine to connect to the Pi, you can do it from the terminal command line using the following command: ssh -l <username> <ip.address>  If you are using a Windows machine you will need to use a terminal program like Hyperterminal or Putty.  Putty is the most commonly used terminal program and can be downloaded for free from here: www.putty.org  If you want remote access and are not comfortable having only a text-based terminal, you can turn on VNC server here as well.  VNC will give you remote access to the desktop environment by using a VNC client like RealVNC's VNC Viewer (downloadable from: https://www.realvnc.com/download/viewer/).  Another good option is TightVNC found here: http://www.tightvnc.com/download.php  Unfortunately I have found VNC to be a little slow and laggy on the Pi, so I recommend leaving the keyboard and monitor hooked up if you want to use the desktop environment, and only use SSH for remote connections.

The last thing we need to do is to "Expand the Filesystem" to allow Raspbian to use the entire SD card.  This option is in the sub-menu under "Advanced Options".  Once selected the filesystem will automatically be expanded, and then you should be prompted to reboot.  Once you have completed this last step and rebooted you should be finished with the initial configuration, and be ready to explore the world of Raspberry Pi.

Additional Configuration

I started to include a few other things here, like terminal-based network configuration, changing the root user, and mounting USB drives from the terminal, but I decided this post was already long enough, and the information would not be necessary for all Pi users.  Instead I decided to create a separate post for additional Linux configuration options.  It will be coming soon, and I know you are all very excited.

Raspberry Pi 2B/3B GPIO Pins

The last thing I will touch on in this post is the GPIO pins available on the Raspberry Pi (General Purpose Input/Output).  On the Pi models 2B and 3B there are 40 GPIO pins accessible via the two rows of male header pins.  These pins can be used to connect the Pi to external sensors, or use the Pi to control other electronics.  Really your imagination is the limit.  Just keep in mind that the Pi's GPIO pins use 3.3V logic, so if you are communicating with something using 5, 9, or 12V, you will need to use a level converter of some kind in order to not damage the Pi.



The image above is the layout for the pins on the Raspberry Pi models 2B and 3B.  It was borrowed from the following website, where you can also find the pinouts for other models of the Raspberry Pi:  http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/

For additional information regarding writing Python scripts utilizing the GPIO Pins see the following links:


So that is where I am going to stop for now.  In the future if I run across any useful links regarding general information on the Raspberry Pi I will post them below, just for reference.  If anyone can think of any other basic information that I have omitted please let me know and I will update this post.  

1 comment:

  1. That's a lot of good information. I doubt a lot of people even know this is possible, thinking they have to buy a big expensive computer. Good resources linked too.

    ReplyDelete