# Gadget Mode to setup a serial connection over USB
Prepare Raspberry Pi OS connection over a microUSB serial cable

Important:  choose either PiUART or OTG.  If you do both, it is possible that neither will work.  Also, you will want to disable the serial console if you are trying to use that serial port for communicating with other devices.  Important reading:  Raspberry PI UART configuration


USB-OTG method (Works on Zero, Zero W, and Compute Module)

Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget:

Look for the SD card reader on the VM under "Devices > USB" and click on it  Iff you get errors, you may have to shut down the VM and set the USB version to 2.0 or 3.0
The Boot and Root partition should mount in Linux under /media/$USER/boot and /media/$USER/rootfs
Navigate to /media/$USER/boot and edit:
config.txt: add "dtoverlay=dwc2" at the end
cmdline.txt:  remove "init=/usr/lib/raspi-config/init_resize.sh" and add "modules-load=dwc2,g_serial" to the end.  Make sure everything is on one line!
Eject the card
Boot the pi with a keyboard and monitor attached
Login
sudo systemctl enable serial-getty@ttyGS0.service
sudo systemctl start serial-getty@ttyGS0.service



# connecting to the serial terminal
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.  
Run "ls -l /dev/ttyU*" to check if port /dev/ttyUSB0 is found.  The Pi will most often be accessible over this port.
The user must be in the dialout group to access it.  Use the Linux command: id  -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 screen app 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
