Trouble interconnecting two boards for serial

Hi, I have a project underway which uses serial communication via USB. A windows program sends a character to the Arduino and it responds with some data. I am using a Nano 33 BLE Sense and a Mega 2560. Individually I can connect via USB and use the serial monitor to send a data request to Arduino and then display the returned data. Both boards are working this way. Both boards are powered by USB.
I then connected the TX of one board to the RX of the other. I am hoping I can power both boards via USB, but communicate with only one board directly via USB, and to the other board via the serial interconnection. But when I try this I am not communicating with the second board. For example, I connect both boards to two USB ports on a computer. I connect to the Mega via USB using the serial monitor. I am hoping both boards will see anything sent from the computer to the Mega, and that anything returned from either board will get to the serial monitor via the virtual com port connection, via USB to the Mega. But this is not working, either way.
I have a common ground between the boards. Only one board will respond to any specific data request so there is never simultaneous serial com from the computer or from the two boards. Both boards are set to the same baud.
I am thinking the problem may be that this simply doesn't work between these two boards because the Nano is 3.3V and the Mega 5V. Or perhaps the pinout of the sense is different from the drawing I am looking at. I can't see the markings on the Nano board because they are on the bottom when it is pushed into a breadboard.
On the Nano board I am using Wire.h and on the Mega board I am using HardwareSerial and OneWire.h for some sensors. Can any of these disable the TX and RX pins ?

You can’t communicate with both boards simmultaneously while connected to the same serial port. Serial has no way to switch between devices and if both MCU’s transmit back at the same time the signal from one will interfere with the other. However the Mega has 4 serial ports so you could connect the Nano to say Serial1 and have the Mega relay messages/data back and forth between Serial and Seral1 as required.

If your Nano board operates at 3.3v then you should ideally use a level shifter.

Note that I said I connect to Arduino via virtual com port via USB with only one board, hoping to connect to the second board via the RX and TX pins. Also, I said that with my project the two boards will never transmit at the same time, nor at the same time as the computer. From my reading I thought this would work, that the USB serial and the RX and TX pins have the same serial data.
I want to send serial to both boards simultaneously, while connected to only one board(via USB). And I want to receive from either board even though only one is connected via USB. Only one board will respond to any one data request from the computer.
If I use other serial pin pairs on the MEGA, how do I get them to duplicate the serial stream from the USB port ?
What sort of level shifter would I use between the two TX/RX pairs ?

bobbykennedy:
Note that I said I connect to Arduino via virtual com port via USB with only one board, hoping to connect to the second board via the RX and TX pins. Also, I said that with my project the two boards will never transmit at the same time, nor at the same time as the computer. From my reading I thought this would work, that the USB serial and the RX and TX pins have the same serial data.

I take your point, but at the end of the day you are linking at least 3 devices to one UART: the MCU, USB and the MCU on the second board. There is also USB on the second board, but since is not plugged in we can ignore it. The RX0/TX0 pins present the Serial[0] port from the MCU which is connected to USB. So while it is true that during USB-MCU communication the pins will carry the data being sent back an forth to USB, if you connect another device, the MCU’s of both boards will now “hear” transmission from USB. While either MCU is transmitting, both USB and the other board will hear that transmission. The MCU on the other board may try to process and respond to it unpredictably. One transmission may block another or two inadvertent simultaneous transmissions may cause data to be garbled. There is no collision control/prevention. While it is true that any device connected to the wire may pick up the signal, serial connections were not designed as a 1:1 technology and connecting multiple devices together and the result of doing so can lead to unpredictable results.

bobbykennedy:
I want to send serial to both boards simultaneously, while connected to only one board(via USB). And I want to receive from either board even though only one is connected via USB. Only one board will respond to any one data request from the computer.

Can you absolutely guarantee that the boards will never respond to a request from the PC at the same time? How is the PC determining which board is transmitting at any given time? Since both boards (MCUs) will “hear” the same request, how will each board determine that it should be the one to respond?

