Raspberry Pi, with its low cost and amazing functionality package, has taken the robotic hobbyist community by storm. Unfortunately, many of them, especially those who are new to embedded systems and programming, can end up so discouraged that the board can end up on the shelf gathering dust next to floppy disks and chia pets.
There is nothing as exciting as ordering and finally receiving a new piece of hardware. Yet things can go south quickly, even in the first few minutes. This chapter will hopefully help you avoid the pitfalls that normally accompany unpacking and configuring your Raspberry Pi. We'll step through the process, answer many of the different questions you might have, and help you understand what is going on. If you don't get through this chapter, then you'll not be successful with any of the others and your hardware will go unused, which would be a real tragedy. So let's get started!
One of the most challenging aspects of writing this guide was to decide the level at which I should describe each step. Some of you are beginners, others have some limited experience, and the rest will know significantly more in certain areas. I'll try to be brief but thorough, trying to detail the steps in order to be successful. So for this chapter, here are our objectives:
Unbox and connect the board to power
Connect a display, keyboard, and mouse
Load and configure the operating system
Access the board remotely
Note that Raspberry Pi comes in two flavors: A and B. Flavor B comes with additional input/output capability and will be the flavor we'll focus on in this book. That does not mean that many, if not most, of the projects here require the extra capability of B. Rather, for the relative cost difference, flavor B provides enough benefits for me to assume that you have chosen this version. It will also make it simpler to explain how to use the board. Note that, the initial version of the Raspberry Pi model B had 256 MB of memory, and as of October 2012, the standard Raspberry Pi model B comes with 512 MB of memory.
Here are the items you'll need for this chapter's projects:
A Raspberry Pi model B
A USB cable to provide power to the board
A display with proper video input
A keyboard, mouse, and a powered USB hub
An SD card with minimum 4 GB memory
An SD card writer
Another computer that is connected to the Internet
An Internet connection for the board
A LAN cable
Before plugging in anything, inspect the board for any issues that may have occurred during shipping. This is normally not a problem, but it is always good to do a quick visual inspection. You should also acquaint yourself with the different connections on the board; they are labelled for your information in the following image:
Connect the micro USB connector end of the cable to the board.
Connect the standard-sized USB connector either to a PC or to a compatible DC power source that has a USB connection.
If you are going to use a DC power source at the standard USB connector end, make sure that the unit can supply enough current. You'll need a power supply that can provide at least 1000 mA at 5 volts.
When you plug the board in, the PWR LED should be red. The following image is a close-up of the LED locations, so you're certain of which one to look for:
The OK LED will flash green when you install a card and boot up an operating system. The other three indicators are indications of a valid LAN connection. They will show/flash green or yellow, based on the availability of a valid LAN connection.
Now that your board works, you're going to add peripherals so that it can operate as a standalone computer system. This step is optional, as in future, your projects will often be in systems where you won't connect directly to the board with a keyboard, mouse, and display. However, this can be a great learning step, and is especially useful if you need to do some debugging on the system.
A USB mouse
A USB keyboard (this can be wireless and can contain a built-in mouse pad)
A display that accepts HDMI, DVI Video, or SVideo inputs
A powered USB hub (this is optional for this instance, but you most certainly will need it for future projects)
You may have most of this stuff already, but if you don't, there are some things to consider before buying additional equipment. Let's start with the keyboard and mouse. Most mice and keyboards have separate USB connectors. You'll notice, however, that your Raspberry Pi has only two USB ports. If you want to connect other devices, you may want to choose a keyboard that has a built-in mouse pad. That way, you only have one USB connection for both the devices.
You may also want to consider purchasing a powered USB hub. Before deciding on the hub to connect to your board, we need to understand the difference between a powered USB hub and one that gets its power from the USB port itself. Almost all USB hubs are not powered; that is, we don't plug in the USB hub separately. The reason for this is that almost all of these hubs are hooked up to computers with very large power supplies, and powering USB devices from the computer is not a problem. This is not the case for our board. The USB port on our board has very limited power capabilities, so if we are going to hook up devices that require significant power (for instance, a WLAN adapter or a webcam), we're going to need a powered USB hub; one that provides power to the devices through a separate power source. The following is an image of such a device:
Notice that there are two connections on this hub. The one to the far right is a power connection, and it will be plugged into a battery with a USB port. The connection to the left is the USB connection, which will be plugged into Raspberry Pi.
Now, you'll also need a display. Fortunately, your Raspberry Pi offers lots of choices here. There are a number of different video standards; the following image is a reference to some of the most prevalent ones:
There is an SVideo output and an HDMI connector on Raspberry Pi. The easiest method to create a connection is to connect the board directly to a monitor or TV that has an SVideo or HDMI input; however, you'll need to buy a cable to go between the two. Check the video input on the monitor or TV; it will normally either have a set of RCA jack inputs, normally colored yellow (video), white (left-audio), and red (right-audio), or a multipin SVideo connector. If you are connecting using the RCA jack inputs, connect the output of the Raspberry Pi video to the yellow input. The output of Raspberry Pi is an RCA jack output, so make sure you get a cable, with or without adapters, so that you can make the proper connection.
If you want to use the HDMI output, simply connect your cable with regular HDMI connections to Raspberry Pi and your TV or monitor that has an HDMI input connector. HDMI monitors are relatively new, but if you have a monitor that has a DVI input, you can buy adapters relatively inexpensively that provide an interface between DVI and HDMI. The display I use has a DVI input.
Don't be fooled by adapters that claim that they go from HDMI or DVI to VGA, or HDMI or DVI to -video. These are two different kinds of signals: HDMI and DVI are digital standards, and VGA and SVideo are analog standards. There are adapters that can do this, but they must contain circuitry and require power, so are significantly more expensive than any simple adapter. Now that you have all the bits and bobs, connect the USB hub to the standard USB port, the keyboard and mouse to the standard USB port, and the display to the proper connector as shown in the following image:
Once all these are connected, you are ready to plug in Raspberry Pi. I am using a standard USB 5 volt power supply. Make sure you connect all your devices before you switch on the unit. Most operating systems support the hot swap of devices, which means you are able to connect a device after the system has been powered; but this is a bit shaky in the embedded environment. You should always cycle power when you connect new hardware.
Unfortunately, even though your hardware configuration is complete, you'll need to complete the next section to switch on the device. So let's figure out how to install an operating system.
Now that your hardware is ready, you need to install an operating system. You are going to install Linux, an open source version of Unix, on your Raspberry Pi. Now Linux, unlike Windows, Android, and IOS, is not tightly controlled by a single company. It is a group effort, mostly open source, and while it is available for free, it grows and develops a bit more chaotically.
Thus, a number of distributions have emerged, each built on a similar kernel or core set of capabilities. These core capabilities are all based on the Linux specification. However, they are packaged slightly differently and developed, supported, and packaged by different organizations; Debian, Arch, and Fedora are names of some of the versions. There are others as well, but these are the main choices for the distribution that you might put on your card.
I choose to use Raspbian, a Debian distribution of Linux, on my Raspberry Pi projects for a couple of reasons. First, the Debian distribution is used as the basis for another distribution, Ubuntu, and Ubuntu is arguably the most popular distribution of Linux, which makes it a good choice because of the community support it offers. Also, I personally use Ubuntu when I need to run Linux on my own personal computer. It provides a complete set of features, is well organized, and generally supports the latest hardware and software. Having roughly the same version on both my personal computer and my Raspberry Pi makes it easier for me to use both, as they operate, at least to a certain degree, in the same way. I can also try some things on my computer before trying them on Raspberry Pi. I've also found that Ubuntu/Debian has excellent support for new hardware, and this can be very important for your projects.
So, we are going to install and run a version of Debian, Raspbian, on our Raspberry Pi.
There are two approaches to getting Raspbian on our board. The board is getting popular enough for us to buy an SD card that already has Raspbian installed, or you can download it onto your personal computer and then install it on the card. I'll assume you don't need any directions if you want to purchase a cardâsimply do an Internet search for companies selling such a product.
If you are going to download a distribution, you need to decide if you are going to use a Windows computer to download and create an SD card, or a Linux machine. I'll give brief directions for both here.
First, we'll need to download an image. This part of the process is similar for either Windows or Linux. Open a browser window, go to the Raspberry Pi organization's site www.raspberrypi.org, and select the Downloads selection at the top of the page. This will give you a variety of download choices. Go to the Raspbian section and select the
.zip file just to the right of the image identifier. This will download an archived file that has the image for your Raspbian operating system. Note the default user name and password; you'll need them later.
If you're using Windows, you'll need to unzip the file using an archiving program such as 7-Zip. This will leave you with a file that has the
.img extension; a file that can be imaged on your card. Next, you'll need a program that can write the image to the card. I use the Image Writer for Windows program. You can find a link to this program at the top of this section on the www.raspberrypi.org website. Plug your card into the PC, run this program, and you should see the following screenshot:
Then select Write. This will take some time, but when complete, eject the card from the PC.
If you are using Linux, you'll need to unarchive the file and then write it to the card. You can do all of this with one command. However, you do need to find the
/dev device label for your card. You can do this with the
ls -la /dev/sd* command. If you run this before you plug in your card, you might see something like the following screenshot:
Notice that your card is at
sdb. Now, go to the directory where you downloaded the archived image file and issue the following command:
sudodd if=2013-09-25-wheezy-raspbian.img of=/dev/sdX
2013-09-25-wheezy-raspbian.img command will be replaced by the image file that you downloaded, and the
/dev/sdX command will be replaced by your card ID, in this example,
/dev/sdb.Eject. Once the file is written, eject the card and you are ready to plug it into the board and boot.
Make sure your Raspberry Pi is unplugged and insert the SD card into the slot. Then switch on the device. After the device boots, you should get a screen that looks like the following screenshot:
You are going to do two things, and perhaps something else as well, based on your personal preference. First, you'll want to expand the filesystem to take up the entire card. So hit the Enter key, and you'll see the following screenshot:
When you hit Enter, you'll see the following screenshot:
I prefer to select the middle selection, namely, Desktop Log in as user 'pi' at the graphical desktop. It normally sets up the system the way I like to have it boot up. You could also choose Console Text console, requiring login (default); however, you will need to log in whenever you want to access the graphical environment, for example, while using the vncserver, which we will cover later.
The final choice you can make is to change the overclocking on Raspberry Pi. This is a way for you to get higher performance from your system; however, there is a risk that you can end up with a system that has reliability problems. Here is the warning that comes up when you make this selection:
Once you are done and are back to the main configuration menu, hit the Tab key until you are positioned over the <Finish> selection, then hit Enter. Then hit Enter again so that you can reboot your Raspberry Pi. Now when you boot, your system will take you all the way into the Windows screen. Raspberry Pi uses the LXDE Windows system, and should look like the following screenshot:
Now when the Windows system is up and running, you can bring up a terminal by double-clicking on the LXTerminal selection on the screen. You should end up with a terminal window that looks like the following screenshot:
You are now ready to start interacting with the system!
Two questions arise: do we need an external computer during the creation of our projects? And what sort of computer do we need? The answer to the first question is a resounding yes. Most of our projects are going to be self-contained robots with very limited connections and display space; we will be using an external computer to issue commands and to see what is going on inside our robotic projects. The answer to the second question is a bit more difficult. Because your Raspberry Pi is working in Linux, most notably a version of Debian that is very closely related to Ubuntu, there are some advantages of having an Ubuntu system available as your remote system. You can then try some things on your computer before trying them in your embedded system. You'll also be working with similar commands for both, and this will help your learning curve.
However, the bulk of personal computers today run some sort of Windows operating system, so that is what will normally be available. Even with a Windows machine, you can issue commands and display information, so either way will work. I'll try to give practical examples for both.
There is one more choice, and it is the choice that I actually prefer. I have access to both systems on my PC. Previously, this was done by a process called dual booting, where both systems were installed on the computer and the user chose which system they wanted to run on boot up. Changing systems in this kind of a configuration was time consuming, and used up a lot of disk space. However, there is a better way available now.
On my Windows PC, I have a virtual Ubuntu machine running under a free program from Oracle called VirtualBox. This program lets me run a virtual Ubuntu machine hosted by my Windows operating system. That way, I can try things in Ubuntu yet keep all the functionalities of my Windows machine. I'm not going to explain how to install this; there is plenty of help on the Web; just search for Ubuntu and VirtualBox. There are several websites that offer easy, step-by-step instructions. One of my favorites is http://www.psychocats.net/ubuntu/.
You now have a very usable Debian computer system. You can use it to access the Internet, write riveting novels, balance your accountsâjust about anything you could do with a standard personal computer. However, this is not your purpose; you want to use your embedded system to power our delightfully inventive projects. In most cases, you wouldn't want to connect a keyboard, mouse, and display to your projects. However, you still need to communicate with your device, program it, and have it tell you what is going on and when things don't work right. We'll spend some time in this section establishing remote access to our device.
The following are three ways by which we are going to access our system from our external PC:
Through a terminal interface called SSH.
If you are using Microsoft Windows on your remote computer, I'll show how you can transfer files via a program called WinSCP, which is custom-made for this purpose. You can also use WinSCP to transfer a file in Linux; I'll show you how to do this as well.
So, first make sure your basic system is up and working. Open a terminal window and check the IP address of your unit. You're going to need this no matter how you want to communicate with the system. Do this by issuing the
ifconfig command. It should look like the following screenshot:
inet addr, shown on the second line, to contact your board via LAN. You'll also need an SSH terminal program running on your remote computer. An SSH terminal is a Secure Shell Hypterminal (SSH) connection, which simply means you'll be able to access your board and type in commands at the prompt, just like you have done previously. If you are running Microsoft Windows, you can download such an application. My personal favorite is PuTTY. It is free and does a very good job of allowing us to save our configuration so we don't have to type configurations each time. Type
putty in a search window and you'll soon come to a page that supports a download, or you can go to www.putty.org.
When you click on Open, the system will try to open a terminal window onto your Raspberry Pi via the LAN connection. The first time you do this, you will get a warning about an RSA key, as the two computers don't know about each other, so Windows is complaining that a computer that it doesn't know is about to be connected in a fairly intimate way. Simply click on OK, and you should get a terminal with a login prompt as shown in the following screenshot:
Now, you can log in and issue commands to your Raspberry Pi. If you'd like to do this from a Linux machine, the process is even simpler. Bring up a terminal window and then type
sshpi22.214.171.124 âp 22. This will then bring you to the login screen of your Raspberry Pi, which should look similar to the previous screenshot.
SSH is a really useful tool to communicate with your Raspberry Pi. However, sometimes you need a graphical look at your system, and you don't necessarily want to connect a display. You can get this on your remote computer using an application called vncserver. You'll need to install a version of this on your Raspberry Pi by typing
sudoapt-getinstall tightvncserverin in a terminal window on your Raspberry Pi. By the way, this is a perfect opportunity to use SSH.
TightVNC is an application that will allow you to remotely view your complete Windows system. Once you have it installed, you'll need to start the server by typing
vncserver in a terminal window on Raspberry Pi. You will then be prompted for a password, be prompted to verify it, and then asked if you'd like to have a view-only password. Remember the password you entered; you'll need it to remotely log in via a VNC Viewer.
Type in the password you just entered while starting the vncserver; you should then get a graphical view of your Raspberry Pi that looks like the following screenshot:
You can now access all the capabilities of your system, albeit they may be slower if you are doing graphics-intense data transfer. Just a note: there are ways to make your vncserver start automatically on boot. I have not used them; I choose to type the command
vncserver from an SSH application when I want the application running. This keeps your running applications to a minimum and, more importantly, ensures fewer security risks. If you'd like to start yours each time your system boots, there are several places on the Internet that will show you how to configure this. You can try http://www.havetheknowhow.com/Configure-the-server/Run-VNC-on-boot.html.
Vncserver is also available via Linux. You can use an application called Remote Desktop Viewer to view the remote Raspberry Pi Windows system. If you have not installed this application, install it using the update software application based on the type of Linux system you have. Once you have the software, run the application and you should see the following screenshot:
Make sure vncserver is running on Raspberry Pi; the easiest way to do this is to log in using SSH and run vncserver at the prompt. Now click on Connect on the Remote Desktop Viewer running in Linux, shown in the following screenshot:
Now you should see your LXDE window that is running on Raspberry Pi, as shown in the following screenshot:
The final piece of software I like to use with my Windows system is a free application called WinSCP. To download and install this software, simply search the Web for WinSCP and follow the instructions. Once installed, run the program; it will open the following dialog box:
Click on New and you will get the following screenshot:
The host computer, again, doesn't know the remote computer. Click on Yes and the application will display the following screenshot:
Now we can drag-and-drop files from one system to the other. You can also do similar things on Linux using the command line. To transfer a file to the remote Raspberry Pi, you can use the command
scp [email protected]:path, where file is the filename and
[email protected]:path is the location you want to copy. For example, if I want to copy the term
robot.py from my Linux system to Raspberry Pi, I would type
scp robot.py [email protected]:/home/pi/. Now the system will ask me for the remote password; this is the login for Raspberry Pi. Enter the password and the file should be transferred. Once we've completed this step, we can now access our system remotely without connecting a display, keyboard, and mouse. Now your system will look like the following image:
You need to connect the power and LAN. If you need to issue simple commands, you can connect via SSH. If you need a more complete set of graphical functionalities, you can access them via vncserver. Finally, if you are using a Windows system and want to transfer files back and forth, you need access to WinSCP. Now you have the toolkit to build your first set of capabilities.
One of the challenges of accessing the system remotely is that you need to know the IP address of your board. If you have the board connected to a keyboard and display, you can always just run
ifconfig to get this info. But you're going to use the board in applications where you don't have this information. There is a way to discover this by using an IP scanner application. There are several available for free; on Windows, I use an application known as Advanced IP Scanner. When I start the program, it looks like the following screenshot:
Clicking on the Scan selector scans for all the devices connected to the network. You can also do this in Linux; one application for IP scanning in Linux is Nmap. To install Nmap, type
sudo apt-get installnmap. To run Nmap, type
sudonmap -sP 10.25.155.1/254, and the scanner will scan the addresses from 10.25.155.1 to 10.25.155.254.
These scanners let you know which addresses are being used; this should then let you find your Raspberry Pi address without typing
Your system has lots of capabilities. Feel free to play with the system; try to get an understanding of what is already there and what you'll want to add from a software perspective. One advanced possibility is to connect Raspberry Pi via a wireless LAN connection so that you don't have to connect a LAN connection when you want to communicate with it. There are several good tutorials on the Internet. Try http://learn.adafruit.com/adafruits-raspberry-pi-lesson-3-network-setup/setting-up-wifi-with-occidentalis or http://www.howtogeek.com/167425/how-to-setup-wi-fi-on-your-raspberry-pi-via-the-command-line/.
Congratulations! You've completed the first stage of your journey. You have your Raspberry Pi up and working. No gathering dust in the bin for this piece of hardware. It is now ready to start connecting to all sorts of interesting devices in all sorts of interesting ways. You should have installed a Debian operating system, learned how to connect all the appropriate peripherals, and even mastered how to access the system remotely so that the only connections you need are a power supply cable and a LAN cable.
You are now ready to start commanding your Raspberry Pi to do something. The next chapter will introduce you to the Linux operating system, the Emacs text editor, and also show you some basic programming concepts in both the Python and C programming languages. You'll then be ready to add open source software to inexpensive hardware to start building your robotics projects.