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:

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:

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.