This works using an extra GPIO pin, or you can use it in manual mode setting a jumper without using an extra GPIO pin. See last picture in the gallery. When the GPIO/manual shunt is low, TX1 - RX1 is connected, when GPIO/manual shunt is high the TX2 - RX2 is connected, it's as simple as that.
Nothing stops you from generalizing this, but I found that one extra serial port is usually enough. And if I wanted more, probably I'd look for a different solution (eg. dual 4,8,16:1 multiplexer for instance, plus a buffer if I was worried about buffering the GPIO signal). However, if you wish, of course, you can drive other buffers with this circuit thereby creating a crazy multiplexer for more serial devices. You can drive (number of used GPIO control pins)^2 serial devices this way.
- a 74HC241 buffer & line driver (see more 74HC241 projects here - including AVR programmers) in theory you can use the 74LS241 as a replacement with the exact same circuit, just replace the HC - I haven't tested this, yet (Vcc = 5V only for the LS, check the datasheet)
- a 20 way IC socket
- 5 resistors 22k (other values are possible, too, it depends on your application, eg. 1k to 10k would be usually OK)
- 5 header pins for the jumpers
- 7 sockets or 7 header pins - it depends on whether you use male or female cables for the device/RPi connections
- 1 jumper
- a PCB perma proto board
There is one unused line each way on the buffer (1a4 - 1y4 and 2a3 - 2y3). You can solder extra pins there each way to drive something at the same time with the serial devices, or you can connect them to Vcc or GND when you're not using them. This is up to you. When unused they must be connected to GND or Vcc to meet worst case specifications, don't leave them floating. See this application note from TI on this topic. In some cases you might get away with not doing that, though.
Step by step gallery for building this project with instructions
Tests and limitations including level shifting
What if you operate the HC at 5V and connect a GPIO outputs to one of its outputs with the 22k resistor in series? How much current will flow?
The total maximum current allowed on the 3.3V GPIO rail is 50 mA. The HC74 can have an output of 6 mA max.
Case 1: something goes wrong 1. GPIO is configured to output high:
HC output voltage in test: 5.10 V
GPIO is high in output mode: 3.28V
Now you connect the GPIO with that HC output voltage when the resistor is in series.
Expected current: 1.82 V / 22000 o= 0.082mA
Measured: 0.082 mA.
Case 2: something goes wrong 2. GPIO is configured to output low, this is the shorting worst case.
HC output voltage in test: 5.10 V
GPIO is low in output mode: 0V - oh, no!!
Now the mayhem happens: you connect the GPIO with that 5.1V HC output voltage when the resistor is in series.
Expected current: 5.10 V / 22000 o = 0.232mA
Measured: 0.231 mA.
Case 3: the level shifting scenario 5V to 3.3V - a bit of misuse. GPIO is configured as input and we're running the HC at 5V. Do not run the HC on 3.3V for this.
HC output voltage in test: 5.10 V - now we connect that with the GPIO input with the resistor being in series.
Current drawn: 0.004 mA.
Case 4: the level shifting scenario from 3.3 V to 5V - this is normal operation:
HC voltage: 5.10V
GPIO configured as output high: 3.28V connected to the HC input.
Current drawn from GPIO: 0 uA (I couldn't measure any current even in microamp resolution.)