by

Arduino Uno with Multiple Software Serial Devices

The Arduino Uno (ATmega328) has a single hardware serial connection. If more serial connections are needed, you must implement these in software. Luckily, there is a very easy to use library to implement software serial connections. It is called SoftwareSerial.h and is included with the latest Arduino IDE. The following code demonstrates how to use this library to implement more than one software serial channel. Importantly, no more than one software serial connection can be in use at a time so an open connection must be terminated before another can be started.

#include <SoftwareSerial.h>

// define the digital pins to use as RX and TX for two
// software serial connections
const int RX1 = 8;
const int TX1 = 9;
const int RX2 = 10;
const int TX2 = 11;

// create SoftwareSerial objects
SoftwareSerial SoftSerialOne(RX1,TX1);
SoftwareSerial SoftSerialTwo(RX2,TX2);

void setup(void) {
// setup the software serial pins
pinMode(RX1, INPUT);
pinMode(RX2, INPUT);
pinMode(TX1, OUTPUT);
pinMode(TX2, OUTPUT);
}

void loop(void) {
SoftSerialOne.begin(9600); // begin communication on the first 
// software serial channel
SoftSerialOne.print("Hello World"); // send something
SoftSerialOne.end(); // end communication on the first software
// serial channel
SoftSerialTwo.begin(9600); // begin communication on the second 
// software serial channel
SoftSerialTwo.print("Hello World"); // send something
SoftSerialTwo.end(); // end communication on the second software
// serial channel
}

Note that before using the second software serial connection, the first connection must be terminated using SoftSerialOne.end(). If this is not done your code will probably compile but will not do what you expect it to do. Also, the hardware serial connection can be open and used at the same time as a software serial connection without problems.

