Built-in wireless on the Pi Zero W opens up a huge number of possibilities for the various USB gadget modes. The Pi Zero W can be configured to spoof different USB device types, such as a keyboard, a webcam, or a smart USB flash drive.

Pi Zero W内置无线功能为各种USB小工具模式提供了大量可能性。Pi Zero W可配置为欺骗不同的USB设备类型,例如键盘,网络摄像头或智能USB闪存驱动器。

At home, many people use a USB flash drive to transfer files to a TV, but it takes time to move the drive to and from the source computer. How about a remotely accessible USB flash drive, permanently located in the TV, where you could transfer files using the wireless connection? Drag and drop, job done!


STEP-01: The Pi Zero W USB flash drive

STEP-01:Pi Zero W USB闪存盘

On Raspbian Jessie, wireless connections can be made via the networking icon on the right-hand end of the desktop menu bar. Left-clicking the icon will bring up a list of available networks. If you see the scanning message, wait for a moment and it should find your network.

在Raspbian Jessie上,可以通过桌面菜单栏右侧的网络图标进行无线连接。左键单击该图标将显示可用网络列表。如果您看到扫描消息,请稍等片刻它应该找到您的网络。

Click the network that you want to join. If the network is secured, you will see a dialogue box. Enter the password, click OK, and wait for a few seconds. Finally, write down the IP address the Pi has acquired. Hover the mouse over the signal strength icon and the IP address will appear in a tooltip. If you prefer, you can complete this process on another Pi, and move the SD card over to the Pi Zero W when you reach Step 03.

单击要加入的网络。如果网络安全,您将看到一个对话框。输入密码,单击“确定”,然后等待几秒钟。最后,记下Pi获取的IP地址。将鼠标悬停在信号强度图标上,IP地址将显示在工具提示中。如果您愿意,可以在另一个Pi上完成此过程,并在到达步骤03时将SD卡移动到Pi Zero W.

STEP-02: Disable desktop and enable SSH


From this point we don’t need the desktop, because the Pi Zero is going to have one job: to be a file server for the TV. To save CPU cycles, we should disable the automatic boot to desktop. Go to Menu (Raspberry icon at the top-left corner of the screen) Preferences Raspberry Pi Configuration.

从这一点来说,我们不需要桌面,因为Pi Zero将有一个工作:成为电视的文件服务器。为了节省CPU周期,我们应该禁用自动启动到桌面。转到菜单(屏幕左上角的覆盆子图标)首选项Raspberry Pi配置。

On the System tab, find the Boot option. Select ‘To CLI’ (command line interface), and disable the ‘Login as user pi’ checkbox.
On the Interfaces tab, make sure that SSH is enabled. Click OK and choose Yes to reboot. After the reboot, you should see a text login prompt with a flashing cursor.

在“系统”选项卡上,找到“引导”选项。选择“To CLI”(命令行界面),然后禁用“以用户pi身份登录”复选框。

STEP-03: Switch to remote access


We recommend using SSH (Secure Shell), because we’ll be doing everything using the command line. SSH allows us to see the Raspberry Pi command prompt in a window on another PC, Mac, smartphone, or tablet.

我们建议使用SSH(Secure Shell),因为我们将使用命令行执行所有操作。SSH允许我们在另一台PC,Mac,智能手机或平板电脑的窗口中查看Raspberry Pi命令提示符。

Go to and follow the instructions for the platform you’re using. You’ll need the IP address you wrote down earlier, and the login. The default login for Raspbian is pi, with the password raspberry.


When the SSH link is working, you can disconnect any keyboard and screen.


STEP-04: Free up some disk space


When you’ve logged in over SSH, you can free up disk space by removing some programs that we’re not going to need for this project.
The command df -h shows your disk space usage. Look at the Avail column for /dev/root to see how much free space you have. We can claw back about 1GB if we remove LibreOffice and Wolfram.

命令df -h显示磁盘空间使用情况。查看/ dev / root的Avail列以查看您有多少可用空间。如果我们删除LibreOffice和Wolfram,我们可以回收大约1GB。

Enter the commands below into your SSH client:


sudo apt-get remove --purge libreoffice* -y

You can safely ignore any errors you see here. They happen because the command will try to remove some parts of LibreOffice that aren’t actually installed.


sudo apt-get purge wolfram-engine -y
sudo apt-get clean
sudo apt-get autoremove -y

Now run df -h again and check the Avail column.

现在再次运行df -h并检查Avail列。

STEP-05: Enable the USB driver


Next, we need to enable the USB driver which provides the gadget modes, by editing two configuration files.


sudo nano /boot/config.txt

Scroll to the bottom and append the line below:



Press CTRL+O followed by Enter to save, and then CTRL+X to quit.

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。

sudo nano /etc/modules

Append the line below, just after the i2c-dev line:



Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
Now shut down the Pi Zero W with the command:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。
现在使用以下命令关闭Pi Zero W:

sudo halt

STEP-06: Switch to TV power


On the Pi Zero W, you’ll see two micro USB ports. One is marked ‘USB’ and the other ‘PWR IN’. You can supply power through either port, but the USB port is for data as well. There are two options.

在Pi Zero W上,您将看到两个微型USB端口。一个标记为“USB”,另一个标记为“PWR IN”。您可以通过任一端口供电,但USB端口也可用于数据。有两种选择。

You could plug the TV into the Pi Zero W USB port, not the PWR IN port, using a standard micro USB cable. The cable will both supply power from the TV and make the USB data connection. The disadvantage is that the TV must be switched on to supply power to the Pi. When someone turns the TV off with the remote, the Pi will also lose power, which can corrupt your SD card.

您可以使用标准的micro USB线将电视插入Pi Zero W USB端口,而不是PWR IN端口。电缆既可以从电视供电,也可以进行USB数据连接。缺点是必须接通电视以向Pi供电。当有人用遥控器关闭电视时,Pi也会断电,这可能会损坏您的SD卡。

Alternatively, you can connect a separate, always-on power supply to the PWR IN port, and use a slightly modified micro USB cable to connect the TV to the USB port. The modification is to cut the red wire inside the micro USB cable. This protects the Pi from damage that could be caused by drawing power from two different power sources. The advantage of this method is that the Pi is powered independently from the TV. It will be available on the network even if the TV is off, and there is a reduced risk of sudden power loss and SD card corruption.

或者,您可以将单独的常开电源连接到PWR IN端口,并使用略微修改的micro USB线缆将电视连接到USB端口。修改是切断micro USB线内的红线。这可以保护Pi免受可能由两个不同电源汲取电力造成的损坏。这种方法的优点是Pi独立于电视供电。即使电视关闭,它也可以在网络上使用,并且可以降低突然断电和SD卡损坏的风险。

You might want to test the system with the first option, and then move onto the second when you want a more permanent setup. Don’t forget to cut the red wire if you use the second option.


Connect the Pi Zero W USB port to the TV using your chosen method, power everything up, and log back in over SSH.

使用您选择的方法将Pi Zero W USB端口连接到电视,为所有设备供电,然后通过SSH重新登录。

STEP-07: Create a container file


To enable mass storage device mode, we need to create a large file to act as the storage medium on the SD card. This file will emulate the USB flash drive that the TV sees.


The command below will create an empty 2GB binary file (change the count=2048 parameter if you want a different size). Please note that this will be limited by the available free space on your SD card (check the Avail column in df -h), and it may take a few minutes to complete the setup:

下面的命令将创建一个空的2GB二进制文件(如果您想要不同的大小,请更改count = 2048参数)。请注意,这将受到SD卡上可用空间的限制(请参阅df -h中的Avail列),完成设置可能需要几分钟时间:

sudo dd bs=1M if=/dev/zero of=/piusb.bin count=2048

We now need to format the file as a FAT32 file system so that the TV can understand it. Enter the command below:


sudo mkdosfs /piusb.bin -F 32 -I

STEP-08: Mount the container file


Now let’s mount the container file locally so we can download some test files. First, create a folder on which we can mount the file system:


sudo mkdir /mnt/usb_share

Now let’s add this to fstab, the configuration file that records our available disk partitions:


sudo nano /etc/fstab

Append the line below to the end of the file:


/piusb.bin /mnt/usb_share vfat users,umask=000 0 2

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
The line we added to fstab allows the USB file system to be error-checked and mounted automatically at boot time. Instead of rebooting, we can manually reload fstab with the command below:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。

sudo mount -a

STEP-09: Download a test file


