© Jeff Cicolani 2021
J. CicolaniBeginning Robotics with Raspberry Pi and Arduinohttps://doi.org/10.1007/978-1-4842-6891-9_2

2. An Introduction to Raspberry Pi

Jeff Cicolani1  
(1)
Pflugerville, TX, USA
 

The purpose of this book is to challenge you to build a simple robot that will be expanded over time. This book is intended to be difficult; however, it isn’t too difficult or unnecessarily complicated. You’ll experience plenty of complications along the way, but the installation of the operating system on your Raspberry Pi does not need to be one of them.

Downloading and Installing Raspberry Pi OS

There are, essentially, two methods of installing the operating system (OS) on your Pi.

The first involves downloading the latest Raspberry Pi OS image, writing it to an SD card, and going from there. This method requires the installation of a third-party software package that writes a bootable image on an SD card. The advantage is that it takes less room on your SD card. If you’re using a minimum 8 GB SD card, this may be helpful; if you went bigger, then this consideration is moot.

While the direct installation is not all that complicated (rather easy actually), there’s an easier way that involves installing minimal software on your system. Raspberry Pi Imager is designed to make the installation and configuration of your Raspberry Pi easier. It allows you to select from multiple operating systems and simply install.

In the end, the choice is yours. I’ll go over both options so you can choose the installation path which works best for you. No matter which option you choose, your journey begins on the Raspberry Pi OS download page at www.raspberrypi.org/software/operating-systems/ (see Figure 2-1).
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig1_HTML.jpg
Figure 2-1

Raspberry Pi OS download screen

Raspberry Pi OS with OpenCV

Toward the end of this book, we will work with computer vision to show you why you should use a Raspberry Pi rather than a less capable platform. In order to do that, however, you need to install OpenCV on your Pi. Previously, there was no simple OpenCV installer for the Raspberry Pi. Now there is a simple method for installing OpenCV entirely within Python. But don’t worry about that now. We’ll be covering Python in Chapter 3 and OpenCV in Chapter 9.

The “Hard” Way

The more difficult method installs the Raspberry Pi OS image directly on the SD card—ready to boot up. This is the method that I use because it really isn’t any more complicated than the other method and it allows me to use versions that are not available through Raspberry Pi Imager.

You have two options for your Raspberry Pi OS installation. The first option is Raspberry Pi OS with PIXEL, their optimized GUI. It is a 2.5 GB download, and it is a 7.1 GB image once it’s been decompressed. The second option is Raspberry Pi OS Lite, a minimal image that is a much smaller 435 MB download (1.8 GB after decompression). However, minimal means no GUI, so everything is done via the command line. If you’re a fan of headless Linux, then this is the option for you. We will use the larger install with PIXEL.

If you have a BitTorrent client installed, click Download Torrent. This is much faster than downloading the .zip file.
  1. 1.
     
  2. 2.

    Select the Raspberry Pi OS version that you want to install and click Download. I selected Raspberry Pi OS with desktop and recommended software. This will have the IDEs for Python and Arduino we will need later.

     
  3. 3.

    Once the download is complete, decompress the file somewhere you’ll easily find it.

     
  4. 4.

    Download and install Win32 Disk Imager. This allows you to write the image file that you just downloaded to the micro SD card. You can get it at https://sourceforge.net/projects/win32diskimager/.

     
  5. 5.

    Optionally, you may also want to download SDFormatter to make sure that your SD card is properly prepared. You can get it at www.sdcard.org/downloads/formatter_4/.

     
  6. 6.

    Insert your micro SD card into the card reader connected to your computer.

     
  7. 7.

    If you have downloaded and installed SDFormatter, open it. You should see a dialog box similar to the one shown in Figure 2-2.

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig2_HTML.jpg
Figure 2-2

SD Card Formatter

  1. 8.

    Make sure that you select the drive representing your SD card. You’re about to format it, so if you select the wrong thing, it will wipe out whatever you have on that drive. The tool usually selects the right one by default, but double-check. It would be wise to disconnect any other external storage devices.

     
  2. 9.

    Make sure that Format size adjustment is set to On. This removes any other partitions on the card and uses the whole thing. Leave all the other settings at the default.

     
  3. 10.

    Click Start. When the process finishes, you’re ready to install the OS.

     
  4. 11.

    To flash the image to the SD card, open Win32 Disk Imager.

     
  5. 12.

    In the image file field, select the Raspberry Pi OS image that you downloaded. You can click the file folder icon to navigate to it.

     
  6. 13.

    Make sure that your SD card is selected in the device drop-down box. Again, selecting the wrong device can lead to a world of hurt; so pay attention.

     
  7. 14.

    Click Write.

     
  8. 15.

    Once the process has completed, remove the card from your card reader.

     
  9. 16.

    Insert the card into the micro SD card reader on the Raspberry Pi.

     

