Setting up 2 or multiple UART connection in an Arduino UNO with other modules

I want to set up 2 or multiple UART connection in an Arduino UNO with other modules. at first I want to know if it is possible? Then if it is possible how can I set up it? (And please if you have a tutorial for this share with me)

As I search the internet I use Softwareserial library and myserial.listen() syntax,but unfotunetly it's not working. Thank you for your helps.

Yes, in case of two UART it is possible. The first is hardware UART on pins 0 & 1, the second is software emulation via SoftwareSerial library. If you need more than 2 UARTs - it would be better to choose an Arduino with more than one on-board Serial - i.e Mega, Nano Every or Leonardo

2 Likes

But, on the UNO, beware that this UART is already connected to the USB link to your PC - so it's not really "free" for use in your sketch.

Definitely!

There's also the DUE, which has "native" USB for for communication with the PC - so doesn't "consume" one for that.

Apart from UARTs, other communication links are available; eg, I2C & SPI.
These are specifically designed as buses - meaning that just one link can carry communications with many "modules".
But they're not designed for long distances.

It is also possible to get external hardware UARTs which connect to the Arduino via I2C or SPI...

There are many more exotic options, such as the controllers of the w80x family - they each have 6 hardware UARTs.
It have Arduino support and are several times cheaper than Uno.

Success highly depends on what those modules are and how they work. If they only react on a request from your sketch, it should work. If they however just send unsolicited data, it will more than likely not work.

Baud rate is also a factor; although the documentation for SoftwareSerial states 115200 baud max, I've never heard of someone succeeding with it; 19200 should work, you might be able to push it to 38400.

HardwareSerial (as suggested above) is far preferred.

By the way, "it's not working" is not a good description of a problem :wink:

5 Likes

this is my code but I can't get any response from my module and one other arduino(pro micro):

#include <SoftwareSerial.h>

SoftwareSerial mySerial1(8,9); // RX, TX
SoftwareSerial promic(10, 11);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial1.begin(115200);
  promic.begin(9600);
  //pinMode(13, OUTPUT);
  /*while (!mySerial.available()) {
  mySerial.println("AT");
  delay(1000);
  Serial.println("connecting....");
  }
  Serial.println("Connected.");*/
}
void loop() {
  // To adding every At commands use this paragraph
  if (mySerial1.available()) {
    Serial.write(mySerial1.read());
  }
  if (Serial.available()) {
    mySerial1.write(Serial.read());
  }
  promic.print("1");
  delay(2000);
  promic.print("0");
  delay(2000);
}

Read post #6

I test other baud rates but I have the same problem

So please help me here then I'll remove my question in stack over flow!

So that you don't have to go find post #6.

Again. Use one software serial instance, at as low a baud rate as possible.
For all others, use hardware serial ports. I strongly suggest that you actually use a device with a hardware port for every connection, as that will avoid the likelihood that you will use a software serial port beyond it's very limited capabilities.
There is one scenario where you might have success with multiple software serial devices. It must fit all of these criteria:

  • all serial devices on software serial instances MUST only talk when asked to
  • each transaction with a software-serial device must be executed to conclusion. For example, you cannot askA, askB, askC, then lookforresponsefromA, lookforresponsefromB, lookforresponsefromC. Guarantees failure.
  • No other interrupt-driven services should be active while software serial devices are being talked to and or awaiting responses from; exception is millis(), but read on.

This is because to do the necessary bit-timing for software serial, you must have absolute control over time. Interrupts, by their very nature, violate this. I suspect even the minimal interrupt time associated with millis() and related functionality, can be enough disruption to violate software serial dependencies, if software serial is used at high baud rates, though I have not gone through the code for either millis(), or the various software-serial implementations, to know this for sure.

1 Like

Why do you opened a two SoftwareSerials? but reading only one?

And yes, you definitely should to choose more suitable baudrate/ testing the code with 115200 bauds is a pointless.

@khak, it's been pointed out several times that an Uno is not a good choice for this application. Why do you continue with the attempts? Move to a board with multiple hardware UARTs and make your life much simpler.

1 Like

Accoring to the following Example from Arduino Reference, two software UART Port may work; practically, they don't work:

#include <SoftwareSerial.h>

// Set up a new SoftwareSerial object with RX in digital pin 10 and TX in digital pin 11
SoftwareSerial portOne(10, 11);

// Set up a new SoftwareSerial object with RX in digital pin 8 and TX in digital pin 9
SoftwareSerial portTwo(8, 9);

void setup() {
    // Set the baud rate for the Serial object
    Serial.begin(9600);

    // Set the baud rate for the SerialSoftware objects
    portOne.begin(9600);
    portTwo.begin(9600);
}

void loop() {
    // Enable SoftwareSerial object to listen
    portOne.listen();
    
    if (portOne.isListening()) {
        Serial.println("portOne is listening!");
    } else {
        Serial.println("portOne is not listening!");
    }

    if (portTwo.isListening()) {
        Serial.println("portTwo is listening!");
    } else {
        Serial.println("portTwo is not listening!");
    }
}

This is the sketch I have tried with UNO-1, UNO-2, and NANO. UNO-1 can receive data either from UNO-2 or NANO; but, both receive data from UNO-1.

#include<SoftwareSerial.h>
SoftwareSerial SUART(4, 5); //SRX = 4, STX = 5
SoftwareSerial SUART1(6, 7); //SRX = 6, STX = 7

unsigned int integersToSend[] = {12345, 65432, 333, 4, 5050};

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  SUART1.begin(9600);
}

void loop()
{
  for (int i = 0; i < 5; i++)
  {
    SUART.print(integersToSend[i], DEC); //ASCII codes for decimal digits
    SUART1.print(integersToSend[i], DEC); //ASCII codes for decimal digits
    SUART.print(',');
    SUART1.print(',');
  }
  SUART.print('\n');  //end of message mark
  SUART1.print('\n');  //end of message mark
  delay(1000);

 SUART.listen();
  if (SUART.isListening())
  {
    char myData[20]; //if (SUART.available() > 0)
    {
      byte m = SUART.readBytesUntil('\n', myData, sizeof myData - 1);
      myData[m] = '\0';
      Serial.println(myData);
    }
  }

  //delay(2000);

  SUART1.listen();
  if (SUART1.isListening())
  {
    char myData1[20]; //if (SUART.available() > 0)
    {
      byte m1 = SUART1.readBytesUntil('\n', myData1, sizeof myData1 - 1);
      myData1[m1] = '\0';
      Serial.println(myData1);
    }
  }
}

@GolamMostafa If you'd read the rest of my post, you'd have noted the description of a limited-context scenario where multiple software serial instances can be made to work.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.