Ask HN: Advice for creating a USB device linking 2 computers
I want to build a little device that connects two computers together via usb, and send keystrokes from one to the other.
(I would use it to use a laptop keyboard on a headless computers).
I am looking for an easy solution, it does not have to be the cheapest.
ChatGPT points me to Arduino, but as far as I can see, there's no arduino with 2 usb ports. It also points me to Raspery pi zero, but that's a computer, not a microcontroller, so not sure if it's suitable.
If anyone with experience can give me some pointers, it would be greatly appreciated!
25 comments
[ 3.3 ms ] story [ 46.8 ms ] threadChatGPT isn't wrong - this sounds like what you need: https://pypi.org/project/zero-hid/.
You also might want to use optical isolators between these microcontrollers, instead of wiring them directly. Just connecting grounds might be wrong, because different computers might have different grounds.
https://www.digikey.ca/en/products/detail/ftdi-future-techno...
It might be simpler to get 2 microcontrollers and establish a communication link between them. Something like Arduino (Nano or even smaller third party boards), or Teensy would be suitable for this kind of setup.
Using Zig to Unit-Test a C Application (2023) - https://mtlynch.io/notes/zig-unit-test-c/
On the hardware side, it probably has what you need (and more).
The reason it is not simple is USB is asymmetric. There’s one host and multiple clients. And computers are approximately always built as hosts.
But if you are going to add hardware, a pair of usb network adapters (wired (or wireless)) has the same architecture.
And you won’t have custom software below the application layer. Good luck.
If not: You need something that can act as a usb "device" (the "host" and "device" sides of a usb connection are very different). A search for "usb keyboard emulator" turns up a lot of projects in that area. I'm not sure you're going to get much simpler than a pi-zero or teensy, unless you can find someone selling a pre built device that meets your needs.
If you're lucky, your laptop has a usb port that can be configured to take the "device" role instead of the usual "host" role. In that case you could probably build a software-only solution using the Linux usb "gadget" framework to make that port act like a keyboard when attached to the target's usb port.
You're going to need software on the laptop to gather the input. That seems necessary. You're not going to invent a purely hardware based solution here.
Given that, I'd abandon your starting requirement of needing two usb device ports. That doesn't seem to help you out here, doesn't actually buy you much.
I'd look at the field of existing software out there that can ship input between systems. Use ethernet, wifi, or BT for connectivity if possible (perhaps via usb-ethernet adapters which are very cheap!), and if absolutely required you can build a little rpi-zero with a usb-gadget to act like a virtual keyboard. https://github.com/input-leap/input-leap https://github.com/feschber/lan-mouse https://github.com/lkundrak/btkbdd
After each newline, you are the "device" in the middle that unplugs the USB from the source computer and plugs it in to the destination computer.
Now we want to automate the middle, but without networking. How to do that?
See also Logitech Flow keyboard (and mouse). After each batch of commands, communicate via the shared clipboard.
2 arduinos with a serial link between them.
https://www.digikey.com/en/products/detail/texas-instruments...
Alternatively, the newer ESP32-S3 boards (<$6 each) have dual USB-C interfaces, so you could do it with only one (smaller) card instead of two. Development might take longer though.
https://www.amazon.com/dp/B0CN4789XC
If you want to allow a network connection between the two computers, you could load soft-KVM software on each, and go from there.
https://goinglinux.com/open-source-cross-platform-kvm-softwa...
You could do something similar with a couple of Raspberry Pi's or RPI nanos. It doesn't really matter as both can be configured as USB devices and to access their GPIO ports.
The trick is getting them to act as a USB device and then to have them send data over the GPIO ports.
Fortunately I don't think either of those should be super hard.
1) The two computers are on 2 different grounds, so I believe it could damage the computers. So I would like to isolate the ground somehow, I am not sure what the options are. I tried to look for opto-isolated options, but didn't really find something clear. Do you have any suggestion?
2) This specific chip is marked NRND (not recommended for new designs) on ftdi's website, So it feels slightly wrong to use it?
https://xairy.io/articles/thinkpad-xdci
> (I would use it to use a laptop keyboard on a headless computers).
Are you sure you need such a device? If this is to occasionally manage that headless system, wouldn’t a Remote Desktop connection work fine?
You’re mentioning a Mac Mini and a MacBook elsewhere, so https://support.apple.com/en-gb/guide/remote-desktop/apdf49e... might be sufficient for your needs.
What you want to do is make sure your client device is an HID device, so that it's recognized as a keyboard. I think something like this will do: https://makerspot.com/cp2110-usb-hid-to-uart-serial-adapter/
For the server side, you'll either want a USB to serial adapter with a microcontroller in between, or one of the RP2040 Raspberry pis as it can be both those at once. https://www.waveshare.com/rp2040-zero.htm
Actually you can probably do it with two RP2040s (or maybe just one) as their USB ports I believe can be configured for different roles.
Good luck, and share your results!