That sounds lengthy, but it is remarkably fast and easy to do. Next, let’s walk through the NOOBS installation process.

The “Easy” Way

I call this method the “easy” way, although the hard way is actually pretty easy. What makes this easy is the imager does all the work for you. You will probably want to format the card, but if it’s a new card, that may not be necessary.

You have the same two options as the “hard” method: Raspberry Pi OS or Raspberry Pi OS Lite.
  1. 1.

    Click the Raspberry Pi Imager for your operating system on the download page. I selected the Windows installation.

     
  2. 2.

    Optionally, you may also want to download SDFormatter to make sure that your SD card is properly prepared. You can get it at www.sdcard.org/downloads/formatter_4/.

     
  3. 3.

    If you downloaded and installed SDFormatter, open it.

     
  4. 4.

    Make sure that you select the drive representing your SD card. You’re about to format it, so if you select the wrong thing, it will wipe out whatever you have on that drive. The tool usually selects the right one by default, but double-check. It would be wise to disconnect any other external storage devices.

     
  5. 5.

    Make sure that Format size adjustment is set to On. This removes any other partitions on the card and uses the whole thing. Leave all the other settings at the default.

     
  6. 6.

    Click Start. When the process has finished, you’re ready to install the OS.

     
  7. 7.

    Install Raspberry Pi Imager and open it.

     
  8. 8.

    Select your Raspberry Pi OS flavor, if you have.

     
  9. 9.

    Select the SD card.

     
  10. 10.

    Click “Write.”

     
  11. 11.

    Remove the card from your card reader.

     
  12. 12.

    Insert the card into the micro SD card reader on the Raspberry Pi.

     

The installation should be complete.

Connecting Raspberry Pi

Now that your micro SD card is ready to go, you need to hook up your Raspberry Pi. If you’re using an original, first-generation Pi, this is a little more complicated.

Every model after the original, however, includes multiple USB ports and an HDMI connector to make things easier. Hooking up the Pi is very simple.
  1. 1.

    Connect your monitor via the micro HDMI port. If you are using a small television that is outfitted with component hookups rather than HDMI, the audio jack on the Pi is a four-pole component jack. You need an RCA-to-3.5 mm converter, usually in cable form, to do this.

     
  2. 2.

    Connect your keyboard and mouse to the USB ports. I use a wireless keyboard/touchpad combination because it’s compact and portable.

     
  3. 3.

    Make sure that your micro SD card with Raspberry Pi OS is installed in the micro SD port on the Pi. Essentially, this is the hard drive for your small computer, so it has to be in the right place. It will not read the OS through an SD card reader connected to one of the USB ports.

     
  4. 4.

    If you are using an Ethernet cable, connect it to the Ethernet port. You may also plug a Wi-Fi dongle to the USB port. If you are using a Pi 3 or newer, as I am, Wi-Fi is built in.

     
  5. 5.

    Connect the 5 V power to the micro USB port. This port is only for power. You cannot access the board via USB.

     
That’s it. Your Raspberry Pi should look similar to what’s shown in Figure 2-3. The Pi should be booting on your monitor.
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig3_HTML.jpg
Figure 2-3

Raspberry Pi 3 connections

Configuring Your Pi

The first time you boot up your Pi, it will do a couple things. First, it will automatically expand the file system to take advantage of the entire SD card. It will then reboot to the GUI. Once there, the OS will walk you through a configuration wizard that will, among other things, set your location, prompt for a new password, and connect to your Wi-Fi. Much of the configuration used to be managed through a terminal program called raspi-config. In the current version of the Raspberry Pi OS, this is handled through this configuration wizard and the preferences tool, both accessed through the GUI. Figure 2-4 shows the Raspberry Pi at initial start with the configuration wizard.
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig4_HTML.jpg
Figure 2-4

Initial start screen

The configuration wizard will walk you through personalizing your Pi. Once the initial configuration is complete, we’ll be using the configuration tool to make a few more changes. But, first, let’s get your Pi set up.
  1. 1.

    Set your location. Select your country, language, and time zone. Be sure to click the two checkboxes at the bottom and click Next.

     
  2. 2.

    Now we will change the default user password. By default, the password is “raspberry.” Default passwords are not safe, so you’ll want to change this.

     
  3. 3.

    Next, we set up the screen. On some setups, there will be a thick black border around the desktop. If you see this border, click the checkbox and then Next.

     
  4. 4.

    The next two screens are for connecting to your local Wi-Fi. You will select your network on this screen and provide the password on the next.

     
  5. 5.

    The final step in this process is to update the software. This will take a few minutes once it’s kicked off, so it may be a good time to grab a coffee.

     
  6. 6.

    Once the software is updated, you will need to restart for the changes to take place. It’s a good idea to do it now, though you may choose to defer the reboot until after the next step in configuration.

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig5_HTML.jpg
Figure 2-5

Raspberry Pi Configuration

We’re not done with the configuration just yet. Now we will make a couple changes to customize your installation and prepare it for future use. We’ll be changing the hostname, change the boot process, and enable SSH, VNC, and I2C.

The hostname is how your Raspberry Pi appears on the network. You’ll want to give your Pi a unique name, especially when you consider how many of them may be on the network at any given time. The hostname should be both meaningful to the application and unique. This becomes particularly important when you’re in a room with 20 other Raspberry Pis.

SSH allows us to access the Pi through a terminal window (SSH client) from another computer. On Windows, PuTTY is a very popular, free SSH client. SSH does not provide a GUI. All interactions are made using terminal commands. This is helpful if you want to quickly execute a program, install software, and so forth. As you become more familiar with the terminal, you will likely find yourself using SSH to connect for simple commands while reserving VNC (remote desktop) for more involved tasks such as writing programs.

I2C is a serial communications protocol that is very popular in embedded systems such as the Pi, Arduino, and so forth. It allows for robust communication with multiple devices by using a number of pins. The motor control board that we will use communicates via I2C. (If you later choose to add other boards, such as a servo control board, it will also use I2C.) As long as the devices have different addresses, you can keep stacking them.

Because we also plan to use the Raspberry Pi headless (without a monitor, keyboard, or mouse attached), let’s set it up to boot into the console automatically. Don’t worry; it’s easy enough to launch the desktop GUI when you want to, as you will see.
  1. 1.

    Click the Raspberry logo at the top left of the desktop. This will open the main menu. For you Windows users, this is essentially the Start menu.

     
  2. 2.

    Click Preferences and then Raspberry Pi Configuration. It should open a window that looks like Figure 2-5.

     
  3. 3.

    On the System tab, change the hostname to something unique.

     
  4. 4.

    Next, you will click Change Password to update the password, again. At the time of writing this book, the changes made in the wizard did not stick, so you’ll need to do it here as well.

     
  5. 5.

    Change the “boot” setting to CLI. This will boot the Pi without the GUI. Since we’ll be setting up remote desktop, we won’t want to load the GUI here.

    If you need to load the GUI from the Pi, you can use the startx command after you’ve logged in.

     
  6. 6.

    Disable auto login. This is a security concern and, since we’re not logging on to the Pi directly, auto login is unnecessary.

     
  7. 7.

    On the Interface tab, you will want to enable SSH, VNC, and I2C. These three protocols are what we’ll be using to connect remotely to the Pi and allow the connection to the Motor HAT, later.

     
  8. 8.

    Click OK to save these changes.

     

At this point, your Pi reboots. This may take a few minutes, especially if you did not install via NOOBS and the Pi has to expand your file system.

Remember, we set up the Pi to boot into the console by default. Since the next few steps are all done via the command line, we’ll not need to load the GUI. However, let’s do it anyway so that you can see how easy it is.
  1. 1.

    Type startx and press Return.

     
You’re now in the GUI desktop. To exit the desktop, do the following:
  1. 1.

    Click the programs menu (the raspberry in the upper-left corner).

     
  2. 2.

    Click Logout. This will display the Shutdown options dialog box (Figure 2-6).

     
  3. 3.

    Select Exit to command line.

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig6_HTML.jpg
Figure 2-6

Shutdown options

You should now be back to the command line.

Users

The default user on every installation of Raspberry Pi OS is pi. Earlier, we changed the password to make it more secure. However, you probably don’t want to always log on as the pi user.

Remember when I said we’d start using the terminal more? Well, that starts now. The easiest way to create and manage users is through the command line. We’re going to walk through that process now.

Securing Root

