Hands-On Robotics Programming with C++

By Dinesh Tavasalkar
  • Instant online access to over 8,000+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Introduction to the Raspberry Pi

About this book

C++ is one of the most popular legacy programming languages for robotics, and a combination of C++ and robotics hardware is used in many leading industries. This book will bridge the gap between Raspberry Pi and C/C++ programming and enable you to develop applications for Raspberry Pi. To follow along with the projects covered in the book, you can implement C programs in Raspberry Pi with the wiringPi library.

With this book, you’ll develop a fully functional car robot and write programs to move it in different directions. You’ll then create an obstacle - avoiding robot using an ultrasonic sensor. Furthermore, you’ll find out how to control the robot wirelessly using your PC/Mac. This book will also help you work with object detection and tracking using OpenCV, and guide you through exploring face detection techniques. Finally, you will create an Android app and control the robot wirelessly with an Android smartphone.

By the end of this book, you will have gained experience in developing a robot using Raspberry Pi and C/C++ programming.

Publication date:
March 2019
Publisher
Packt
Pages
312
ISBN
9781789139006

 

Chapter 1. Introduction to the Raspberry Pi

Initially developed with the idea of teaching and promoting basic computer programming in schools across the UK, the Raspberry Pi (RPi) became an instant hit. At a price of just $25 when it was initially released, it became so popular that it was, and still is, used by developers, hobbyists, and engineers all over the world.

In this chapter, you will explore the basic idea of a Raspberry Pi. You will then learn to install an operating system on the device. Finally, you will configure Wi-Fi on your Raspberry Pi and learn to connect it to a laptop over Wi-Fi and set up a remote desktop.

You will achieve each of these objectives through the following topics:

  • Understanding the Raspberry Pi
  • Installing Raspbian OS on a Raspberry Pi 3B+
  • Connecting a Raspberry Pi 3B+ to a laptop via Wi-Fi
  • Installing Raspbian OS on a Raspberry Pi Zero W 
  • Connecting a Raspberry Pi Zero W to a laptop via Wi-Fi
 

Technical requirements


For this chapter, the following software and hardware will be required.

Software required

Please download the following software if you want to follow along with the instructions in this chapter:

Note

The installation of all of this software is pretty straightforward. Keep the default settings checked, click on the Next button a few times, and then hit the Finish button once the installation is complete.

Hardware requirements

We need the following hardware to follow along with the instructions in this chapter.

For Raspberry Pi 3B+ and Raspberry Pi Zero W

If you use the Raspberry Pi 3B+ or the Raspberry Pi Zero W, you will need the following hardware:

  • Keyboard
  • Mouse
  • SD card—this should have a minimum of 8 GB of storage, but 32 GB is recommended
  • MicroSD card reader
  • Display unit—a computer monitor or TV that contains an HDMI port
  • HDMI cable
  • 5V mobile charger or power bank. This will power the Raspberry Pi

Additional hardware for Raspberry Pi 3B+

The Raspberry Pi 3B+ needs the following additional hardware:

  • An Ethernet cable

Additional hardware requirements for Raspberry Pi Zero W

Since the Raspberry Pi Zero has a micro USB port and a Micro HDMI port, it needs the following additional hardware:

  • A USB hub
  • A micro USB B-to-USB connector (also known as an OTG connector)
  • An HDMI-to-mini HDMI connector
 

Understanding the Raspberry Pi


Raspberry Pi is a credit card sized, Linux-based minicomputer invented by the Raspberry Pi Foundation in 2012. The first Raspberry Pi model was called the Raspberry Pi 1B, which was then followed by the Model A. Raspberry Pi boards were initially intended to promote computer science programs in schools. However, their inexpensive hardware and free, open source software, quickly made the Raspberry Pi popular among hackers and robotics developers.

The Raspberry Pi can be used as a fully functional computer. It can be used to perform tasks such as browsing the internet, playing games, and watching HD videos, as well as creating Excel and Word documents. But what really differentiates it from a normal computer is its programmable GPIO pins. The Raspberry Pi consists of 40 digital I/O GPIO pins that can be programmed.

In simple terms, the Raspberry Pi can be thought of as a combination of a minicomputer, as it can be used as a fully fledged computer, and an electronics hardware board, as it can be used to create electronics and robotics projects.

