Additional Serial Ports and Adapters

Now we have our standard Serial interface working back to the PC, there is often a need to use a secondary interface to talk to e.g a Serial LCD Screen, or to talk to another Arduino board or PC.

Most boards support more than one Serial interface in the Hardware (such as the Arduino Mega), and additionally you can also use the Software Serial Library on most boards, to allow the Rx/Tx pins to work on any pair of digital pins.

 

Using Secondary Serial (Hardware)

This is the same as using the standard Serial interface, except it will have a different name such as Serial1, Serial2 (depending on the board in use)

Again this will need to have the begin(baudrate) function call to set it up in your software, and then all Println() and Write() functionality can be used as normal on this second port.

The Serial Devices available on standard Arduino Boards are listed here.


Using Software Serial (Software)

When there is no hardware device available, Software Serial can be used.

This has the additional setup element of needing to define which pins will be used for Rx and Tx, as well as the baud rate it should work at. We will also need to include the header from the library (shown below) to allow us to access this functionality.

Note Icon Note:

Software Serial is not as fast as Hardware Serial, so speeds > 115200 can become unstable if using this mechanism. Check the notes on the Arduino Documentation for all limitations.

 

In Visual Micro you can add this library by simply selecting vMicro > Add Library > Current Platform > Software Serial, and it will automatically add the headers for you to your sketch.  We will go over libraries more in the upcoming section...

 

#include <softwareserial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup(){
  // set the data rate for the SoftwareSerial port
  mySerial.begin(38400);
}

void loop(){
  mySerial.println("Hello, world?");
  delay(1000);
}

Using Additional Serial Ports to a PC

As we noted before, the TTL Serial on the board cannot be plugged directly into your PC's serial port due to the power differences.

To allow us to use our additional ports from above, we will need to use a USB > Serial adapter which is compatible with our boards voltage.

Note Icon Note:

USB to Serial adapters can be 5v or 3.3v, ensure you select the one compatible with your board, as using too high a voltage will cause damage to your board.

To wire in the USB to Serial adapter, we will simply need to wire the Rx and Tx pins, to the boards' Tx and Rx pins (so they are crossed over as board Tx goes to Adapter Rx...)

Once wired both the board and the USB Serial adapter can be plugged into your PC (and any additional drivers installed).

You will now see an additional COM port in Visual Micro for your USB to Serial Adapter, and you can have more than one Serial Window open at any one time in Visual Micro to monitor multiple ports (useful when communicating with other devices).