🛠 Gadget USB : Ethernet sur le port OTG + relais Wi-Fi
Objectives
Use the micro-USB data port (OTG, BCM2835) in gadget mode: the Zero W enumerates as an Ethernet card on any PC while relaying Internet through its Wi-Fi — the "Linux USB stick", with no configuration required on the PC side.
Hardware
- 1 Raspberry Pi Zero W (legacy boot: FAT partition with config.txt, no EEPROM)
- 1 micro-USB data cable to the PC
Steps
- Enable the controller: in
/boot/config.txtadddtoverlay=dwc2, andmodules=dwc2on thedwc_otg.lpm_enable=0 console=...line in/boot/cmdline.txt. - Create the Ethernet gadget via configfs/libcomposite (
modprobe libcomposite,usb0ing_etherRNDIS/CDC). - Link the two network interfaces: enable routing
sysctl -w net.ipv4.ip_forward=1and masquerade behindwlan0:iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE. - On the PC, the interface appears (usb0 / "Remote NDIS") and obtains an address via DHCP through dnsmasq on the board.
Verification
ip a shows usb0 UP; from the PC, ping to a public IP goes out through the Zero W's Wi-Fi.
Common Errors
- Plugging into the PWR port: power only, no USB signal.
- Confusing OTG device and host: the Zero W has no USB host port or hub — gadget mode is the only USB path.
- Forgetting NAT or ip_forward: the PC sees the link but cannot access the Internet.
Other tutorials for this board
- Premier démarrage headless de la Zero W (Wi-Fi + SSH)
- Suder le header 40 broches et faire clignoter une LED
- Se connecter au Wi-Fi en ligne de commande (nmcli)
- Pi-hole : bloqueur de pub DNS sur Zero W
- Balise BLE : diffuser un beacon avec le CYW43438
- Nœud capteur autonome sur batterie : énergie et fiabilité
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash