Switch Between Two Different UART Depending On If USB Is Plugged In Or Not

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
Hello, I have an ESP32 microcontroller that has UART (TX and RX pins). I have them currently connected to a CP2102 (very common USB to UART converter chip). I then have a micro USB connected to the CP2102. This works great for flashing firmware, serial dubigging, etc.

I also have a Raspberry PI, which also has UART(TX and RX pins), that I would like connected to the same RX and TX pins of the ESP32 (has to be the same pins in this case - I cannot set up multiple separate UARTs in this project). Since I cannot have them both connected at the same time, I would like to be able to switch between the two.

Instead of using jumpers / dip-switches to switch between the two.... is there a way I can use the VBUS signal on the USB to trigger the switch? For example if a USB is plugged in, +5 volts will now be present on the VBUS pin, could this somehow be connected up to some transistors/mosfets, which disconnects UART from the raspberry PI, and connects UART with the USB port. If USB port is disconnected, then UART is disconnected from USB port, and is re-enabled for the raspberry pi.

This seems doeable, but I am a little confused on how to configure the transistors/mosfets to accomplish this. Any help or advice is greatly appreciated, thanks!

Here is a sketch of what I am trying to do:
20230213_155910.jpg
 
Last edited:

MrChips

Joined Oct 2, 2009
30,714
It is hard to follow what you are trying to do. Perhaps a drawing would help.

How many USB connections do you have?
How many PCs are involved?
What is connected to what?
What devices are permanently connected?
 

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
It is hard to follow what you are trying to do. Perhaps a drawing would help.

How many USB connections do you have?
How many PCs are involved?
What is connected to what?
What devices are permanently connected?
I just uploaded a sketch above.

1 USB connection. I want to switch between these 2 scenarios:

USB Plugged in: ESP32 -> CP2102
USB Not Plugged in: ESP32 -> Raspberry PI
 

MrChips

Joined Oct 2, 2009
30,714
Thanks for the drawing. That is much clearer now.
ESP32 TX sends data to two RX devices. You don't have to do anything here.

CP2102 is powered by USB. It should go into hi-z state with the USB cable not connected.
The only thing you have to worry about is the RaspberryPi TX output. If you can configure this TX to open-drain then it should not conflict with the active CP2102 TX.
 

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
Thanks for the drawing. That is much clearer now.
ESP32 TX sends data to two RX devices. You don't have to do anything here.

CP2102 is powered by USB. It should go into hi-z state with the USB cable not connected.
The only thing you have to worry about is the RaspberryPi TX output. If you can configure this TX to open-drain then it should not conflict with the active CP2102 TX.
Thanks for the response!

How do you know the CP2102 goes into hi-z state when not powered? This was something I was trying to find in the datasheet but couldn't figure it out. So in this case, the CP2102 needs to be powered by the USB cable right? (I currently have it powered by onboard 3.3V.

On the raspberry pi, there is a UART driver that drives the pins high and low, so I don't think it can be easily configured. What is another option for this? I wonder if there is a UART multiplexer chip available or something?

If you have no control on the rPi TX output, you can try this logic circuit.

1676334184583.png

A and B are the TX outputs and Q is the ESP32 RX.
Use 1N5817 diodes and R = 10kΩ
 

MrChips

Joined Oct 2, 2009
30,714
If you have an oscilloscope you can tell if the pins float with no USB cable attached.
If you have no oscilloscope, use a 100kΩ load resistor. Test the voltage on a DMM with the load resistor connected to Vdd and then to GND. If the DMM shows Vdd and GND respectively then you know the pin is floating.
 

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
If you have an oscilloscope you can tell if the pins float with no USB cable attached.
If you have no oscilloscope, use a 100kΩ load resistor. Test the voltage on a DMM with the load resistor connected to Vdd and then to GND. If the DMM shows Vdd and GND respectively then you know the pin is floating.
I just had a thought - would this work?

ESP32 TX pin connects to both CP2102 RX pin, and PI RX pin.
CP2102 TX pin, and PI TX pin go into an "OR Gate". Output of OR Gate goes to ESP32 RX pin.
VBUS on USB goes through a voltage divider to make it a 3.3V signal, and this goes to a raspberry PI GPIO. If the raspberry PI reads a 1 on this GPIO, that means USB is active, so don't use UART on PI for the time being, until USB is removed.

Even if the raspberry PI can't detect the presence of the VBUS on USB, the OR gate should protect everything electrically right? Just data could potentially get scrambled if both CP2102 and raspberry PI are transmitting at same time.

Would something like that work?
 

dcbingaman

Joined Jun 30, 2021
1,065
The most straight forward method I can think of would simply be a multiplexer like the following:

https://assets.nexperia.com/documents/data-sheet/74LVC1G157_Q100.pdf
and this:
https://assets.nexperia.com/documents/data-sheet/74LVC1G18.pdf

1676340532192.png

Both chips are 5V tolerant inputs. You need to run 3.3V power to both chips. The resistors on the RX lines keep the RX lines 'high' that is no start bits during transitions. The resistor on the CP2102 TX line keeps it high when VBUS is not connected, it is there again to stop transients during transitions from one TX/RX pair to the other. You could use other chips just make sure the inputs are 5V tolerant. Don't forget decoupling caps on the chips. It is important to keep the RX lines from transitioning low at any point to avoid a false 'start' bit showing up on the lines during USB being plugged in or removed. I think this should accomplish that.
 
Last edited:

dcbingaman

Joined Jun 30, 2021
1,065
I just had a thought - would this work?

ESP32 TX pin connects to both CP2102 RX pin, and PI RX pin.
CP2102 TX pin, and PI TX pin go into an "OR Gate". Output of OR Gate goes to ESP32 RX pin.
VBUS on USB goes through a voltage divider to make it a 3.3V signal, and this goes to a raspberry PI GPIO. If the raspberry PI reads a 1 on this GPIO, that means USB is active, so don't use UART on PI for the time being, until USB is removed.

Even if the raspberry PI can't detect the presence of the VBUS on USB, the OR gate should protect everything electrically right? Just data could potentially get scrambled if both CP2102 and raspberry PI are transmitting at same time.

Would something like that work?
You may need to place a pull down resistor on the CP2102 TX pin to make sure the 'OR' gate gets a good logic low signal when USB is not plugged in the problem is though that the TX line on the Rasberry is going to hold the TX line high on the output if it is programmed as a UART, you will need to program it as a standard output and drive it low when the USB is plugged in (rather complicated). I would recommend the circuit I have in the previous post.
 

dcbingaman

Joined Jun 30, 2021
1,065
I just had a thought - would this work?

ESP32 TX pin connects to both CP2102 RX pin, and PI RX pin.
CP2102 TX pin, and PI TX pin go into an "OR Gate". Output of OR Gate goes to ESP32 RX pin.
VBUS on USB goes through a voltage divider to make it a 3.3V signal, and this goes to a raspberry PI GPIO. If the raspberry PI reads a 1 on this GPIO, that means USB is active, so don't use UART on PI for the time being, until USB is removed.

Even if the raspberry PI can't detect the presence of the VBUS on USB, the OR gate should protect everything electrically right? Just data could potentially get scrambled if both CP2102 and raspberry PI are transmitting at same time.

Would something like that work?
ok, if you replace the 'OR' gate with an 'AND' gate and keep pull up on the CP2102 TX output with a resistor (10K say) that should work. Both TX lines then stay high when not in use (not transmitting) and only one or the other can pull the TX line out of the AND gate low when transmitting should work.
 
Top