I sometimes do electronics projects as a hobby and as far as I know if you want to switch something in a circuit electronically, transistors are the way to go. And I have used them successfully before to control high current loads such as LEDs or DC motors with a micro-controller.
But what I'm trying to do now is to turn a TTL serial connection on and off. As if I was physically disconnecting the TX
wire from my micro-controller's RX
pin. And I have realized that every tutorial I've ever read about using transistors as switches (this one for example) assume that you are using the transistor to turn a load on or off.
I found a few similar questions on the Internet but they all are drifted away from the subject one way or another and left unanswered. (This and this for example).
I have actually managed to do it myself, with the following circuit:
My though process behind this is: A transistor connects the Collector to the Emitter when it is saturated, so when I "turn on" the transistor, TX
should get connected to RX
. And it actually works!
When nothing is connected to the base my micro doesn't receive anything. When I connect 5V to the base the micro starts receiving serial data. I'm outputting this data to a serial console and it's valid, non-gibberish data.
But having learnt from previous experiences, after achieving this I started searching the Internet to see if this is the proper way to do it. But I couldn't find anything about how to switch a signal/data line on and off. As I mentioned, they are all about switching loads using NPN
or PNP
transistors to connect to GND
or VCC
respectively.
What I want is something that acts exactly like a physical switch, that I can put anywhere in the middle of my wire and connect/disconnect it. Which my circuit seems to be achieving.
So am I doing it right or am I just getting lucky?
I have a feeling that I'm just getting lucky.
Best Answer
This is probably not the best way to do this, as the voltage will drop 0.6V across the transistor (assuming its saturated, which it's probably close). For 3.3V TTL the Vih is 2.4V or 2.0V depending on what you have, so odds are you'll still turn on the logic your connecting to. But this isn't the way to do this.
There are better options.
A TLL buffer with an enable line would probably be the best:
Source: https://www.allaboutcircuits.com/textbook/digital/chpt-3/special-output-gates/
There are lots of chips available if you search a distributor (like digikey)
Another way would to use a switch with a low resistance:
The low resistance keeps the voltage from dropping.