There are different Raspberry Pi models. In this book, we are going to be using the following two models:

  • The Raspberry Pi 3B+
  • The Raspberry Pi Zero W

The Raspberry Pi 3B+

The Raspberry Pi 3B+ was released in February 2018. Its specifications are shown in the following annotated photo:

The Raspberry Pi 3B+ specifications are as follows:

  • A Broadcom BCM2837 quad-core 1.4 GHz processor
  • 1 GB RAM
  • A Broadcom VideoCore GPU
  • Bluetooth 4.2
  • Dual-band 2.4 GHz and 5 GHz Wi-Fi
  • An Ethernet port
  • Storage with a microSD card via a microSD slot
  • 40 programmable GPIO pins
  • Four USB 2.0 ports
  • An HDMI port
  • A 3.5 mm audio jack
  • The Camera Serial Interface (CSI), used for connecting the Raspberry Pi Camera directly to the Raspberry Pi

The Raspberry Pi Zero W

If we were looking for a smaller-sized version of the Raspberry Pi, we could instead opt for the Raspberry Pi Zero W. TheW stands for wireless, as the Raspberry Pi Zero W has built-in Wi-Fi. The following is an annotated photo of the Raspberry Pi Zero W:

The Raspberry Pi Zero W model costs around $10. There is also Raspberry Pi Zero, without the W, which costs around $5, but this does not have built-in Wi-Fi, which makes it very difficult to connect it to the internet. The Raspberry Pi Zero W, which was released in 2017, is basically an updated version of the Raspberry Pi Zero, released in 2015.

Note

Later on in this book, when we design our robot, we will learn how to upload a program wirelessly to our Raspberry Pi from a laptop via a Wi-Fi network. If you opt to purchase the smaller version of Raspberry Pi, I recommend that you go with Raspberry Pi Zero W instead of Raspberry Pi Zero, for ease of use.

The Raspberry Pi Zero W has a couple of disadvantages, due to its small size. Firstly, it's a bit slower than the Raspberry Pi 3B+. Secondly, if we want to use it as a minicomputer, we would need to purchase different extensions to connect peripherals such as a keyboard, a mouse, or a monitor. If we are going to use the Raspberry Pi Zero W for building electronics and robotics projects, however, we don't need to worry about this drawback. Later on in this book, we will learn how to connect the Raspberry Pi Zero W to a laptop via Wi-Fi and how to use the laptop to control it as well. 

The specifications of the Raspberry Pi Zero W are as follows:

  • A Broadcom ARM11 1 GHz processor
  • 512 MB RAM
  • A Broadcom VideoCore GPU
  • Bluetooth 4.0
  • Dual-band 2.4 GHz and 5 GHz Wi-Fi
  • Storage with a microSD card via a microSD slot
  • 40 programmable GPIO pins
  • A mini HDMI port
  • The Camera Serial Interface (CSI), used for connecting the Raspberry Pi Camera directly to the Raspberry Pi
 

Setting up a Raspberry Pi 3B+ as a desktop computer


To set up the Raspberry Pi 3B + and install the Raspbian OS on it, we will need various hardware and software components. The hardware components include the following: 

  • A laptop to install Raspbian OS on a microSD card.
  • A keyboard.
  • A mouse.
  • An SD card—a minimum of an 8 GB memory card is more than sufficient, but with an 8 GB card, the default OS will occupy 50 percent of memory card space. Later on in this chapter, we will also install OpenCV on your Raspberry Pi, and since OpenCV will also occupy a lot of space on your memory card, you will need to uninstall some default software. So, I recommend you use a 16 GB or 32 GB memory card—with a 32 GB memory card, the default OS only occupies 15 percent of the card's space.  
  • An SD card reader.
  • A display unit—this can be a computer monitor or TV, as long as it features an HDMI port.
  • An HDMI cable.
  • A mobile charger or a power bank to power the Raspberry Pi.

The software components required include the following:

  • Etcher 
  • Raspbian Stretch with Desktop OS

Now that we know what we need to install the OS, let's start installing it.

Installing Raspbian OS on an SD card

To install Raspbian OS on a microSD card, we will first install Etcher on our computer. After that, we will insert the microSD card into a microSD card reader and connect it to our computer.

Downloading and installing Etcher 

