3.2 KiB
Gadget Mode to setup a serial connection over USB
Prepare Raspberry Pi OS connection over a microUSB serial cable
Important: You will want to disable the serial console on RX/TX if you are trying to use that serial port for communicating with other devices.
Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget: The author discovered the guide was out of date, consulted the 2025 Raspberry Pi whitepaper on OTG, found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383
Make sure you have your USB cable plugged into the left port USB and not the right port marked PWR.
- Open a connection to the device via ssh or Raspberry Pi Connect
- Become root (the superuser)
sudo su - Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the
[all]category (which is at the end)echo "dtoverlay=dwc2" >> /boot/firmware/config.txt - We add the required module
dwc2for loading at startupecho "dwc2" >> /etc/modules - Check that we can load the libcomposite module
modprobe libcomposite - Put a copy of this repository in root's home directory using git
apt install -y git git clone https://gitea.cs.ru.is/RU-V207/rover-pi.git - Go to Scripts and test if the
gadget.shruns without errors and hopefully a Serial port appers in Device Manager or equivalentcd rover-pi/Scripts/ ./gadget.sh - Install the systemd script
cp serial-ether-gadget.service /lib/systemd/system/. - Enable the getty (which connects a login terminal to the serial port)
systemctl enable serial-getty@ttyGS0.service - Reboot and see if the serial port appears
Connecting to the serial terminal
You will need a serial terminal program (which used to be very very common) in order to communicate over your serial port
Windows
- Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
- MobaXterm: https://mobaxterm.mobatek.net/
Linux
If using a Linux VM, you will need to pass the Pi USB connection there. This is similar to the procedure for accessing an Arduino.
- Start the Linux VM if you haven't already
- Menu: Devices > USB
- Look for "Silicon Labs CP210x USB to UART Bridge" and click on it.
- check if port /dev/ttyUSB0 is found. The Pi will most often be accessible over this port.
ls -l /dev/ttyU* - The user must be in the dialout group to access it. Use the Linux command:
uid-to check if you are in the group. If not in the dialout group, use:sudo usermod -a -G dialout <username> - You will use the GNU
screenapplication in Linux to access the Pi. You can install it with:sudo apt install screen - You should now gain access to the Pi with:
` sudo screen /dev/ttyUSB0 115200 ```
- Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry