Introduction
In this guide, we'll configure a Raspberry Pi 4 to be powered and accessed directly through a single USB-C cable connected to an iPad or laptop. This setup creates a portable, headless Raspberry Pi that requires no separate power adapter, HDMI display, keyboard, or Ethernet cable. Perfect for mobile development, testing, or working with your Pi on the go.
Objective
Our goal is to create a fully functional Raspberry Pi 4 setup that can be accessed via a single USB-C connection. We'll accomplish this by:
- Using Raspberry Pi Imager to customize Pi OS Lite (64-bit) with SSH and WiFi credentials
- Burning the configured image to an SD card
- SSH into the Pi via WiFi for initial configuration
- Updating boot configuration files to enable USB gadget mode
- Creating a new network connection using
nmcliand activating it on boot - Connecting the Pi to an iPad or laptop using only a USB-C cable
Hardware Requirements
To complete this setup, you'll need:
- iPad or laptop with USB-C port capable of providing power
- Raspberry Pi 4 (any RAM variant)
- MicroSD card (16GB or larger recommended)
- USB-C cable that supports both power and data delivery
- Computer (Windows, Mac, or Linux) to burn the initial image
Detailed Setup Instructions
Step 1: Burn the Image
First, we'll prepare the SD card with Raspberry Pi OS and pre-configure essential settings.
- Download and install Raspberry Pi Imager on your computer
- Launch Raspberry Pi Imager
- Click "Choose Device" and select Raspberry Pi 4
- Click "Choose OS" and select Raspberry Pi OS (other) → Raspberry Pi OS Lite (64-bit)
- Click "Choose Storage" and select your SD card
- Click the gear icon (⚙️) or "Next" to access OS customization settings
- Configure the following settings:
- Hostname: Set a memorable hostname (e.g.,
rasp.local) - Username and password: Create your credentials
- WiFi: Enter your WiFi network name (SSID) and password
- Locale settings: Set your country and timezone
- Enable SSH: Check "Enable SSH" and select "Use password authentication"
- Hostname: Set a memorable hostname (e.g.,
- Click "Save" and then "Write" to burn the image to the SD card
- Once complete, insert the SD card into your Raspberry Pi 4 and power it on
Wait a few minutes for the Pi to boot and connect to your WiFi network.
Step 2: Configure the Pi to Use USB-C Port as a Network Interface
Now we'll configure the Raspberry Pi to act as a USB Ethernet gadget, allowing network communication through the USB-C port.
2.1 Connect via SSH
From your computer (connected to the same WiFi network), SSH into your Pi:
ssh username@rasp.local
Replace username with the username you configured earlier. This assumes the following:
- You setup the hostname of your Pi as rasp.local when creating an image
- The laptop and Pi are connected to the same wireless network
2.2 Become Superuser
Switch to root user for the following configuration steps:
sudo su
2.3 Edit cmdline.txt
Open the boot command line configuration file:
nano /boot/firmware/cmdline.txt
Add the following options after rootwait (keep everything on a single line):
modules-load=dwc2,g_ether
The complete line should look similar to this:
console=serial0,115200 console=tty1 root=PARTUUID=c8575274-02 rootfstype=ext4 fsck.repair=yes rootwait modules-load=dwc2,g_ether cfg80211.ieee80211_regdom=US
Save and exit (Ctrl+X, then Y, then Enter).
2.4 Edit config.txt
Open the boot configuration file:
nano /boot/firmware/config.txt
Find the [cm4] section and ensure that otg_mode=1 is uncommented. Then scroll down to the [all] section and add the following line after it:
dtoverlay=dwc2
The final configuration file should look similar to this:
# For more options and information see
# http://rptl.io/configtxt
# Some settings may impact device functionality. See link above for details
# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
# Additional overlays and parameters are documented
# /boot/firmware/overlays/README
# Automatically load overlays for detected cameras
camera_auto_detect=1
# Automatically load overlays for detected DSI displays
display_auto_detect=1
# Automatically load initramfs files, if found
auto_initramfs=1
# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=2
# Don't have the firmware create an initial video= setting in cmdline.txt.
# Use the kernel's default instead.
disable_fw_kms_setup=1
# Run in 64-bit mode
arm_64bit=1
# Disable compensation for displays with overscan
disable_overscan=1
# Run as fast as firmware / board allows
arm_boost=1
[cm4]
# Enable host mode on the 2711 built-in XHCI USB controller.
# This line should be removed if the legacy DWC2 controller is required
# (e.g. for USB device mode) or if USB support is not required.
otg_mode=1
[cm5]
dtoverlay=dwc2,dr_mode=host
[all]
dtoverlay=dwc2
Save and exit (Ctrl+X, then Y, then Enter).
Step 3: Create USB Network Connection and Activate on Boot
Now we'll create a persistent network connection for the USB interface that will automatically activate at boot.
3.1 Create USB Connection
Create a new Ethernet connection for the USB interface:
nmcli con add type ethernet con-name ethernet-usb0
3.2 Configure Connection Settings
Edit the connection configuration file:
nano /etc/NetworkManager/system-connections/ethernet-usb0.nmconnection
Update the file to match the following configuration:
[connection]
id=ethernet-usb0
uuid=Will be specific to your env
type=ethernet
autoconnect=true
interface-name=usb0
[ethernet]
[ipv4]
method=shared
[ipv6]
addr-gen-mode=default
method=auto
[proxy]
The uuid field will already be populated with a unique identifier. The key settings here are:
autoconnect=true- Connection starts automatically at bootinterface-name=usb0- Binds to the USB gadget interfacemethod=shared- Enables internet connection sharing
Important Note on Connection Sharing: Setting method=shared makes the Raspberry Pi automatically run a DHCP server that assigns an IP address to your iPad or laptop. The Pi will then NAT (Network Address Translation) all traffic from your device through to the internet via its WiFi connection. This means your iPad can access the internet through the Pi.
If you don't want internet sharing and only need direct device-to-device communication, change the method to link-local:
[ipv4]
method=link-local
With link-local, both devices will self-assign IP addresses in the 169.254.x.x range without running a DHCP server, and no internet traffic will be routed through the Pi.
Save and exit.
3.3 Create USB Gadget Activation Script
Create a script that will bring up the USB connection:
nano /usr/local/sbin/usb-gadget.sh
Paste the following content:
#!/bin/bash
nmcli con up ethernet-usb0
Save and exit, then make the script executable:
chmod a+rx /usr/local/sbin/usb-gadget.sh
3.4 Create Systemd Service
Create a systemd service that will run the script at boot:
nano /lib/systemd/system/usbgadget.service Paste the following configuration:
[Unit]
Description=My USB gadget
After=NetworkManager.service
Wants=NetworkManager.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/usb-gadget.sh
[Install]
WantedBy=sysinit.target
Save and exit.
3.5 Enable Service and Reboot
Enable the service to run at boot and restart the Pi:
systemctl enable usbgadget.service
reboot
Step 4: Connect the Pi to the iPad/Laptop via USB-C
Connect your Raspberry Pi 4's USB-C port to your iPad or laptop using a USB-C cable and wait 30-60 seconds for the connection to establish:
- The USB-C cable will both power the Pi and create the network connection
- On your iPad, navigate to Settings → Ethernet
- You should see a new Ethernet connection appear automatically
- Your iPad will receive an IP address on the 10.x.x.x network from the Pi's DHCP server
- Your iPad will now have internet access through the Raspberry Pi's WiFi connection
- Install a terminal application like Terminus or Blink Shell on your iPad
- Connect to your Pi using its hostname:
ssh username@rasp.local
The Raspberry Pi will have the gateway IP address (typically 10.42.0.1) on the usb0 interface, and your iPad will be assigned an address like 10.42.0.x.
Conclusion
Congratulations! You've successfully configured your Raspberry Pi 4 to work seamlessly with just a single USB-C cable. This setup provides several advantages: your iPad or laptop powers the Pi while simultaneously providing network connectivity, eliminating cable clutter and making your Pi truly portable. Whether you're developing on the go, demonstrating projects, or simply prefer a minimalist setup, this configuration offers convenience without sacrificing functionality.
The USB gadget mode essentially turns your Raspberry Pi into a plug-and-play device that creates its own network connection whenever you connect it via USB-C. You can still access WiFi for internet connectivity while using the USB connection for direct device-to-device communication. This makes it perfect for scenarios where you need reliable, low-latency access to your Pi without depending on WiFi network availability.
Feel free to expand on this setup by installing additional software, setting up development environments, or creating automated scripts. Your portable Raspberry Pi workstation is now ready for whatever projects you have in mind. Happy tinkering!
Comments
Post a Comment