In addition to the default user, pi, there is another default user on the Pi. This is the root user. The root user is, essentially, an administrative user that is used by the machine to execute low-level commands. This user has access to everything and can do anything because, well, it’s the machine. Unlike the default pi user, however, root does not have a default password. It has no password.

So, while we’re configuring and securing the computer for our robot, let’s go ahead and give the root user a password.
  1. 1.

    Open a terminal window.

     
  2. 2.

    Type sudo passwd root. (Note that passwd is the proper command and not a typo.)

     
  3. 3.

    Enter the new password for the root user.

     
  4. 4.

    Enter the password again to confirm.

     

Your root user is now secured, which is good because you’ll need it for the next step in the configuration.

Change the Default Username

The first thing you’re going to do is change the default username to something of your choosing. What this will do is replace the username pi with your own username. This provides another layer of security on the device; now, not only would someone need to figure out the password but they wouldn’t even have the default username to work with. It also preserves some of the special, undocumented permissions that the default user is given.
  1. 1.

    Log out of the pi user. You can do this through the menu system or by simply typing logout in a terminal.

     
  2. 2.

    Log on with your—now secure—root user.

     
  3. 3.

    Type

     
usermod -l <newname> pi
<newname> is the new username that you chose. Do not include < or > in the command.
  1. 4.

    To update the home directory name, type

     
usermod -m -d /home/<newname> <newname>
Again, <newname> is the new username that you used in the previous step.
  1. 5.

    Log out of the root user and log back in with your new username.

     

At this point, you have changed the default user credentials for both the default user and the root user. You have also changed the hostname. This is the minimum needed to secure your Pi and your robot.

Your Raspberry Pi is now set up, configured, and ready for use. There is one more thing we’re going to want to do before we move on to the next chapter, and that is setting up your Pi to be headless.

Making a machine “headless” simply means configuring it so you no longer need to connect a monitor, a keyboard, and a mouse to it to operate it. This is generally done in two ways: with a KVM switch or by setting up remote access. On a mobile robot, connecting a KVM is not really an option. In fact, it would be a little different from simply having everything connected to it. What we want to do is set up the Pi so that we can access it remotely over the network. But first, let’s make sure that you’re connected to your network.

Going Headless

You’re not going to want to haul around an extra monitor, keyboard, and mouse while working through these workshops. To make your life much, much easier, let’s set it up so you can access the Pi headless.

Remote Access

There are two ways to get remote access. One method is to use SSH, which allows you to connect to a remote device using a terminal client. The other method is to set up a remote desktop.

Remote Desktop with xrdp

Let’s start with accessing the desktop remotely from another computer. The following instructions are for Windows users. Most modern Windows installations come with Remote Desktop Connection already installed, which is what we’ll use to connect to the Pi once it’s set up.

Let’s install a couple of services on the Pi: tightVNCserver and xrdp. Theoretically, xrdp should install the VNC server all on its own. In actuality, it does not. At this point, you should be at the command line on your Pi.
  1. 1.

    Type sudo apt-get install tightvncserver.

     
  2. 2.

    Complete the installation.

     
  3. 3.

    Type sudo apt-get install xrdp.

    When the installation is complete, you should be ready to go.

     
To get connected, do the following:
  1. 4.

    On the Pi, type sudo ifconfig.

     
  2. 5.

    Note the Internet address (inet addr) in the eth0 block if you are using an Ethernet cable or the wlan0 block for Wi-Fi.

     
  3. 6.

    On your laptop, open Remote Desktop Connection. This displays the connection dialog box, as shown in Figure 2-7.

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig7_HTML.jpg
Figure 2-7

Windows Remote Desktop Connection

  1. 7.

    Enter the inet addr from your Pi.

     
  2. 8.

    Click Connect.

     
You should see the remote desktop screen with the xrdp login form (see Figure 2-8).
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig8_HTML.jpg
Figure 2-8

XRDP remote desktop login screen

  1. 9.

    Enter your username and password.

     
  2. 10.

    Click OK. This opens the desktop from your Pi (see Figure 2-9).

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig9_HTML.jpg
Figure 2-9

Default Raspberry Pi OS desktop viewed through a remote desktop session

As long as the IP address of your Pi doesn’t change, you no longer need the keyboard, mouse, or monitor to use your Pi.

SSH with PuTTY

The most common SSH client is probably PuTTY. It’s free to use and can be downloaded from www.chiark.greenend.org.uk/~sgtatham/putty/download.html.

The file you downloaded for PuTTY is an executable, which won’t need to be installed. Put it on your desktop or somewhere easy to find. To get connected, do the following:
  1. 1.

    Open the PuTTY interface (see Figure 2-10).

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig10_HTML.jpg
Figure 2-10

PuTTY configuration window

  1. 2.

    Enter your Raspberry Pi’s IP address.

     
  2. 3.

    Click Open.

     
  3. 4.

    You will likely get a security warning, as shown in Figure 2-11, but we know that this is the proper connection, so click Yes.

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig11_HTML.jpg
Figure 2-11

Security warning on first SSH connection with PuTTY

A terminal window opens, asking for your username and password.
  1. 5.

    Enter your username and password. You should now see the terminal prompt, as shown in Figure 2-12.

     
../images/457480_2_En_2_Chapter/457480_2_En_2_Fig12_HTML.jpg
Figure 2-12

Open SSH connection

That’s it. You are now connected via SSH to your Raspberry Pi. You can have multiple connections going at once, but don’t have more than you need to. Multiple connections are handy when you’re working with something like the Robot Operating System (ROS). (Don’t worry; that’s a ways away.) ROS runs multiple programs via the terminal. Each one requires its own terminal window. With PuTTY, you’re able to have as many remote terminal connections as you need.

Finding Your Device on a Network

To access your Pi remotely, you need to know its IP address on the network. Generally, a network switch retains a device’s IP from session to session; however, this isn’t guaranteed.

Finding your device’s IP address on the network can be tricky. If you are at home and have access to your router’s admin panel, this is probably the most straightforward way to find your device. Simply log on to your router, find the list of connected devices, and scroll down until you find the hostname of your Raspberry Pi.

If you need to find the IP address but are away from home, there are a few ways to do it. The easiest is to use an Nmap app on your phone. I use an app called Fing on my Android phone. Once the phone is connected to the local Wi-Fi network, the app scans the network and lists all the other devices on that network. You can just scroll down the list until you find your hostname.

If the network is new to you, your Raspberry Pi will not automatically connect to it. This situation makes it a little tricky. To make things easier, be prepared before you go out. I am a Windows user; if you are not, you need to look up the proper procedure for your OS. I do this operation with the laptop I have when traveling. It allows me to remote into the Pi long enough to connect to the local Wi-Fi and get the wlan0 IP address.

Keep in mind that the IP is assigned by the laptop. The IP you get at the end of this operation will likely not work from any other machine.

On a Windows 7 or later machine, you can perform the following steps to remote into your Pi directly to get its IP address. Make note of the IP address should you ever need to connect directly to the Pi to set up a new Wi-Fi connection. You will need a short Ethernet cable, which should be added to your kit or toolbox.

Make sure that you are able to view your Raspberry Pi with a monitor, keyboard, and mouse set up or through a remote connection via your Wi-Fi network. The Pi cannot be connected to the network via Ethernet cable because that port is needed for this operation.
  1. 1.

    Connect the Ethernet cable to your laptop.

     
  2. 2.

    Connect the other end to the Ethernet port on your Pi.

     
  3. 3.

    Open a terminal window on the Pi.

     
  4. 4.

    Type sudo ifconfig.

     
  5. 5.

    Locate the inet addr in the eth0 block.

     
  6. 6.

    Open a terminal window on your laptop. You can do this by searching cmd in the Start menu.

     
  7. 7.

    Type the following in the Windows terminal:

     
ping <your.Pis.IP.address>
<your.Pis.IP.address> is the eth0 IP address from your Pi.
  1. 8.

    Open Remote Desktop Connection on your laptop.

     
  2. 9.

    Enter the IP address from the Pi and press Enter.

     

You should now have a remote connection from your laptop directly to the Raspberry Pi. Make sure that you save this IP address where you can find it later. Remote Desktop Connection should remember it, but it’s a good idea to also save it somewhere else.

Now whenever you are trying to connect to a new Wi-Fi network, you can use the Ethernet cable to remote into your Pi directly from your laptop. Once remoted in, simply select the network from the list of available networks and enter the passcode, if there is one.

Summary

The Raspberry Pi was designed for the hobbyist maker. The small Linux computer makes it very useful for a lot of different types of projects, but this means that you need to learn a little Linux. The developers at the Raspberry Pi Foundation provide an easy-to-use version of Debian Linux called Raspberry Pi OS.

We took the basic installation a step further by configuring remote access. This allows you to remotely access your robot through your network, which means the monitor and keyboard are no longer needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.48.135