Friday, November 11, 2022

Multi-serial with software serial on Arduino Uno, Nano, etc

A problem an Arduino novice may face is the inclination to connect to a serial device (such as a Loadstar Loadcell read by their proprietary serial interface or a Benewake TF-LC02 Lidar Sensor) to an Arduino board with only one hardware serial interface (such as Arduino Uno or Nano) and then open the Arduino IDE Serial Monitor with hopes of using it to communicate with the serial device. This novice would (hopefully) notice right away that things aren't going as hoped. 

This post explains why this doesn't work.  My own experience is that I could send to the device but not get anything back, which is a little different that what is posted in that link, though I make no claim to knowing why.

The best solution allowing connection of Serial Monitor for input/output to a serial device, in my opinion, is to use an Arduino board with multiple hardware serial interfaces, such as an Arduino MEGA2560. The sample code here allows connection of a serial device to a second serial interface, relays all input and output to the Serial Monitor and vice versa.  This allows for testing of commands to the device, and a more involved sketch can be built out from there.

Unfortunately, the notional novice may be disinclined to buy new hardware.  Arduino does have at least one library for implementing a serial interface via software.  The code below adapts the code from the link above for use with boards with only one hardware serial interface, by using the Arduino Software Serial Library.  

The code:
  /*
  Multiple serial test with software serial

  Receives from the main serial (Serial 0), sends to software serial port configured in the code.
  Receives from software serial port, sends to the main serial port.

  This example works with boards with only one serial like Arduino Uno, Nano etc.

  The circuit:
  - any serial device attached to software serial pins defined below
    -device tx connects to Arduino pin 2
    -device rx connects to Arduino pin 3
    -device GND connects to Arduino GND
    -use own discretion about powering device (Device Vcc to 5v Arduino pin if safe)

  - Serial Monitor open on Serial port 0

  Adapted 10 Nov 2022 from Arduino multi serial code found at:
  https://docs.arduino.cc/built-in-examples/communication/MultiSerialMega.

  Original code:
  created 30 Dec 2008
  modified 20 May 2012
  by Tom Igoe & Jed Roach
  modified 27 Nov 2015
  by Arturo Guadalupi

  This example code is in the public domain.
*/


#include <SoftwareSerial.h>
const byte SSrx = 2;
const byte SStx = 3;

SoftwareSerial SoftSerial (SSrx, SStx);

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  pinMode(SSrx, INPUT);
  pinMode(SStx, OUTPUT);
  SoftSerial.begin(115200);
}

void loop() {
  // read from software serial, send to built in serial:
  if (SoftSerial.available()) {
    int inByte = SoftSerial.read();
    Serial.write(inByte);
  }

  // read from built in serial, send to software serial:
  if (Serial.available()) {
    int inByte = Serial.read();
    SoftSerial.write(inByte);
  }
}
My experience is that the software serial connection generally works OK, but can't be relied upon 100% of the time.  Some bytes (or whole strings of bytes) may come through as the wrong characters, which is why I suggested the better solution is to use a board with more than one hardware serial connection.

Still, the sketch above may meet the novice's needs for demonstrating the serial connection and testing out a few commands.  Wiring should be as follows:
  • Device TX to Arduino pin 2
  • Device RX to Arduino pin 3
  • Device GND to Arduino GND pin
If the device accepts 5V and current draw is within the limits of what can be supplied by the Arduino board, and powering from the Arduino is desired, then also connect
  • Device Vcc to Arduino 5V pin
Below shows an Arduino Uno running the sketch above connected to an Arduino MEGA2560 (clone) using multi serial.

Output is shown in the screen capture below.  


As can be seen, the Arduino Uno seems to send just fine, but its receiving leaves something to be desired.

No comments:

Post a Comment