Etcher will first format the microSD card, and then write the Raspbian Stretch image to it. Let's start by installing Etcher:

  1. On your browser, go to http://www.etcher.io/.
  2. Select your OS from the drop-down menu. Etcher will start downloading, as shown in the following screenshot:
  1. Once it has downloaded, open the setup file and install Etcher.

Now that Etcher is set up, let's move on to Raspbian.

Downloading the Raspbian Stretch image 

We now have to download an OS to run on our Raspberry Pi. While there are many third-party Raspberry Pi OSes available, we will install Raspbian OS. This OS is based on Debian and is developed specifically for the Raspberry Pi. The newest version is called Raspbian Stretch.

To download the Raspbian Stretch image, visit https://www.raspberrypi.org/downloads/raspbian/, look for the RASPBIAN STRETCH WITH DESKTOP ZIP file, and click on the Download ZIP button, as shown in the following screenshot:

Now that we have a copy of Raspbian Stretch on our laptops, let's move on to writing it to our microSD cards.

Writing the Raspbian Stretch image to a microSD card

After downloading Etcher and the RaspbianStretch image, let's write Raspbian Stretch to our microSD card:

  1. Insert the microSD card into the microSD card reader, then connect the card reader to your laptop via USB:
  1. Next, open Etcher and click on the Select Image button. After this, select the Raspbian Stretch ZIP file and click on Open:
  1. After that, make sure that the microSD card reader drive is selected, as in the following screenshot. If any other drive is selected by mistake, click on the Change button and select the microSD card drive. Click on the Flash! button to write the Raspbian OS to the microSD card:

Note

The process of writing or flashing an image to an SD card is also called booting.

It will take around 15-20 minutes for Etcher to flash your SD card with the Raspbian OS:

Once the OS is written to the SD card, Etcher will automatically eject the microSD card reader.

Now that we've written Raspbian Stretch to our microSD card, let's begin setting up the Raspberry Pi 3B+.

Setting up the Raspberry Pi 3B+

After booting the Raspbian OS from the microSD card, we will set up the Raspberry Pi by connecting different peripherals to it, as follows:

  1. Insert the microSD card into the SD card slot, which is located on the back of the Raspberry Pi 3B+:
  1. Connect the keyboard and mouse to the USB ports of the Raspberry Pi 3B+. It is also possible to use a wireless keyboard and mouse:
  1. The Raspberry Pi 3B+ contains an HDMI port with which we can connect the RPi to a display unit, such as a computer monitor or TV. Connect one end of the HDMI cable to the Raspberry Pi's HDMI port and the other end to a display unit:
  1. Finally, to turn the Raspberry Pi on, we need to provide it with power. A typical Raspberry Pi requires 5V of power and ideally 2.5A of current. There are two methods that we can use to supply power to the Raspberry Pi: 
    • Smartphone charger: Most smartphone chargers provide a 5V voltage output and 2.5A of current output. If you take a closer look at your smartphone charger, you will find the maximum voltage and the current output value printed on it, as shown in the following photo. On my charger, the current output of 3A indicates the maximum current output. The charger, however, will only provide the current output as required for the RPi and not the maximum current of 3A. Note that the Raspberry Pi contains a micro USB B port, so, to connect to the power port of Raspberry Pi, we need to connect a micro USB B wire to our charger:
    • Power bank or battery bank: Alternatively, we can use a power bank or a battery bank. As mentioned earlier, we need to connect the power bank to the Raspberry Pi via micro USB B port, and we also need to make sure that it provides 5V of voltage output and around  2.5A of current output:
  1. Once everything is plugged in, turn the display unit on and make sure that you have selected the correct HDMI option.
  2. Next, turn on the power supply. You will see the red LED on your Raspberry Pi turn on. Wait for around 10-20 seconds for the Raspberry Pi to boot up. Once this is done, you will see the following screen:

Now that we've got our Raspberry Pi 3B+ running, let's connect it to the internet.

Connecting the Raspberry Pi 3B+ to the internet

There are two methods that we can use to provide an internet connection to the Raspberry Pi:

  • An Ethernet cable: The Raspberry Pi 3B+ contains an Ethernet port. To provide an internet connection via an Ethernet port, simply connect an Ethernet cable to it.
  • Wi-Fi: Connecting the Raspberry Pi over Wi-Fi is also pretty straightforward. Click on the Wi-Fi icon in the taskbar. Select your Wi-Fi network, enter the correct password, and the Raspberry Pi will connect to the desired Wi-Fi network:

After setting up the Raspberry Pi 3B+ as a desktop computer, we can simply open any code editor and start writing programs to control motors or LEDs with the Raspberry Pi.

Since we are going to create a movable robot using the Raspberry Pi, however, the desktop computer setup will not work. This is because the display, the keyboard, and the mouse, all of which are attached directly to the Pi, will limit its movement. In the next section, to be able to use it without these peripherals, we'll look at how to connect the Raspberry Pi 3B+ wirelessly to a laptop via Wi-Fi. 

 

Connecting the Raspberry Pi 3B+ to a laptop via Wi-Fi


To connect the Raspberry Pi 3B+ wirelessly to a laptop via Wi-Fi, we first need to connect the RPi to a Wi-Fi network with a piece of software called PuTTY. After that, we can find out the IP address of the Raspberry Pi and enter that into a piece of software called VNC Viewer to connect the Raspberry Pi to a laptop. In order to perform this task successfully, the Raspberry Pi and laptop must be connected to the same Wi-Fi network.

The hardware required includes the following:

  • An Ethernet cable: The Ethernet cable will be attached directly to the Ethernet port of the Raspberry Pi 3B+ and the Ethernet port of the laptop. If your laptop does not contain an Ethernet port, you will need to purchase a USB-to-Ethernet connector for your laptop.
  • Micro USB B wire: This is a standard Micro USB B wire for connecting the Raspberry Pi 3B+ to the laptop.

The software required is PuTTY, VNC Viewer, and Bonjour. 

Creating an SSH file on a microSD card

After installing the aforementioned software, we need an SSH file on the microSD card to enable SSH for the Raspberry Pi 3B+. To do this, perform the following steps:

  1. Open the drive allotted to the SD card. In our case, this is the boot (F:) drive. As shown in the following screenshot, there are some files on the microSD card:
  1. To create the SSH file, right-click in the drive, click on New, and select a Text Document, as shown here:
  1. Give this text file the name ssh but do not include the .txt extension. We will get a warning stating that this file will become unstable because it doesn't have an extension. Click on the Yes button:
  1. Next, right-click on the ssh file and select the Properties option. Inside Properties, click on the General tab. We should see that the Type of file is set to File. Click OK:

After creating an SSH file on a microSD card, remove the card reader from the laptop and insert the microSD card into the Raspberry Pi 3B+.

In the next section, we will look at how to connect the RPi 3B+ to a Wi-Fi network. The setup is done on a Windows system. If you have a Mac, then you can follow one of the following tutorial videos:

Connecting the Raspberry Pi 3B+ to a Wi-Fi network using PuTTY

After inserting the microSD card into the RPi, let's see how to connect the Raspberry Pi to a Wi-Fi network using PuTTY:

  1. First, connect one end of the Ethernet cable to the Ethernet port of the Raspberry Pi, and the other end to the Ethernet port of the laptop. 
  2. Next, power up the Raspberry Pi 3B+ by connecting it to the laptop using a micro USB B cable. We will see the red power LED turn on. We will also see that the yellow LED of the Ethernet port turns on and blinks continuously.
  3. After that, open the PuTTY software. Inside the Host Name box, type raspberrypi.local and click on the Open button:
  1. We will then see a PuTTY Security Alert message. Click Yes:
  1. Inside PuTTY, we will need to enter the credentials of the Raspberry Pi. The default login is pi and the password is raspberry. After entering the password, press Enter:
  1. After that, to connect the Raspberry Pi 3B+ to a particular Wi-Fi network, enter the sudo nano /etc/wpa_supplicant/wpa_supplicant.conf command as shown in this screenshot:
  1. This command will open the nano editor, which will look as follows:
  1. Below the update_config=1 line, enter the name and password for your Wi-Fi, following this syntax:
network={
ssid="Wifi name"
psk="Wifi password"
}

Make sure that you add the preceding code exactly below the update_config=1 line. The Wi-Fi name and the password should be in double quotes (""), as shown here:

