3

I'm trying to figure out how "plug the GPIO pins into a computer's USB" to receive data from said computer. The results from my searches have been either too confusing or not helpful. Please be gentle, I have no knowledge of low level USB and serial. I may be good with computers, but I've never tried messing with this level of stuff. I have a pi zero.

  • You can do one of two things: 1) Make the Pi Zero (only works on the Zero) act as a USB gadget, like a serial device, and connect to it from a computer. If you do this, you can't connect anything to the Pi's USB port. 2) Use a USB to serial converter to connect the Pi to a PC and get a login shell (works on all Pis). They have similar results, but they work completely differently. – anonymoose Jan 16 '18 at 21:37
  • I'm liking the sound of option 1. Can you expand as an answer? – Timberwolf Jan 16 '18 at 21:39
  • You can do it cutting a USB 1.0 cable. How? I don't know. The max transfer speed is 12mbps, while the pi's 14. – Garmekain Jul 7 '18 at 0:45
4

You can make the Pi act as a USB gadget and impersonate a USB device.

Adafruit has a tutorial on how to do that -- or you can just keep reading for a summary.

Plug the SD card into another computer. Depending on the OS, you may see one or two new storage volumes. (If you see more, that's because you're using NOOBS. If you only have one OS installed, the rest of the tutorial will work.)

Find the volume with the files config.txt and cmdline.txt in it. Open config.txt a text editor. If you're on Windows, open it in WordPad or Notepad, not Word.

Scroll down to the bottom and add dtoverlay=dwc2 on a new line. Now save and close.


Here is where you have to decide if you want the Zero to act as a serial gadget or an ethernet gadget. If you choose a serial gadget, it will simply give you a login shell and you can go from there. However, you'll need to be to access a shell on the Pi some other way.

If you choose an ethernet gadget, it will show up as a network interface. This gives you a lot more flexibility because you can do things like VNC in -- which will give you a graphical desktop -- or you can share your computer's network connection with the Pi (Adafruit's tutorial talks about how to do that under "Ethernet Tweaks.")

If you choose an ethernet gadget, enter g_ether instead of g_serial in the next paragraph. You'll also want to create a file called ssh (no file extension) on the SD card to enable SSH. You don't have to do that now, but it's convenient.


Next, open cmdline.txt. This file is one long line, so don't add any newlines. Go to the end of that line and add a space followed by modules-load=dwc2,g_serial. Now eject the card and plug it into the Pi, but leave it off.

If you decided to go the serial route, you'll need to boot the Pi, log in by some other method, and run sudo systemctl enable getty@ttyGS0.service to enable using the connection as a serial console, followed by sudo shutdown -h now to power it off.

Now, use a micro-USB to USB cable to connect it to your computer (make sure to use the port labeled "USB"). The Adafruit tutorial goes into more detail on how to connect to your Pi from there.

  • This will work perfectly. – Timberwolf Jan 16 '18 at 21:57
  • Oops, I forgot one important detail about the serial connection. I'll update. – anonymoose Jan 16 '18 at 21:59
4

You can not implement a USB bus using the Pi GPIO. The transfer speeds are far too high, you simply wouldn't be able to talk the USB protocol in software without hardware support.

Why not investigate using one of the Pi's USB ports rather than the GPIO?

  • I could do that. I figured the GPIO would work fine, but if it won't work then it won't. Heck I don't even need USB. As long as I have a physical, non-network connection between the pi zero and my PC that the PC can send data to the pi, I don't care. – Timberwolf Jan 16 '18 at 21:55
  • 5
    A serial link would be a fairly typical solution. Either using a USB dongle at the Pi end or using Pi GPIO14/15 (pins 8/10) as long as the other end was a Pi friendly 3V3 TTL. – joan Jan 16 '18 at 22:08
  • @Timberwolf, modern PCs have no "non-network" connections anymore. Parallel Port is gone, even COM ports are no longer available in most cases. The only physical connection between PC and outside world is USB, and you need to work hard to get any proprietary connection, you have to use various converters (USB to UART, or to I2C) to get connected to DIY hardware. – Ale..chenski Feb 3 '18 at 6:24
  • This is actually not true for USB 1.0 – Garmekain Jul 7 '18 at 0:44
  • 1
    @Garmekain I don't think it is possible in software on the Pi, theoretically or practically, but I am guessing as I have not read the USB specs and do not know its real-time constraints. – joan Jul 7 '18 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.