How to wire keyboard controller board (four usb wires 5V, d+, d-, gnd) onto Teensy for usb input?

Hello,
I am new to microcontroller programming/design and I am looking for a solution that will allow me to receive a usb keyboard's key input into a microcontroller which can also be setup to communicate with an esp32. \

So basically, the keyboard has wires that connect the controller board to the usb connector which goes into the computer. I want to cut the wires and solder them onto a microcontroller's pads to intercept the keyboard input. I guess it needs to be a microcontroller that has be a usb host and and device? And needs to have a usb library. I think the newer teensy controllers are able to do this? I need to confirm, and also understand where you would solder the wires coming out of the keyboard.

Again, I don't want to plug the keyboard's usb a cable into the microcontroller, I want to solder the wires onto the microcontroller, and then after I am done doing all the data processing on the teensy and esp32, I want to solder either of those microcontrollers to the remains of the keyboard wiring so that the original wire can still plug into the computer.

The important checkboxes are:

-microcontrollers are housed inside the keyboard case
-microcontroller(s) receive and interpret keyboard keystrokes
-original keyboard cable is wired back (after the microcontroller) and still plugs into pc.

So keyboard controller board >>> microcontrollers >>> keyboard wires >>>usb A plugs into PC tower.

Any direction is greatly appreciated.
 
That sounds like you want the Teensy to eavesdrop (sniff) the keyboard's USB line without actually controlling it? That isn't going to work. You would have to do this:
Keyboard -> Teensy -> PC

and have the Teensy act as a man-in-the-middle, receiving all the data from the keyboard and retransmitting it to the PC (possibly with modifications).
 
That sounds like you want the Teensy to eavesdrop (sniff) the keyboard's USB line without actually controlling it? That isn't going to work. You would have to do this:
Keyboard -> Teensy -> PC

and have the Teensy act as a man-in-the-middle, receiving all the data from the keyboard and retransmitting it to the PC (possibly with modifications).

Hey so I dont understand how that is different than what I proposed. What exactly wouldn't work with what I mentioned? Keyboard -> teensy -> PC sounds like what I want to do.

The teensy will sniff the keyboard usb line and send it to the PC without modifying it. The wifi microcontroller will get the usb data from the teensy and transmit the data over wifi. I suppose it would make more sense to just use one microcontroller that has wifi and usb host capabilities.

So lets say I find one, I think this microcontroller can usb-host and has wifi: https://store-usa.arduino.cc/products/arduino-mkr-wifi-1010

Can't I solder the keyboard usb wires onto the mkr 1010 and then also solder the output lines or whatever back into the keyboard wiring which plugs into the pc?

I know something like that has to be possible because thats basically what this guy is doing, just looks like he is using usb breakout boards instead of soldering the wires directly:

 
Hey so I dont understand how that is different than what I proposed.

Both messages use relatively few words rather than a clear diagram or schematic, so I might be misunderstanding, but...

Seems like you proposed having the PC continue to communicate directly with the USB keyboard and have Teensy passively monitor the communication. If that is your goal, please allow me to save you a lot of time and frustration by saying as clearly as possible, it definitely will not work with the USB port on Teensy or pretty much any other microcontroller.

I believe the other proposal "Keyboard -> Teensy -> PC" means you would use Teensy 4.1 which has 2 different USB port. You would connect the USB keyboard to Teensy 4.1 USB host port (5 pins meant to work with this cable), and connect the PC to Teensy 4.1 USB device port (the Micro-B connector). Your PC would not see the original keyboard, but rather Teensy acting as a USB keyboard. Code on Teensy would receive the keystrokes from your USB keyboard and then transmit the same on to your PC. This definitely can work. Several people have build projects this way. Just recently we published a blog article about a project with modifies USB mouse data using this sort of connection.

What exactly wouldn't work with what I mentioned? Keyboard -> teensy -> PC sounds like what I want to do.

To understand the reason why, you will need to learn more about how USB works. Here is a copy of the USB 2.0 spec. You can also get it from usb.org, but this direct link can save a little time.


This document is huge, but the really important part to read is chapter 4 which is only 10 pages and pretty easy to read. If you have more time, skimming the first part of chapter 5 can also really give you much more. As explained in that info, USB has 3 roles (host, device, hub). The USB hardware inside Teensy and virtually all other microcontrollers is designed to perform the device role or the host role.

The reason boils do to quite a lot of the USB protocol is implemented directly in the hardware. You don't get raw access to underlying data. The host hardware automatically transmits tokens based on the devices you've told it to use. The device hardware automatically receives those tokens, and if you've set up data buffers the hardware automatically moves data between the buffers and tokens it sends in reply to the host's tokens.

You simply do not get direct raw access to the tokens and all bits and bytes (read chapter 8 for a deep dive into gory details). The hardware simply is not built to passively monitor. The hardware inside Teensy and other microcontrollers is built to actively participate. In device mode, it listens only for the tokens the host sends. The hardware doesn't have the ability to capture the tokens another device sent in reply. The hardware simply can't do it.