After entering the Wi-Fi name and password, press the Ctrl + O keys to save the changes. Then, press Enter. After that, press the Ctrl + X keys to exit the nano editor.

  1. To reconfigure and connect the Raspberry Pi to the Wi-Fi network, enter the following command: sudo wpa_cli reconfigure. If you have connected successfully, you will see the type of the interface and an OK message:
  1. We will then need to restart the Raspberry Pi. To do this, type sudo shutdown now. Once the Raspberry Pi is shut down, close the PuTTY software.
  2. Next, unplug the USB cable from the laptop. 
  1. After that, unplug the Ethernet cable that is connected to the Raspberry Pi and the laptop.
  2. Re-connect the USB cable so that the Raspberry Pi turns on.
  3. Open PuTTY. Inside the Host Name field, enter raspberrypi.local again and press the Open button:
  1. Enter the username and password that we used previously.
  1. Once the Raspberry Pi is connected to the Wi-Fi network, the Wi-Fi network will assign it an IP address. To find the IP address, enter the ifconfig wlan0command and press Enter. You will notice that an IP address has now been assigned:

Note

In my case, the IP address is192.168.0.108. Please note down your IP address somewhere, as you will need to enter it when using the VNC Viewer software. 

Enabling the VNC server

To view the Raspberry Pi display, we will need to enable the VNC server from the Raspberry Pi configurations window:

  1. To open the configurations window, we need to type sudo raspi-config inside the PuTTY Terminal and press Enter. We can then open the Interfacing Options as shown here:
  1. We can then open the VNC options:
  1. To enable the VNC server, navigate to the Yes option and press Enter:
  1. After enabling the VNC server, press Ok:
  1. Press Finish to exit the Raspberry Pi configuration window:

After enabling the VNC server, we will open the VNC Viewer software so that we can see the Raspberry Pi display.

Viewing the Raspberry Pi output on the VNC Viewer

To view the Raspberry Pi output on the VNC viewer, following the instructions below:

  1. After opening the VNC Viewer software, enter the IP address of your Raspberry Pi inside the VNC Viewer and press Enter:
  1. You will get a pop-up message stating that VNC Viewer has no record of this VNC server. Press Continue:

 

  1. Enter the username as pi and the password as raspberry. Select the Remember passwordoption and press OK:

We should now be able to view the Raspberry Pi display output inside the VNC Viewer software:

Now that we have connected the Raspberry Pi to the laptop via Wi-Fi, there is no need to connect the Raspberry Pi to the laptop via a USB cable. Next time, we can simply power the Raspberry Pi using a power bank or mobile charger. When we select the IP address of our Raspberry Pi, we can view the Raspberry Pi display output using the VNC Viewer software. 

Note

As mentioned already, please make sure that both the Raspberry Pi and the laptop are connected to the same Wi-Fi network when using the laptop for remote desktop access.

Increase the VNC's screen resolution

After viewing the RPi's display output in the VNC Viewer, you will notice that the screen resolution of the VNC Viewer is small and it does not cover the entire screen. To increase the screen resolution, we need to edit the config.txt file:

  1. Enter the following command in the terminal window:
sudo nano /boot/config.txt
  1. Next, below the #hdmi_mode=1 code, enter the following three lines:
hdmi_ignore_edid=0xa5000080
hdmi_group=2
hdmi_mode=85
  1. After this, press Ctrl + O and then press Enter to save the file. Press Ctrl + X to exit:
  1. Next, reboot your RPi to apply these changes:
sudo reboot

Once rebooted, you will notice that the VNC's screen resolution has increased and it now covers the entire screen.

Handling VNC and PuTTY errors

In the VNC Viewer, sometimes, when you are selecting the IP address of the RPi, you may see the following pop-up error message instead of the RPi display:

You may also see the following message:

If you get either of these errors, click on the Wi-Fi icon on your laptop and make sure that you are connected to the same Wi-Fi network to which the RPi is connected. If this is the case, there is a chance that the IP address of your RPi has been changed inside the Wi-Fi network, which sometimes happens when a new device is connected to the Wi-Fi network.

To find the new IP address follow these steps:

  1. Open PuTTY and type raspberrypi.local inside the Hostname box. 
  2. Enter the command ifconfig wlan0 inside PuTTY's Terminal window. If your IP address has changed, you will notice the new IP address in theinetoption.
  3. Enter the new IP address inside VNC Viewer to view the RPi display output. 