20 Comments


  1. // Reply

    Hi,
    Has Arduino uno multiple hardware serial ports?
    Because I am doing Arduino connect to other module. But how to send ‘z’ to other module from Arduino uno?
    Thanks


    1. // Reply

      The Uno uses the ATmega328 which has a single UART (hardware serial channel). You can always use the SoftwareSerial library to simulate more. That is what I am talking about in this post.


  2. // Reply

    Hey,
    Thanks for the post. Cleared up a lot of doubts. I wanted to know the maximum number of serial software ports that can be created on Arduino Uno (atmega328) practically. I need to connect three/four modules, all using serial UART. The above mentioned code lets me use 3 devices right? One using hardware serial, and the other two using software serial?


    1. // Reply

      Each software serial connection requires two digital input/output pins so there are enough pins on the Uno to do four software serial connections. Just make sure to stop one software serial connection before starting another.


      1. // Reply

        why can’t the two(or)more software serial ports be open parallely


  3. // Reply

    if i have arduino having 2 h/w serial, and i want to use only 1 software serial so 3 serial interface in total, can i still obtain a simultaneous reading of data from 3 uart-devices?


    1. // Reply

      Do you mean the mega? Yes, you should be able to have all three simultaneously turned on if two are hardware UARTs and the other is software serial.


      1. // Reply

        Can i send and receive at the same time ?


  4. // Reply

    Hello, I am using two arduinos. The first is printing “dog” using the hardware serial ports. This is connected to pins 2 & 3 on the second arduino using the softwareserial ports. However it seems so far that software serial pins can only read each other and not hardware pins. I just wanted to double check with you. Thank you!


  5. // Reply

    Hi Jeff, this was a great thread, but were you able to make this program work? doesn’t SoftwareSerial need PINs with Interrupt, whereas in UNO, only DP2 and DP3 has INT support. thanks in advance! i was thinking of buying either Uno or Mega but i need cheaper :p thanks!


  6. // Reply

    Hi Jeff, this post is interesting. Uhmm, just wondering, were you able to make this program work with Arduino Uno?
    TIA!


  7. // Reply

    I am trying to receive two uart in softwareserial but it is not getting into the while loop mentioned below
    while (portOne.available()) {
    Serial.println(“Data from port one:”);
    inByte[count++] = portOne.read();
    if(count==8)break;
    Serial.write(inByte,count);
    clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
    count = 0; // set counter of while loop to zero

    }

    my full code is
    /*
    Software serial multple serial test

    Receives from the two software serial ports,
    sends to the hardware serial port.

    In order to listen on a software port, you call port.listen().
    When using two software serial ports, you have to switch ports
    by listen()ing on each one in turn. Pick a logical time to switch
    ports, like the end of an expected transmission, or when the
    buffer is empty. This example switches ports when there is nothing
    more to read from a port

    The circuit:
    Two devices which communicate serially are needed.
    * First serial device’s TX attached to digital pin 2, RX to pin 3
    * Second serial device’s TX attached to digital pin 4, RX to pin 5

    Note:
    Not all pins on the Mega and Mega 2560 support change interrupts,
    so only the following can be used for RX:
    10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

    Not all pins on the Leonardo support change interrupts,
    so only the following can be used for RX:
    8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

    created 18 Apr. 2011
    modified 25 May 2012
    by Tom Igoe
    based on Mikal Hart’s twoPortRXExample

    This example code is in the public domain.

    */

    #include
    // software serial #1: TX = digital pin 10, RX = digital pin 11
    SoftwareSerial portOne(10, 11);

    // software serial #2: TX = digital pin 8, RX = digital pin 9
    // on the Mega, use other pins instead, since 8 and 9 don’t work on the Mega
    SoftwareSerial portTwo(8, 9);
    unsigned char inByte[64];
    int count=0;

    void setup()
    {
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }

    // Start each software serial port
    portOne.begin(9600);
    portTwo.begin(9600);
    }

    void loop()
    {
    // By default, the last intialized port is listening.
    // when you want to listen on a port, explicitly select it:
    portOne.listen();
    //Serial.println(“Data from port one:”);
    // while there is data coming in, read it
    // and send to the hardware serial port:
    while (portOne.available()) {
    Serial.println(“Data from port one:”);
    inByte[count++] = portOne.read();
    if(count==8)break;
    Serial.write(inByte,count);
    clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
    count = 0; // set counter of while loop to zero

    }

    // blank line to separate data from the two ports:
    // Serial.println();

    // Now listen on the second port
    portTwo.listen();
    // while there is data coming in, read it
    // and send to the hardware serial port:
    // Serial.println(“Data from port two:”);
    while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
    }

    // blank line to separate data from the two ports:
    // Serial.println();
    }
    void clearBufferArray() // function to clear buffer array
    {
    for (int i=0; i<count;i++)
    { inByte[i]=NULL;} // clear all index of array with command NULL
    }


  8. // Reply

    My project consist xbee and rfid both uses serial comm, how to connect the hardare pls let me know


  9. // Reply

    now how do i select which port to use?


  10. // Reply

    hi, i really need your help
    I have a project that uses 2 RFID ID-12LA with Arduino UNO. Is it possible to use two serial port in it?


  11. // Reply

    How to solve the problem of being used at same time plz ?


  12. // Reply

    Thanks for clear description of working with two software serial. I was working with GSM and GPS both instantiated as serial objects. But nothing was appearing on Serial monitor. It works now.

    Still I need to know how software serial exactly works. If it is same as UART connection then how any digital/analog pin can be assigned as RX/TX ??


  13. // Reply

    This answered my problem.
    I was using two Software serials and the hardware serial talking to a robot over XBees, but I kept getting random characters in the string. I tried your idea of turning off one serial connection using your method and it solved my problem.
    I ended up keeping the XBee serial running all the time and turned off the serial that talked to the driver card [sabertooth 3×32].
    Thanks a lot for the helpfull notes on this.


  14. // Reply

    So, Let me get this straight, If I use this library I can then connect lets say two bluetooth modules to the Arduino, then one of the módules can recieve/send serial data to the PC but the other one can only recieve data via bluetooth (to actívate leds) but I cant send data to the serial port on the PC? If that is how it is then this is pretty much what I need .


    1. // Reply

      I think you are better off starting with the Arduino 101 if you need bluetooth connectivity as it comes with an intel chip with bluetooth functionality built in. The problem with using software serial for multiple linkups is you can’t use the connections concurrently which will probably be unacceptable when interfacing with external devices.

Leave a Reply

Your email address will not be published. Required fields are marked *