Now let’s download a 300MB test file to view on the TV. Big Buck Bunny is a short open-source film, made by the Blender Foundation (, and released under the Creative Commons Attribution License 3.0:

现在让我们下载一个300MB的测试文件,以便在电视上观看。Big Buck Bunny是一部由Blender Foundation(制作的短片开源电影,并根据知识共享署名许可3.0发布:

cd /mnt/usb_share

You’ll see a progress bar move from left to right. When the download is complete, run a command to flush any cached data to the disk:



STEP-10: Test mass storage device mode


Now comes the moment of truth. Let’s see whether the TV is going to be friends with the Pi Zero W. The command below will enable USB mass storage device mode, and the TV should pop up a dialogue box. If it doesn’t, you may need to use the Input or Source button on the TV remote to select the USB device.

现在是关键时刻。让我们来看看电视是否会成为Pi Zero W的朋友。下面的命令将启用USB大容量存储设备模式,电视应弹出一个对话框。如果没有,您可能需要使用电视遥控器上的输入或源按钮来选择USB设备。

sudo modprobe g_mass_storage file=/piusb.bin stall=0 ro=1

The TV should provide a file browsing interface. Locate the Big Buck Bunny file and hit Play.

电视应提供文件浏览界面。找到Big Buck Bunny文件并点击Play。

Once you’re satisfied that all is well, try a dismount:


sudo modprobe -r g_mass_storage

The correct behaviour here is for the film or browsing interface to disappear from the screen. You may see a message saying that the USB device was disconnected.


STEP-11: Install and configure Samba


The next step is to provide network access to the /mnt/usb_share folder that we created earlier.

下一步是提供对我们之前创建的/ mnt / usb_share文件夹的网络访问。

sudo apt-get update
sudo apt-get install samba winbind -y

When the installation is complete, we need to configure a Samba network share. For simplicity, this will not require a user name or password, as it is already protected by your wireless network security. If you want more security, see


sudo nano /etc/samba/smb.conf

Scroll down to the end of the file and append the lines below:


browseable = yes
path = /mnt/usb_share
guest ok = yes
read only = no
create mask = 777

Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
Now restart the Samba service for the changes to take effect:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。

sudo systemctl restart smbd.service

STEP-12: Access the share from another computer


Now we can try to access the share from a Windows PC or a Mac. You’ll need the host name the Raspberry Pi is using. To check this, enter the command below:

现在我们可以尝试从Windows PC或Mac访问共享。您将需要Raspberry Pi正在使用的主机名。要检查此项,请输入以下命令:

cat /etc/hostname

By default this will be raspberrypi.


In Windows, you can bring up Explorer (Windows key + E) and type \\raspberrypi into the address bar at the top. The Run dialogue also works (Windows key + R).
On macOS, the Raspberry Pi will show up in the Finder sidebar. Alternatively, from the Finder menu, select Go Connect to server (Apple key + K) and type smb://raspberrypi as the server address.

在Windows中,您可以调出资源管理器(Windows键+ E)并在顶部的地址栏中键入\\ raspberrypi。运行对话框也有效(Windows键+ R)。
在macOS上,Raspberry Pi将显示在Finder侧栏中。或者,从Finder菜单中选择Go Connect to server(Apple key + K)并输入smb:// raspberrypi作为服务器地址。

Depending on your network settings, you may still see a login dialogue. Any user name, including a guest or anonymous login, will work. Once you’re in, you’ll see a share named usb. Open this and test that you have write access, either by creating a new folder or by copying over a file.


You can enable mass storage device mode to check that the TV can see your changes, but don’t forget to run the sync command first.


If you want to change the host name, edit the name in /etc/hostname. Make the same change in /etc/hosts, and finally use sudo reboot to apply the changes.

如果要更改主机名,请在/ etc / hostname中编辑名称。在/ etc / hosts中进行相同的更改,最后使用sudo reboot来应用更改。

STEP-13: Automate USB device reconnect


In order for the TV to detect any changes we’ve made over the network (for example, file or folder creations and deletions), it needs to be tricked into thinking that the USB device has been removed and reinserted.


We can use a Python library called watchdog (, which is designed for monitoring file system events and responding to them. Install this with the command below:


在lite版的镜像里,默认没有安装 python3所以也没有pip3。在执行pip3前可能需要安装一下:sudo apt-get install python3-pip

sudo pip3 install watchdog

We then need some code to start a timer whenever something changes in the shared folder. The timer is reset to zero every time a new change occurs, and the USB reconnect is only triggered if we see 30 seconds of inactivity after a change. This avoids spamming the TV while we’re copying over multiple files.


We’ve written a program to do this. To download it, type:


cd /usr/local/share
sudo wget -O
sudo chmod +x

STEP-14: Background service


We need to make this program into a background service, so that it starts automatically at boot time. We can do that by making it into a systemd unit. Enter the commands below:


cd /etc/systemd/system
sudo nano usbshare.service

This will start a new blank file. In the new file, enter:


Description=USB Share Watchdog



Press CTRL+O followed by Enter to save, and then CTRL+X to quit.
Now we need to register the service, enable it, and set it running:

按CTRL + O,然后按Enter保存,然后按CTRL + X退出。

sudo systemctl daemon-reload
sudo systemctl enable usbshare.service
sudo systemctl start usbshare.service

Whenever you copy files over to the network share, the USB device should automatically reconnect to the TV after 30 seconds of inactivity.