SoftwareSerial with Arduino Nano

I've got a project that I'm working on and need to control a relay via serial commands from an audio DSP. I've written the code for a Nano and, using the IDE serial monitor, can send the commands and the relay responds correctly.

I assume though, that I need to use a "second" serial interface on the nano so that I can use the serial monitor to test and debug the code. I've been messing with SoftwareSerial, but it's acting strange. I've tried sending serial commands to the nano (to the SoftwareSerial designated pins) with a USB-> Serial (DB9) adapter, and then wired from the DB9 to the nano. And I've also tried with the Audio DSP. In both cases, I get consistent but inaccurate data. For example, if I send "OFF\n", I see 58 FFFFFFAE FFFFFFAE 3D. Every time. If I send "ON\n", I see 58 FFFFFFAC 3D. So the \n is coming across as 0x3D, and the letter 'O' is always 0x58.

Here's the code:

#include <SoftwareSerial.h>

// constants won't change
const int RELAY_PIN = 8;          // the Arduino pin, which connects to the IN pin of relay
const int MOMENTARY_TIME = 100;
const int CORE_RX_PIN = 2;
const int CORE_TX_PIN = 3;
const int CORE_BAUD = 9600;

// Data we get from the serial interface
String rxData;

// Software serial endpoint
SoftwareSerial coreSerial(CORE_RX_PIN, CORE_TX_PIN); // RX, TX

//
// setup()
// Runs once when you press reset or power the board
//
void setup() {
  
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }
  
  Serial.println("coreSerialRelay - starting up");

  // set the data rate for the SoftwareSerial port
  coreSerial.begin(CORE_BAUD);
  coreSerial.println("ready");
  
  // initialize digital pin as an output for the relay
  pinMode(RELAY_PIN, OUTPUT);

  Serial.println("ready");

  relayMomentary();
}

//
// loop()
// Runs over and over again forever
//
void loop() {
  coreSerial.listen();

  while (coreSerial.available() > 0) {
      char received = coreSerial.read();
      Serial.print(received, HEX);
      Serial.print(" ");
          
      rxData += received; 

      // Process message when new line character is recieved
      if (received == '\n')
      {
          Serial.print("Arduino Received: ");
          Serial.print(rxData);
          parseData();
         rxData = ""; // Clear recieved buffer
      }
  }
}

//
// parseData()
// Parse the serial info we just recieved and respond
//
void parseData() {
  // Uppercase the string - keep it easy on the use
  rxData.toUpperCase();

  // Turn on the relay
  if (rxData == "ON\n") {
    Serial.println("on");
    relayClose();
    coreSerial.println("OK");
  }

  // Turn off the relay
  if (rxData == "OFF\n") {
    Serial.println("off");
    relayOpen();
    coreSerial.println("OK");
  }

  // Momentary on
  if (rxData == "MOMENTARY\n") {
    Serial.println("mom");
    relayMomentary();
    coreSerial.println("OK");
  }

  // User wants current status
  if (rxData == "STATUS\n") {
    if (digitalRead(RELAY_PIN)) {
      coreSerial.println("ON");
    }
    else {
      coreSerial.println("OFF");
    }
    coreSerial.println("OK");
  }
}

//
// relayClose()
//
void relayClose() {
  digitalWrite(RELAY_PIN, HIGH);
}

//
// relayOpen()
//
void relayOpen() {
  digitalWrite(RELAY_PIN, LOW);
}

//
// relayMomentary
//
void relayMomentary() {
  digitalWrite(RELAY_PIN, HIGH);
  delay(MOMENTARY_TIME);
  digitalWrite(RELAY_PIN, LOW);
}

What is that? Post a link to the product page.

If it is an RS232-compatible adapter, then the voltage levels are wrong for Arduino and will damage the Arduino.

According to one question on the product page (Amazon), someone measured 6.2v for this unit. I've got another Nano I can try since now I'm straight from the DSP (no USB adapter in play).

Same results with the new board. The DSP uses only COM, RX, TX

That cable is for a USB connection to a computer or device with an old-fashioned RS-232 serial port, which operates on positive and negative voltages. It will not work with Arduino.

For help with connecting to an audio DSP, post a link to the DSP product page. Or read the DSP operating manual.

I'm not using the USB->DB9 connector now. I'm just going straight from the DSP to the Nano (SoftwareSerial). I'm writing the code in the DSP, and I've done this quite a bit, so I'm not concerned about that. But the Arduino still shows wierdness. I've ordered an Arduino Micro, which has two hardware serial inputs, so I'll give that a shot.

The nano has ttl level. ( 5v pins). If you want to connect to a PC you need adapters to covert the voltage levels , as mentioned

UART > RS232 > USB

Or maybe the sparkfun or similar device ( which also useful for programming etc ) will do the job
Here

If the DSP has a DB9 serial connector intended for connection to a PC, then a voltage level converter is required to interface with Arduino.

It’s got a Euroblock (phoenix) connector with RX, TX and COM conductors. I’m using a QSC Q-Sys Core 110fV2.

“Q-SYS Core processors and peripherals include an RS-232 connection for extension of Q-SYS Control to third-party devices, such as projectors, TVs, and A/V receivers. You can control and read from these devices using Lua script from a Q-SYS scripting component.”

RS232 signals are typically +/- 12V.

Good luck with your project.

As @jremington pointed out, you conneced devices with incompatible interface voltages. You probably have damaged the nano and/or your QSC.

That connector is clearly labeled "RS232" on the spec sheet.

You will need an RS-232 to TTL converter to connect to any Arduino.

As has been stated several times, RS-232 uses approximately +/- 12V for the LOW/HIGH levels. -12V is HIGH logic level, +12V is LOW logic level, which is the reason you are seeing the "wierdness" on the Arduino. The RS-232 to TTL converter will convert the signal into the proper voltage range for the Arduino, and invert the logic levels so that you can properly use either hardware or software serial.

Great. Thank you. I think I have some of those. I appreciate everyone’s help here.

We have about 20 schools that need this solution (I’m an AV integrator). The Core used to have GPIO on it, but was removed due to the chip shortage. The typical solution would cost almost $200 and be overkill. I just need a momentary contact closure to put a BT RX in pairing mode.
I figure the Arduino route would be MUCH cheaper.

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