What the hardware can do, on Teensy 4.1, is implement 2 USB ports where one port has the host role and the other has the device role. This is why those projects like Mouse Tailor can work. The original mouse acts as a device which is able to communicate with the Teensy host port. In turn, Teensy's device port is able to communicate with your PC, which of course acts in the host role.

Passive monitoring is possible, using special hardware. Usually these product are called "protocol analyzers". The one I own and use for developing Teensy's software is Total Phase Beagle USB 480. But it doesn't work stand-alone. It transmits everything to a PC where special software shows the communication. Total Phase has been in business a long time and their software is very well refined.

If your goal is to passively monitor with a small device (hopefully for ethical purpose) you might be interested in Cynthion. Sadly, it is still in development and already years late, but perhaps they'll actually start shipping in 2024?
 
Last edited:
Thank you for the very helpful reply! So I wanted to ask, in the video I posted above, isn't the arduino acting as both a usb host and a usb device?

I am sorry if my lack of experience implied something unintended, but I am just looking for a hardware setup that can be used to make a keyboard function whilst also relaying the keystroke data over wifi. I am looking for any way to achieve that, not suggesting any specific type of listening, etc. Just want to know how to achieve that goal with hardware small enough to fit inside the keyboard casing.
 
If you look at the T4.1 card, it is pretty easy to hook up both USB Host and USB Device.

1709214979253.png

The Device pins duplicate what is in the USB connector that you connect up to your PC.

So yes you can connect up the keyboard to the teensy using the host pins, and you can connect wires up to the Teensy to plug it into a PC.
You can similarly use a T4 or Teensy Micromod, but the host pins for T4 are not as easy.
1709215149608.png

The D+ and D- are the host pins, you would need to provide power to the keyboard, probably direct from the VUSB (or VIN) pin of the Teensy

Micromod needs some adapter board, which might have all of those broken out...

As Paul mentioned, the boards are not a passive listener. The USBHost_t36 library does have support for keyboards. There are a few example sketches, included with the library. One of interest: KeyboardForward, does listen to the keyboard attached to it and tries to forward most of the keystrokes to the PC.

This sketch needs you to configure the Arduino USB Type - to be one that includes Keyboard:
1709215619897.png


Such as Keyboard, which will actually define 2 interfaces (keyboard and a Serial Emumlation)

The teensy will then show up on your PC as to be a keyboard. It will have the Product ID and Vendor ID of a Teensy.

There are also limitations with this forwarding. The USB Host code understands, normal keystrokes, it also has some
support for some other standardized keys, such as multimedia and the like.

It does not have any support for vendor specific capabilities. That is some keyboards, might have other keyes which are handled by a different vendor specific interface, that requires special software on the PC to understand. We will not see those and as such not forward.

Likewise, some keyboards have builtin USB hubs, which assuming you add hub objects to your sketch, we can handle, but again we don't automatically connect to any other devices plugged into the hub, unless the sketch added objects to handle the specific devices or devices plugged into it.
...
 
@KurtE thank you, like I said I am new to microcontroller programming but based on what I read online I had the impression that the teensy could be a usb host and device. I also understand that software library is needed to make use of the data and that it is not simply received passively.

The next big question is whether an esp device like esp8266 can relay the data it receives via wifi TO the teensy board as well. See this video where the esp8266 listens to an iphone for keystrokes and sends them to the PC. My question is whether the teensy can receive the physical keyboard input and the wifi keystroke data from the esp, send both to the pc. This way keystrokes are sent to the PC if either the physical keyboard or the iphone on-screen keyboard are pressed?

I am not asking about the software details at this point, other than whether it can be done? Thanks so much for your help so far.
 
Yes, it's probably possibly. You would most likely program the ESP to send the data over WiFi to Teensy on a serial port pin, which could connect to any of the 8 serial ports on Teensy 4.1.
 
@Paul thanks thats what a intuited based on what ive read online. Really new to all this stuff but I am trying to focus on learning only what I need for this project and so just want to identify potential hardware to start learning with/testing designs.
 
I don't know a lot about esp32, but a first important question is whether it has 2 USB ports like Teensy 4.1. If the port is host mode, it can talk to the USB keyboard but not to your PC. You need a 2nd USB port in device mode to do that.
 
@Paul @KurtE so when I google "esp32 usb device mode" it seems like the esp32 supports device mode. The hackaday link i posted above shows how usb host capabilities are added to the esp32 manually. So i get the impression that both could exist simultaneously on the esp32 but what do you two think? Definitely we are discussing the feasibility of adding a port, not whether the esp32 natively comes with both host and device capabilities as is out of the box
 
You will find that the ESP32 devices supporting two USB ports as you require are usually built like shown in the pic below:-
see this.
zz.png
 
Back
Top