Sometimes, you may be unable to connect to Putty as well, and you will see the following error:

To solve the preceding error in PuTTY,  follow these steps:

  1. Connect a LAN cable to the RPi and the laptop.
  2. Power on your RPi and try connecting to putty by entering raspberrypi.local in the hostname box. With the LAN cable connected to the RPi and the laptop, you should be able to access the PuTTY Terminal window.
  3. Follow the previous steps to find the RPi's new IP address.
  4. Once you see the RPi display inside VNC Viewer, you can remove the LAN cable. 
 

Setting up the Raspberry Pi Zero W as a desktop computer


As we've said, the Raspberry Pi Zero W is a stripped-down version of the Raspberry Pi 3B+. The Raspberry Pi Zero W has very limited connections, so in order to connect it to different peripherals, we will need to purchase some additional components. We will need the following hardware components: 

  • A keyboard
  • A mouse
  • A microSD card of minimum 8 GB (recommended 32 GB)
  • A microSD card reader
  • An HDMI cable
  • A display unit, preferably an LED screen or a TV with an HDMI port
  • A mobile charger or a power bank to power the Raspberry Pi

 

  • A micro USB B-to-USB connector (also known as an OTG connector), which looks like the following:
  • A mini HDMI-to-HDMI connector, as follows:
  • A USB hub, as pictured here:

Now that we know what hardware we require, let's set up our Raspberry Pi Zero W.

Setting up the Raspberry Pi Zero W

The steps to install the Raspbian OS onto a microSD card are exactly same as those listed already for the Raspberry Pi 3B+ in the section Installing Raspbian OS on an SD card. Once you have your SD card ready, follow these steps to set up the Raspberry Pi Zero W:

  1. First, insert the microSD card into the SD card slot of the Raspberry Pi Zero W.
  2. Connect one end of the miniHDMI to the HDMI connector (H2HC) inside the HDMI port of the Raspberry Pi Zero W, and the other end of H2HC connector to the HDMI cable.
  3. Connect the OTG connector to the Micro USB data port—not the power port—and connect the USB hub to the OTG connector.
  4. Connect the keyboard and the mouse to the USB hub.
  5. Connect a 5V mobile charger or a battery bank to the power unit's Micro USB port.
  6. Next, connect an HDMI cable to the HDMI port of a TV or a monitor.
  7. Connect the mobile charger to your mains supply to power the Raspberry Pi Zero W. You will then see the green LED blink for a period of time as the Raspberry Pi Zero W turns on. 
  8. If you have connected the HDMI cable to your TV, select the correct HDMI input channel. The following annotated photo shows the connections mentioned here:

Raspberry Pi Zero W Connections

  1. It will take around two or three minutes for the Raspberry Pi Zero W to boot up. Once it's ready, you will see the following window on your TV or monitor screen:
  1. To shut down the Raspberry Pi, press the Raspberry Pi icon and click on Shutdown. 

Now that it's set up, let's connect our Raspberry Pi Zero W to a laptop.

 

Connecting the Raspberry Pi Zero W to a laptop via Wi-Fi


When the Raspberry Pi Zero first came out in 2015, it didn't have a built-in Wi-Fi module, which made it difficult to connect to the internet. Some Raspberry Pi developers came up with useful hacks to connect the Raspberry Pi to the internet and some companies also created Ethernet and Wi-Fi modules for the Raspberry Pi Zero.

In 2017, however, the Raspberry Pi Zero W was launched. This had a built-in Wi-Fi module, which meant that Raspberry Pi developers no longer needed to perform any DIY hacks or purchase a separate component to add internet connectivity. Having built-in Wi-Fi also helps us to wirelessly connect the Raspberry Pi Zero W to a laptop. Let's take a look at how this can be done.

The process of connecting Raspberry Pi Zero W to a laptop's Wi-Fi is similar to that of Raspberry Pi 3B+. Since the Raspberry Pi Zero W does not have an Ethernet port, however, we will have to write a few lines of code inside the cmdline.txt and config.txt files. 

Even though cmdline.txt and config.txt are text (TXT) files, the code in these files does not open properly inside Microsoft's Notepad software. To edit these files, we will need to use code editor software, such as Notepad++ (only available for Windows) or Brackets (available for Linux and macOS).