bobbykennedy:
If I use other serial pin pairs on the MEGA, how do I get them to duplicate the serial stream from the USB port ?

There’s a simple example of how that may be done here:

While this example echoes everything between two ports, it can be modified to conditionally forward only the required information. The Mega receives the request from the PC then decides whether this is for itself or whether to pass to the Nano. Requests for the Nano are forwarded to the second port. Responses from the Nano are passed back from the second port to the USB port. Requests from the PC can be made to target a specific board perhaps by including an ‘ID’ token of some sort in the request and returning replies can likewise perhaps be tagged with the same token to identify which board. At least that way you know for sure which board has responded. Both serial connections Mega <=> USB and Mega <=> Nano are 1:1 and not prone to any cross-over.

bobbykennedy:
What sort of level shifter would I use between the two TX/RX pairs ?

You could use one of these types of board:
https://www.ebay.com/itm/5Pcs-Bi-directional-Logic-Level-Shifter-Converter-Module-5V-to-3-3V-For-Arduino/193366909655?hash=item2d0590e2d7

Alternatively you can use a resistor on the TX output of the Mega. I have used a 1.8k + 3.3k in series to ground (TX → 1.8k <-3.3v output here-> 3.3k → GND) and then tap off the signal to the 3.3v device above the 3.3k resistor. The 3.3V TX signal from the 3.3V board is sufficient to register as ‘HIGH’ on the 5V device. The exact values of the resistors are not critical so long as an approximately 2/3 ratio is maintained. I have also seen suggestions of simply placing a 1k in series.

You asked how I can guarantee that the two boards will not send data simultaneously. I've got a Delphi program running on Windows that sends a request for data. The request is specific. For example it may send an 'A'. This tells the mega sketch to send the temperature. The Nano sketch is not written to respond to an A. Or it may send an 'a'. This tells the Nano to respond with the barometric pressure. The Mega sketch has no response for an 'a'. So only one board sends data at any one time. The computer sends only one character, and doesn't send another until well after receiving a response. In this way only one of the three will send data at any one time. I'm still reading the next responses.
But to reiterate, what I want to do is have both boards hear the data request from the computer even though only one board is connected to the computer. And I want the computer to hear any response from either board via the single virtual USB com port connection.
I'm still reading the last responses.

That's funny...I had found and printed the MultiSeriaMega article you sent me a link to, last night and read it before bed. I didn't find it helpful then, but now after reading your reply I think I am beginning to understand.
Here's what I think you are saying: The reason I get no cross serial com is because the TX from Mega is 5V., too much for the Nano. And so I need to use the board or resistors to make this work. But I shouldn't connect three things this way and so should use another serial pair on the Mega, according to the article. Does that sound right ?

I just looked at and ordered one of those boards. It looks easy and quite useful. It's certainly not expensive.
It's funny, I found and printed the MultiSerialMega article you sent a link to, last night before leaving, and read it before bed. At the time it didn't seem too useful. But now I think I understand. If I understand you correctly, the reason I got nothing between the two boards is because of the voltage difference and that to connect serial between two boards of different voltages, I need to use the board or resistors to limit the voltage coming from the Mega TX, but that I shouldn't connect three things this way even if I can guarantee the three things will never transmit simultaneously, because it can cause other problems. Is that correct ?

On the Mega, use Serial1/2/3 and adjust your code.

Serial is also used for the usb communication from Mega to the PC. Not familiar with the other board, but I think that you also need a different serial (possibly Serial1); also code changes needed.

There is a blue bar just above this reply window that says Newsletter, Enter Your E-mail to sign up...SUBSCRIBE. It hides the very bottom of the text above, so I cannot see your last reply, except the first line. And I can't get rid of this blue bar. If you could just send the same reply again, it will push your last reply up so I can read it. Actually, when I send this, it should fix the problem by pushing up your reply above the blue bar.

On request :wink:

On the Mega, use Serial1/2/3 and adjust your code.

Serial is also used for the usb communication from Mega to the PC. Not familiar with the other board, but I think that you also need a different serial (possibly Serial1); also code changes needed.