Author Topic: Controlling KVM with Raspberry Pi thru MCU?  (Read 722 times)

0 Members and 1 Guest are viewing this topic.

Offline LTNineTopic starter

  • Newbie
  • Posts: 5
  • Country: us
Controlling KVM with Raspberry Pi thru MCU?
« on: April 21, 2025, 05:37:18 pm »
I'm trying to control my KVM using a Raspberry Pi by sending HID keystrokes like ScrollLock + ScrollLock + <number> to switch ports.

The Pi runs Bitfocus Companion and a Stream Deck, which I want to use to trigger those key combos. Ideally, it could also send macros to the currently active computer.

HID support on the Pi (especially the 4B) is pretty rough. It supports OTG over USB-C, but that port is also used for power. The Pi Zero handles OTG better, but it's too underpowered. There aren't many libraries for this either — most solutions rely on shell scripts.

My workaround is to use GPIO 14/15 to send UART to a microcontroller that can act as a USB HID device. Coding this is easy enough, but the electrical part is giving me trouble.

I came across the Adafruit CH9328 UART to HID breakout, which seemed perfect — even has example wiring with GPIO 14/15.

For testing, I used a USB 3.3V UART adapter. It works if USB power is connected first, but if serial is already connected when USB is plugged in, the board fails to enumerate. I see unstable voltage around 3.1–3.5V instead of a clean 5V. That worries me, especially if a power outage causes a weird boot sequence with the board always connected inside the Pi enclosure.

I also have a Seeed XIAO SAMD21 and an Adafruit ItsyBitsy M0 Express. The ItsyBitsy has a VBAT pin that can take 3.7–6V, and it should switch between VBAT and USB automatically. I thought I could power it from the Pi’s 5V rail, but someone pointed out that it might not handle USB D+/D- properly if the cable isn’t unplugged.

For context, I’m a beginner with electronics — decent with a soldering iron, but not great at reading schematics.

I’m open to using other microcontrollers too. I looked at the Waveshare RP2040-ETH to do this over Ethernet, but if I can get UART working reliably, I’d prefer that route.

Does anyone have advice on how to handle the power/backfeed issue or suggestions for a better approach?
 

Offline phil from seattle

  • Super Contributor
  • ***
  • Posts: 1149
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #1 on: April 21, 2025, 06:12:28 pm »
My first thought is to use a RasPi Pico. Cheap, lots of examples out there.
 

Offline LTNineTopic starter

  • Newbie
  • Posts: 5
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #2 on: April 21, 2025, 07:34:40 pm »
Can the Pico tolerate voltage on RX/GND when unpowered? I just tested the XAIO SAMD21 MCU and I observed similar behavior. The MCU is put into an error state and stays there even after restoring 5v power over VBUS. It recovers if I unplug RX/GND and continues to work after I restore RX/GND. Same behavior as the CH9328.

So far my options seem to be:

- ItsyBitsy VBAT pin, providing alt power from Pi 4B's 5v pin so the MCU never loses power (may not be USB complaint, I need to probe D+/D- in this state)
- Find a way to keep power from back-feeding from RX.
- Just use Ethernet and don't connect Pi/MCU (would prefer not to have the network as a middleman if I can help it).

Unless the Pico has a way to prevent the back-feeding, I'm not sure that would work either. If USB power is constant and serial is plugged in after, it works in the above cases. It only fails when the serial device is powered and USB power is restored after.
 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 3521
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #3 on: April 21, 2025, 08:03:39 pm »
Quote
For testing, I used a USB 3.3V UART adapter. It works if USB power is connected first, but if serial is already connected when USB is plugged in, the board fails to enumerate. I see unstable voltage around 3.1–3.5V instead of a clean 5V. That worries me, especially if a power outage causes a weird boot sequence with the board always connected inside the Pi enclosure.

What connections are you using between the USB serial adapter and the CH9328? Are you providing power to the CH9328 from the serial adapter?

Apparently only two connections to the CH9328 are required -- RX and GND with power to the CH9328 being drawn from its USB port.

 

Offline fchk

  • Frequent Contributor
  • **
  • Posts: 324
  • Country: de
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #4 on: April 21, 2025, 08:08:08 pm »

For testing, I used a USB 3.3V UART adapter. It works if USB power is connected first, but if serial is already connected when USB is plugged in, the board fails to enumerate. I see unstable voltage around 3.1–3.5V instead of a clean 5V. That worries me, especially if a power outage causes a weird boot sequence with the board always connected inside the Pi enclosure.

The solution is to use a dual-supply level shifter. Example: SN74LVC4T774
https://www.ti.com/lit/ds/symlink/sn74axc4t774.pdf
The feature you need is "Vcc isolation" "If either VCC input is below 100 mV, all I/Os outputs are disabled and become high-impedance" and "Ioff supports partial-power-down mode operation".

Each side has its own VCC, but both sides share a common ground. The level shifter prevents backfeeding.

You could also use a digital isolator: ADuM1201
https://www.analog.com/media/en/technical-documentation/data-sheets/adum1200_1201.pdf
There each side has its own VCC and its own GND. Both sides are electrically isolated and only magnetically or capacitive coupled.
 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 3521
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #5 on: April 21, 2025, 08:28:22 pm »
From this page:

https://learn.adafruit.com/adafruit-ch9328-uart-to-hid-keyboard-breakout/circuitpython-and-python

it is clear you don't need to power the CH9328 from the UART side.

In fact, I believe the "Vcc" pad on the CH9328 is so that a MCU on the UART side can draw power from the USB port through the CH9328 module.
 

Offline LTNineTopic starter

  • Newbie
  • Posts: 5
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #6 on: April 21, 2025, 08:31:07 pm »
Quote
For testing, I used a USB 3.3V UART adapter. It works if USB power is connected first, but if serial is already connected when USB is plugged in, the board fails to enumerate. I see unstable voltage around 3.1–3.5V instead of a clean 5V. That worries me, especially if a power outage causes a weird boot sequence with the board always connected inside the Pi enclosure.

What connections are you using between the USB serial adapter and the CH9328? Are you providing power to the CH9328 from the serial adapter?

Apparently only two connections to the CH9328 are required -- RX and GND with power to the CH9328 being drawn from its USB port.

Just RX and GND. The issue is the order the connections are established. USB first, serial second, it works. Serial first, USB second, it fails.


For testing, I used a USB 3.3V UART adapter. It works if USB power is connected first, but if serial is already connected when USB is plugged in, the board fails to enumerate. I see unstable voltage around 3.1–3.5V instead of a clean 5V. That worries me, especially if a power outage causes a weird boot sequence with the board always connected inside the Pi enclosure.

The solution is to use a dual-supply level shifter. Example: SN74LVC4T774
https://www.ti.com/lit/ds/symlink/sn74axc4t774.pdf
The feature you need is "Vcc isolation" "If either VCC input is below 100 mV, all I/Os outputs are disabled and become high-impedance" and "Ioff supports partial-power-down mode operation".

Each side has its own VCC, but both sides share a common ground. The level shifter prevents backfeeding.

You could also use a digital isolator: ADuM1201
https://www.analog.com/media/en/technical-documentation/data-sheets/adum1200_1201.pdf
There each side has its own VCC and its own GND. Both sides are electrically isolated and only magnetically or capacitive coupled.

Does this mean I would provide VCC power from UART? Could the MCU be powered from the serial VCC, or does that just serve as a signal? Sorry for the dumb question, this is new to me :)

From this page:

https://learn.adafruit.com/adafruit-ch9328-uart-to-hid-keyboard-breakout/circuitpython-and-python

it is clear you don't need to power the CH9328 from the UART side.

In fact, I believe the "Vcc" pad on the CH9328 is so that a MCU on the UART side can draw power from the USB port through the CH9328 module.


I'm not, just the RX/GND are connected as the picture shows. The issue is TX->RX is sending 3.5v which is keeping the chip partially powered, so when 5v comes in from USB, it pulls the voltage down and it remains in a browned out state. It's an ordering issue - you have to plug the USB power in first, then serial for it to function properly. If you do the reverse it fails. Wiring is the same - the behavior differs depending on what voltage is applied first.
« Last Edit: April 21, 2025, 08:33:03 pm by LTNine »
 

Offline LTNineTopic starter

  • Newbie
  • Posts: 5
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #7 on: April 21, 2025, 08:59:49 pm »

For testing, I used a USB 3.3V UART adapter. It works if USB power is connected first, but if serial is already connected when USB is plugged in, the board fails to enumerate. I see unstable voltage around 3.1–3.5V instead of a clean 5V. That worries me, especially if a power outage causes a weird boot sequence with the board always connected inside the Pi enclosure.