After installing either of these, let's customize the microSD card as follows:

  1. In the Raspberry Pi Zero W, we also need to create an SSH file on the microSD card. For instructions on how to create an SSH file on the microSD card, refer to the section, Creating an SSH file on a microSD card.
  2. After creating an SSH file, right-click on the config.txt file and open it in Notepad++ or Brackets. In this case, we will open it in Notepad++:

Scroll all the way down to the bottom of this code and add the line dtoverlay=dwc2 at the end. After adding the code,  save and close the file. 

  1. Next, open the cmdline.txt file inside Notepad++. The entire code inside the cmdline file will be displayed on one line. Next, make sure that you add only one space between the word consoles and the word modules. 

Enter the line modules-load=dwc2,g_ether at the end next to the plymouth.ignore-serial-consoles code:  

  1. Next, connect the Raspberry Pi Zero W to your laptop using a data transfer USB cable. Connect the USB cable to the data port of the Raspberry Pi Zero W, and not the power port:
  1. Make sure that the USB cable that you are connecting to the Raspberry Pi Zero W and the laptop supports data transfer. For example, take a look at the following photograph:

In the preceding photo, there are two similar, but importantly different, cables:

    • The small USB cable on the left was included with my power bank kit. This USB cable supplies power but does not support data transfer.
    • The USB cables on the right were included with the purchase of a new Android smartphone. These do support data transfer.

A simple way to check whether your USB supports data transfer or not is to connect it to your smartphone and laptop. If your smartphone is detected, this means that your USB cable does support data transfer. If not, you will need to purchase a USB cable that supports data transfer. The following screenshot shows a smartphone being detected by a PC, meaning that the cable in use is a data cable:

Note

If your USB cable is detected but loses connection frequently, I recommend that you purchase a new USB cable. Sometimes, older USB cables do not work properly because of wear and tear. 

Connecting the Raspberry Pi Zero W to a Wi-Fi network using PuTTY

To connect the Raspberry Pi Zero W to a Wi-Fi network, refer to the Connecting the Raspberry Pi 3B+ to a Wi-Fi network using PuTTY section. The steps for connecting a Raspberry Pi Zero W to a Wi-Fi network are exactly the same.

Enabling VNC Viewer for Raspberry Pi Zero W

To enable a VNC Viewer for the Raspberry Pi Zero W, refer to the Enabling the VNC server section.

Viewing Raspberry Pi Zero W output on VNC Viewer

To view the Raspberry Pi Zero W output in the VNC Viewer, refer to the Viewing the Raspberry Pi output on VNC Viewer section.

 

Summary


In this chapter, we've looked at how to set up the Raspberry Pi 3B+ and the Raspberry Pi Zero W as a normal desktop computer. We also learned how to connect a Raspberry Pi to a laptop wirelessly via a Wi-Fi network. You can now control a Raspberry Pi remotely from your laptop without needing to connect the Raspberry Pi to a keyboard, mouse, and monitor.

In the next chapter, we will first look at some basic commands for maneuvering around the Raspberry Pi OS. We will install a C++ library called Wiring Pi onto our Raspberry Pi, and gain an understanding of the pin configurations for this library. Finally, we will write our first C++ program and upload it wirelessly to our Raspberry Pi.

 

Questions


  1. Which processor is present on the Raspberry Pi 3B+?
  1. How many GPIO pins are present on the Raspberry Pi 3B+?
  1. Which software are we using for viewing the Raspberry Pi display output on our laptop?
  1. What is the default username and password of a Raspberry Pi?
  1. What is the command for accessing the configurations inside the Raspberry Pi?

About the Author

  • Dinesh Tavasalkar

    Dinesh Tavasalkar is a trainer and online instructor from India. He has trained more than 8,000+ students on topics related to robotics, Internet of Things, Arduino, Raspberry Pi, Android app development, augmented reality, and virtual reality. Dinesh's online courses have been undertaken by 25,000+ people on Udemy from more than 150+ countries. Some of his popular courses on Udemy include Robotics for beginners, Smartphone Control Robot using Arduino and Android, Build Augmented Reality apps using Unity and Vuforia, and Build Virtual Reality games for Google Cardboard using Unity. He also runs a YouTube channel called EngineersDream, where he teaches Android application development.

    Browse publications by this author
Book Title
Unlock this full book with a FREE 10-day trial
Start Free Trial