0

I'm using an Arduino Mega which has 4 RX/TX UARTS/serials. 3 RX's of them I use for MIDI In 2 TX's of them I use for MIDI Out (maybe later a DMX out too)

One I use currently for debugging. On the other hand, in the future I want:

  • Using an ESP8266 WIFI which also seems to use an UART (both RX/TX)

Of course I can remove the debugging RX/TX, but maybe some more additions are needed and for the first RX/TX I need to remove the RX each time I upload (so I rather leave it for debugging).

I was wondering, is it possible to share an UART if I don't use the WIFI module and MIDI port at the same time?

2

It's certainly possible to multiplex a UART port, yes, though you will need extra hardware to do it. You will need:

  • A multiplexer to select between the two RX lines and feed the right one to the Arduino's RX pin
  • A demultiplexer to select which of the devices to send the Arduino's TX pin to.

"Analog" (de)multiplexers (e.g., CD4052) may be the simplest option since they can work in both directions. Another alternative would be combinations of tri-state buffers and glue logic to control them.

3
  • You could simply send the TX to both devices. If one is always going to be missing it doesn't matter what the data line to it is doing. Rx it depends whether these are TTL level signals or not. If they are TTL straight from the devices and one one device is ever fitted you can wire them together. As soon as you have a level shiftier or buffer on the signal then you need a Mux. – Andrew May 5 '17 at 11:16
  • If there is only one physical device connected at a time then yes you can just wire direct. – Majenko May 5 '17 at 11:18
  • I don't know if e.g. a WIFI ESP8226 is considered as physically connected (I guess it is) ... the MIDI cable is not, but I rather don't want to be rely on a user having to remove the cable (also the MIDI in is transferred through an opto coupler which is physically connected to an RX pin). However, it's ok to know there is a solution (for now). I just don't want to think further into a direction that is absolutely impossible from the start. (background: now I use or plan to use an SD card to move from a PC from the Arduino every time, but later a WIFI module is more convenient). – Michel Keijzers May 5 '17 at 11:21
1

If your application can tolerate the limitations of SoftwareSerial, it's the easiest way to add a port. The terminal may be the better candidate for it.

4
  • 1
    SoftwareSerial is the absolute worst alternative. It disables interrupts for an entire character time (1ms @ 9600) when transmitting OR receiving (it can't do both at the same time, either). If the baud rates don't match, this would cause all the other UARTS to lose characters. If he must use a software serial port (not that I suggest that), use AltSoftSerial instead! – slash-dev May 5 '17 at 13:29
  • 1
    The original SoftwareSerial hasn't been in the core library for quite some time. Since v1.0, SoftwareSerial has been replaced by [arduiniana.org/libraries/newsoftserial/](NewSoftSerial) which addresses some, if not all, of those shortcomings. The different software implementations come with their ownabilities and limitations (AltSS is tied to fixed pins, f/ex). And do note that I alluded to the limitations of software implementations. – JRobert May 5 '17 at 20:10
  • 1
    Nope. Those are the current problems. Look at the SoftwareSerial::recv method, which is called from the ISR: it has a for loop. The ::write method is very similar. :( – slash-dev May 5 '17 at 21:05
  • I am not a favorite for using SoftwareSerial, only for debugging it might be 'acceptable', but in that case I probably rather try to wire another Arduino only for that purpose. – Michel Keijzers May 6 '17 at 0:06
1

Consider running the RX and TX of the first UART to the common terminals of an On-Off-On DPDT switch or relay. Calling the On positions A and B, connect the A terminals to RX and TX of one device, and the B terminals to RX and TX of another device. Use position A for the first device, position B for the second device, and the Off position for program loading.

It isn't clear to me whether your debugging is via USB serial or via a separate device. In the former case, a DPST switch would be adequate. Also, two SPDT's could be used in place of a DPDT if you wish to switch RX and TX separately, or four SPST switches can be used if you have nothing better.

This is premised on the idea that during development you need to be able to send/receive debugging IO. I don't know how to interpret your “don't want to be rely on a user having to remove the cable” phrase, but suppose a user-cabled setup won't need debug-channel switching.

1
  • This should be possible (at least for two of the UARTs, since I need the other exclusively and simultaneously). I am debugging it now via the USB serial (but afaik, I have to keep that RX/TX free, at least while downloading, maybe nothing to do with debugging). And the SPDT/DPDT could be used for two UART 'inputs'. And I mean with user in this case myself (as the debugger). Sorry for being unclear, – Michel Keijzers May 6 '17 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.