The solution is to use a dual-supply level shifter. Example: SN74LVC4T774
https://www.ti.com/lit/ds/symlink/sn74axc4t774.pdf
The feature you need is "Vcc isolation" "If either VCC input is below 100 mV, all I/Os outputs are disabled and become high-impedance" and "Ioff supports partial-power-down mode operation".

Each side has its own VCC, but both sides share a common ground. The level shifter prevents backfeeding.

You could also use a digital isolator: ADuM1201
https://www.analog.com/media/en/technical-documentation/data-sheets/adum1200_1201.pdf
There each side has its own VCC and its own GND. Both sides are electrically isolated and only magnetically or capacitive coupled.

Sorry ignore my earlier question, I've been reading about this and I'm getting a better handle on it. Question though, UART VCC is typically 5v on power, and 3.3v on data lines. It looks like the SN74AXC4T774 expects no more than 3.6v. I'm assuming this solution won't work if the power is 5v? I can modify the CH9328 to send 3.3v on VCC and use a 3.3v pin in the Pi. But with the USB adapter I'm using, the VCC is 5v and I don't think I can change that?

Not sure about the ADuM1201. It says it has a 5v operation mode, but from what ChatGPT tells me (and this could be totally wrong):

Quote
The ADuM1201 does support up to 5 V on each of its supply pins (VDD1 or VDD2)—in fact, its recommended operating range is 2.7 V to 5.5 V on either side
Analog Devices
.

However, it’s important to understand:

No internal buck or regulator.
Supplying 5 V to VDD2 simply means the isolator will drive its B‑side I/Os at 5 V.

I/O voltage = supply voltage.
Whatever you feed into VDD2 shows up (via the transformer/capacitor coupling) at VOA/VOB. If you power VDD2 at 5 V, the outputs will be 5 V—even if the CH9328 itself only wants 3.3 V logic.

Mixed‑voltage operation is supported—but only because you can independently choose VDD1 and VDD2 anywhere between 2.7 V and 5.5 V. It does not step 5 V down to 3.3 V for you.

Just trying to navigate a subject I know very little about  :)
 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 3521
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #8 on: April 21, 2025, 09:08:49 pm »
The module has a Vcc jumper so you can have the Vcc pad be 3V instead of 5V.

If you have a MCU/Rpi on the UART side you can fix the TX idle state being high in software.

Another idea is to use one of those simple single MOSFET level shifters -- have the TX signal on the "HV" side and have the "LV" source be the 3V from the module.

https://electronics.stackexchange.com/questions/555631/understanding-how-this-bi-directional-logic-level-shift-works
 

Offline phil from seattle

  • Super Contributor
  • ***
  • Posts: 1149
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #9 on: April 21, 2025, 10:20:59 pm »
Can the Pico tolerate voltage on RX/GND when unpowered? I just tested the XAIO SAMD21 MCU and I observed similar behavior. The MCU is put into an error state and stays there even after restoring 5v power over VBUS. It recovers if I unplug RX/GND and continues to work after I restore RX/GND. Same behavior as the CH9328.

So far my options seem to be:

- ItsyBitsy VBAT pin, providing alt power from Pi 4B's 5v pin so the MCU never loses power (may not be USB complaint, I need to probe D+/D- in this state)
- Find a way to keep power from back-feeding from RX.
- Just use Ethernet and don't connect Pi/MCU (would prefer not to have the network as a middleman if I can help it).

Unless the Pico has a way to prevent the back-feeding, I'm not sure that would work either. If USB power is constant and serial is plugged in after, it works in the above cases. It only fails when the serial device is powered and USB power is restored after.

I'd use a voltage divider for RX into a 3.3V device, assuming you are using a 5V UART output.  Since RX operates as an input, I don't see how backfeeding happens.
 

Offline fchk

  • Frequent Contributor
  • **
  • Posts: 324
  • Country: de
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #10 on: April 22, 2025, 10:11:10 am »
The solution is to use a dual-supply level shifter. Example: SN74LVC4T774
https://www.ti.com/lit/ds/symlink/sn74axc4t774.pdf
The feature you need is "Vcc isolation" "If either VCC input is below 100 mV, all I/Os outputs are disabled and become high-impedance" and "Ioff supports partial-power-down mode operation".

Each side has its own VCC, but both sides share a common ground. The level shifter prevents backfeeding.

