Issue with Software Serial on Arduino Mega and Nano for UART Communication

I am working on a project where I need to connect six devices to a single Arduino Mega using UART while the mega is also communicating with a PC via Ethernet. Here is my setup:

  1. Hardware UART on Mega:
  • I am utilizing the three hardware UART ports (Serial1, Serial2, Serial3) on the Mega to communicate with three devices.
  • Communication through these hardware UARTs is working perfectly.
  1. Software UART on Mega:
  • For the remaining three devices, I am using the SoftwareSerial library to create additional UART ports on the Mega.
  1. Nano Boards:
  • Each Nano is connected to specific sensors/devices and communicates with the Mega using software UART(pins 10,9).
  1. Ethernet Communication:
  • The Mega is equipped with an Ethernet shield and serves as a bridge between the PC and the connected devices.
  • Commands are sent from the PC (via Ethernet) to the Mega, which routes them to the appropriate device (Nano or other peripherals) via UART.
  • Responses from the devices are sent back to the Mega and forwarded to the PC through Ethernet.

Problem:

  • While the hardware UARTs on the Mega are functioning as expected, the SoftwareSerial communication is unreliable when both the Mega and Nano boards are involved:
    • Sometimes, I do not receive any data, or the data received is corrupted.
    • The issue seems specific to the software UART setup.

Questions:

  1. Is there a limitation or conflict when using SoftwareSerial on both the Mega and Nano for communication?
  2. Could timing or processing conflicts arise when both ends (Mega and Nano) use SoftwareSerial while also managing Ethernet communication?
  3. Are there alternative libraries (like AltSoftSerial or NeoSWSerial) or approaches for managing multiple UART communications more reliably?
  4. Would it help if the Nano boards used hardware UART instead of software UART?
  5. Are there potential conflicts between handling Ethernet communication and software UART on the Mega?

Wiring and Connections

  1. Hardware UART Connections:
    Serial1 (TX1/RX1): Nano 1
    Serial2 (TX2/RX2): Nano 2
    Serial3 (TX3/RX3): Nano 3

  2. Software UART Connections:
    SoftwareSerial1 (pins 10, 11): Nano 4 (pins 10,9)
    SoftwareSerial2 (pins 12, 13): Nano 5 (pins 10,9)
    SoftwareSerial3 (pins 14, 15): Nano 6(pins 10,9)

  3. Ethernet:

  • Ethernet shield on the Mega connects to the PC over a LAN.
  • Static IP configuration is used for communication.

If you use multiple SoftwareSerial instances, only one can listen at a time. Does you code implements this?

If your devices continuously send data (so without it being requested by the processor) you might have a problem.

so instead of using the software serial is there any other way to connect the 6 devices?

Is there a particular reason that you have to use serial communication? If not, have you thought of using I2C or SPI to communicate with the Nanos, with the Mega as master?

No actually i have 6 devices to be connect so i though we can use the hardware uart and software uart . the thing is software uart is not functioned properly now .I am trying to figure it out. If it is possible to solve the issue without changing the mode of communication protocol it is better .

I am also planning to try the below set up.

Wiring and Connections

Main Mega:

  • Ethernet:
    • Ethernet shield connected to the PC over a LAN.
  • Hardware UARTs:
    • Serial1 (TX1/RX1): Connected to TX/RX of Mega 1 (secondary Mega).
    • Serial2 (TX2/RX2): Connected to TX/RX of Mega 2 (secondary Mega).

Mega 1 (Secondary Mega):

  • Hardware UARTs:
    • Serial1 (TX1/RX1): Nano 1.
    • Serial2 (TX2/RX2): Nano 2.
    • Serial3 (TX3/RX3): Nano 3.

Mega 2 (Secondary Mega):

  • Hardware UARTs:
    • Serial1 (TX1/RX1): Nano 4.
    • Serial2 (TX2/RX2): Nano 5.
    • Serial3 (TX3/RX3): Nano 6.

what about this?

Are you using usb serial on the Nano's? If not, the you can use hardware serial on pins 0 and 1 and avoid software serial use.

now i am using it for usb serial but later i will change it so that time i will use the ins 0 and 1. i want to know now for this set up it will work fine or not?

do i need to provide my codes?

Whatever issues you may encounter with the ethernet coms and your actual code, I can not see that there will be problems caused by using the hardware uarts on all devices.

If you do need the usb serial on the Nano's, you might want to consider Nano Every's or an Arduino Micro (or other 32u4 board) which have additional hardware serial ports.

could you use RS485 which enables multiple boards to be connected using serial IO, e.g. RS485-Serial-Communication-Between-Arduino-Mega and Arduino Nano

The Ethernet part is fine. I am using UDP data transfer, and currently, I am using hardware UARTs with a total of 3 Mega boards, which is working fine. I am also planning to use some UART multiplexers. Any suggestions?

I will try

You will probably encounter the same problem as with SoftwareSerial. The multiplexer will switch to one of the devices and if a device that is not selected sends data it will be lost.

Maybe it's time to describe your project in detail?

How I would approach this, given the following constraints(these are design constraints, which you have yet to tell us so may be irrelevant):

  1. traditional serial must be used (no wifi/BT/RS485 options)
  2. distances preclude I2C or SPI
  3. the use of a Mega is because it's large I/O set is needed

And presuming you have control over all code in all the serial devices communicating.

Options, depending on which of the constraints is irrelevant:

  • use RS485 to implement a simple multidrop network
  • use BT/wifi to implement same
  • use two Every as data concentrators that provide data on request,
  • use a third Every to gather data from the first

YMMV on these ideas, depending mostly on things you've not revealed.

The project is a modular communication system that integrates multiple Arduino boards with a PC, enabling seamless interaction between connected devices and the computer. Here's a brief summary:

  1. Hardware Setup:
  • Main Mega: Acts as the central controller, connected to the PC via Ethernet for sending and receiving UDP commands.
  • Mega 1 and Mega 2: Each manages communication with three Arduino Nano boards through hardware UART.
  • Nano Boards: Serve as slaves interfacing with specific modules (device1,2,3,4,5,6)
  1. Communication Protocols:
  • UART: Used between Mega boards and Nano boards, as well as between the Main Mega and Mega 1/Mega 2.
  • UDP: Facilitates communication between the Main Mega and the PC over Ethernet.
  1. Functionality:
  • The PC sends UDP commands (e.g., *device1#, *device2#, *device3#,*device4#,*device5# or *device6# ) to the Main Mega.
  • The Main Mega routes commands to Mega 1 (if it is *device1#, *device2#, *device3#)or Mega 2(if it is *device4#,*device5# or *device6#), which communicate with the appropriate Nano board.
  • Responses from the Nano boards are sent back through the Mega chain to the PC.

It looks like the main Mega is basically polling the Nanos in which case I would consider the earlier mentioned RS485 approach. It also allows you to get rid of Mega1 and Mega2.

seem like you may be overcomplicating the system with so many microcontrollers - you will probably spend 90% of your time sorting out communications between modules
could you

  1. connect a USB-RS485 module to your PC which communicates directly with the Nanos eliminating all the Mega boards
  2. if for some reason you require the PC <> Mega Ethernet connection (e.g. distance) have that Mega communicating over RS485 wit the Nanos eliminating two Mega boards
  3. use Nano V3 Ethernet Shields to connect the nanos to the PC via a ethernet switch eliminating the Megas

can you briefly explain how the set up look like? actually In my set up the distance is 20m between the pc and the controller set up that is why i used ethernet . previously all the 6 devices are connected to a usb hub and from the hub it is connected to pc for communication. but due the distance issue I choose the ethernet cable and mega boards. if it is complicated i want to implement simple methods using RS485(I didn't worked previously with RS485)

can you just explain the whole set up and any available codes to establish the communication. I already done coding based on my approach. and I am not worked with RS485 previously.