Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Adafruit Logo
0

Turning your Raspberry Pi Zero into a USB Gadget

Image for user adafruit2
published December 24, 2015, last edited May 29, 2025
last major update April 22, 2024
Save Link Note Download
171
Beginner
Skill guide

Ethernet Gadget

The Ethernet Gadget is a little more difficult to set up, but is a lot more powerful because you can tunnel networking, VNC, ssh and scp files, etc. Basically you get the ability to log in to the console as well as anything else you could want to do over a network connection

Note that even though it's called "Ethernet Gadget" you do not use an Ethernet cable! The only cable is the USB micro-B cable from your computer to your Pi Zero. The Pi 'appears' like an Ethernet device.

You can even share your desktop computer's network setup so your Pi can access the internet through your computer via the USB cable! Cool huh?

Step 0. Download and install latest Raspberry Pi OS

We're using Bullseye Lite but plain Bullseye Raspberry Pi OS should work too!

This tutorial has the details

Step 1. Edit config.txt & cmdline.txt

After burning the SD card, do not eject it from your computer! Use a text editor to open up theconfig.txtfile that is in the SD card post-burn.

Go to the bottom and adddtoverlay=dwc2as the last line:

raspberry_pi_dwc2.png

Save the config.txt file as plain text and then open up cmdline.txt Afterrootwait (the last word on the first line) add a space and thenmodules-load=dwc2,g_ether

raspberry_pi_gether.png

Boot Your Pi with USB

Plug in a MicroUSB cable from your Pi Zero's USB port to your computer

Don't forget to plug in the USB cable from your computer to the "USB" connector port on the Pi Zero, not the PWR connector.

If you have aconsole cable (3.3 volt, not 5 volt), you can watch the Zero's console to see it enable theg_ether device:

USB to TTL Serial Cable With Type A plug and 4 wire sockets
The cable is easiest way ever to connect to your microcontroller/Raspberry Pi/WiFi router serial console port. Inside the big USB plug is a USB<->Serial conversion chip and at...
$9.95
In Stock
raspberry_pi_getherboot.png

SSH!

If you enable SSH on your Pi, you can then also SSH in toraspberrypi.local

Start by enabling SSH

If you are using a Mac or Linux chances are you have Bonjour already installed.On Windows, you may need to add Bonjour support so it knows what to do with .local names

raspberry_pi_pilocal.png
raspberry_pi_ssh.png

Advanced Networking (Fixed IP)

If you need to manage fixed IP addresses for some reason - here's some useful techniques for managing your Pi's Gadget Ethernet device. Otherwise, you can always just keep usingraspberrypi.local

You can now log in and check that you have a new network device calledusb0

sudo ifconfig -a
sudo ifconfig -a
raspberry_pi_getherifconfig.png

Try plugging the Pi Zero into your computer now. For example, on a Mac, we plugged it in

raspberry_pi_dhcpd.png

As you can see above, between the first ifconfig and second, the network came up with an address. The problem this is a arbitrary (Bonjour/Zero Conf assigned) address, and we dont want to have to guess it.

We can configure this device to have a fixed address (this makes it easier to find on a network!)

sudo nano /etc/network/interfaces
sudo nano /etc/network/interfaces

and add at the end

allow-hotplug usb0iface usb0 inet static        address 192.168.7.2        netmask 255.255.255.0        network 192.168.7.0        broadcast 192.168.7.255        gateway 192.168.7.1
allow-hotplug usb0iface usb0 inet static        address 192.168.7.2        netmask 255.255.255.0        network 192.168.7.0        broadcast 192.168.7.255        gateway 192.168.7.1

This will give theRaspberry Pi the IP Address 192.168.7.2

you can change this to a different address but unless you're sure that 192.168.7.* is unavailable, keep it as above for now.

raspberry_pi_interface.png

Save the file and run (the first command may fail, its fine)

sudo ifdown usb0 sudo ifup usb0ifconfig usb0
sudo ifdown usb0 sudo ifup usb0ifconfig usb0

to verify it now has the 192.168.7.2 address

raspberry_pi_ifdownup.png

Now on yourcomputer you'll need to set it up as well.

If you are using a Mac as the Host Computer

On a Mac OS X machine, open up theSystem Preferences -> Network box.

raspberry_pi_selfassigned.png

You'll see the device show up as an RNDIS/Ethernet Gadget. it'll probably be set up for DHCP by default so change it toConfigure IP4 Manually

  • For the IP address pick192.168.7.1(note that this is not the same as the Pi Zero's address!)
  • For the subnet mask, use255.255.255.0 (same setting as Pi)
  • For the router/gateway use192.168.7.1(same setting as Pi)

If you didn't use our suggested netconfig above on the Pi, you may have to adjust this one to match

raspberry_pi_networkconfig.png

ClickApply when done, and wait a minute or so you will get a green dot:

raspberry_pi_connected.png

If you're still having issues, a reader reported some Mac's need a special option on the g_ether device. While logged into your Pi with a console cable, run

sudo nano /etc/modprobe.d/g_ether.conf
sudo nano /etc/modprobe.d/g_ether.conf

and add:options g_ether use_eem=0

on it's own line, at the end.

After a reboot or manual load of the module, the the RNDIS/CNC gadget will turn yellow then green after assigning an IP.

You can use a terminal on the computer to check the IP address was set, your device will be calledenX where X is some number, useifconfig -ato see a list of all devices, chances are the Pi is the last one.

Once you can see that the IP address is set, try pinging the pi with

ping 192.168.7.2
ping 192.168.7.2
raspberry_pi_ifconfigping.png

To be honest, I rebooted the Pi after setting up the network config file, so if it doesnt work at first, try that.

Next up you can ssh into your pi from your Mac!

raspberry_pi_ssh.png

If you are using Windows as the Host Machine

Plug in the Pi Zero into your computer, I'm using Windows 7 64-bit. It will automatically download and install the RNDIS Ethernet drivers

raspberry_pi_usbrnd.png

Some versions of windows may mis-interpret the PI as a COM port and you must manually force or install Microsoft RNDIS driver usage in Device Manager by right-click>Update Driver Software>Browse my computer>Pick from a list>Network Adapters>Microsoft>Remote NDIS compatible device.

Check the Device Manager to check that it is a new network adapter

raspberry_pi_1rndisdriver.png

Open upNetwork and Sharing Center and click onChange Adapter Settings 

raspberry_pi_2networksharing.png

You'll see a list of all the myriad adapters you have. I have a lot but you'll likely only have 2 or 3. Find the RNDIS adapter and rename itpizero (makes it easier to find)

raspberry_pi_3rename.png

Then right-click and selectProperties...

raspberry_pi_4properties.png

And select theInternet Protocol Version 4 (TCP/IPv4) from the connection list and clickProperties

raspberry_pi_5tcp4.png

Enter in192.168.7.1 as the computer's IP address and gateway (the gateway got erased later, I think Windows just automatically uses the IP address if they're the same) the subnet mask is255.255.255.0 same as the Pi's

There's no DNS address

raspberry_pi_manual.png

I unplugged & replugged in the Pi Zero, Windows will then identify the network.

raspberry_pi_pizeroidentifying.png
raspberry_pi_unidentified.png

Now you can use a command box to runipconfig /all if you want to check out the stats on the connection

raspberry_pi_ipconfig.png

and pingthe pi with

ping 192.168.7.2
ping 192.168.7.2
raspberry_pi_ping.png

and evenssh!

raspberry_pi_winssh.png

Page last edited May 29, 2025

Text editor powered bytinymce.

Related Guides
Search

Search

Categories

[8]ページ先頭

©2009-2025 Movatter.jp