Sorry ignore my earlier question, I've been reading about this and I'm getting a better handle on it. Question though, UART VCC is typically 5v on power, and 3.3v on data lines. It looks like the SN74AXC4T774 expects no more than 3.6v. I'm assuming this solution won't work if the power is 5v? I can modify the CH9328 to send 3.3v on VCC and use a 3.3v pin in the Pi. But with the USB adapter I'm using, the VCC is 5v and I don't think I can change that?

The voltage on the data lines of these chips are the same as the voltage on the respective Vcc. If your device supplies 5V but uses 3.3V levels on the date lines, then simply use a linear regulator like MCP1703A to get your 3.3V supply:
https://ww1.microchip.com/downloads/en/DeviceDoc/20005122B.pdf

Not sure about the ADuM1201. It says it has a 5v operation mode, but from what ChatGPT tells me (and this could be totally wrong):

Quote
The ADuM1201 does support up to 5 V on each of its supply pins (VDD1 or VDD2)—in fact, its recommended operating range is 2.7 V to 5.5 V on either side
Analog Devices
.

However, it’s important to understand:

No internal buck or regulator.
Supplying 5 V to VDD2 simply means the isolator will drive its B‑side I/Os at 5 V.

I/O voltage = supply voltage.
Whatever you feed into VDD2 shows up (via the transformer/capacitor coupling) at VOA/VOB. If you power VDD2 at 5 V, the outputs will be 5 V—even if the CH9328 itself only wants 3.3 V logic.

Mixed‑voltage operation is supported—but only because you can independently choose VDD1 and VDD2 anywhere between 2.7 V and 5.5 V. It does not step 5 V down to 3.3 V for you.
That's right.
The reason why they choose 5V on power supply but 3.3V on data lines is voltage drop. On data lines the current flow is very low. So very few mV are lost. On the power supply the current is usually much higher, so for long and thin wires you might loose a considerable amount of voltage. So they feed more volts than needed and expect a voltage regulator on the other side to make the desired 3.3V out of it.

Old linear regulators like the 78xx need 2V headroom to operate, newer ones like the suggested MCP1703A only need 0.6V. So even if you loose 1V on the 5V rail the MCP1703A would be able to provide stable 3.3V.
 

Offline LTNineTopic starter

  • Newbie
  • Posts: 5
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #11 on: April 22, 2025, 08:10:23 pm »
Looking deeper at the CH9328, it looks like it is not designed with two power sources in mind. There is no back flow protection even on power, let alone data. VBUS and VCC have no resistance between them, and they share a common net. It look like the intended design is to send power thru USB-C on the CH9328, and then power the serial device with VCC. Modification would be tricky. The Pi example showing VCC disconnected is odd because the Pi would most certainly have an independent power source.

Looking at easier options, it does look like the Pi Pico would be a good choice because it supports an external power source on VSYS. It even mentions in the doc that you can have dual power sources, however, the Pico only has one Schottky diode protecting VBUS from the external power source. It calls out adding your own Schottky diode to protect your external power source when needed.

I'm also looking at the ItsyBitsy M0 Express. It has two Schottky diodes protecting each power source in both directions. However, this answer concerns me a bit: https://electronics.stackexchange.com/a/744374/164335

If I use the Pico and add a Schottky diode it's not clear to me if the Pico has the same problem mentioned in the SE answer, or if they would behave the same or differently.

The ItsyBitsy schematic is here (although it seems to be mislabeled): https://learn.adafruit.com/assets/108639

This file has the right label, but the schematics look the same: https://github.com/adafruit/Adafruit-ItsyBitsy-M0-PCB/blob/master/Adafruit%20ItsyBitsy%20M0.sch

The pico data sheet is found here: https://datasheets.raspberrypi.com/pico/pico-datasheet.pdf

Schematic is on page 26. Page 20 and 21 talks about protecting VSYS power with a Schottky diode or a P-FET.

It appears like the ItsyBitsy could be the better option because it already has the second Schottky diode protecting VBAT. But I'm not sure about the data behavior when the USB cable is plugged into an unpowered device and whether that would be a concern or not.
« Last Edit: April 22, 2025, 08:15:12 pm by LTNine »
 

Online ledtester

  • Super Contributor
  • ***
  • Posts: 3521
  • Country: us
Re: Controlling KVM with Raspberry Pi thru MCU?
« Reply #12 on: April 22, 2025, 10:32:38 pm »
My idea is to only connect GND and TX from the UART side and prevent power from being injected into the RX pin.

The CH9328 would only get power from the USB port. You would pull up the RX pin to the CH9328's Vcc and when low the TX signal would bring it ground via a transistor or optocoupler.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf