SerialSoftware serial: Nano + Sensor + RS485-to-TTL module problem

Hi there,

I’Ve got a problem - and I think I might be quite close to the solution.

So I have an Arduino Nano to whom a pressure sensor ist attached. This works and is not the issue. Just mentioning it for “completeness”.

So my acutal problem is the readout of a SMT100 soil moisture sensor with the Nano.

The SMT100 can be accessed via RS485, so: SMT100 → RS485-to-TTL → Arduino Nano.

To read out I use the following code, below:

// Test File für SHT25 + MPL3115

#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include <Sodaq_SHT2x.h>
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();

#include <Arduino.h>
#include <SoftwareSerial.h>

#define TX 1
#define RX 2
#define CONTROL 13

boolean RCV_ready = LOW;
boolean SND_ready = HIGH;
SoftwareSerial mySerial(TX, RX);

String str;


void setup()
{
 
  Wire.begin(); // for pressure sensor
  Serial.begin(9600); 
  pinMode(CONTROL, OUTPUT); // Pin "Conrol" is in output mode, i.e. it will send to the RS485 module
  digitalWrite(CONTROL, RCV_ready); // R485 module in recieve mode
  mySerial.begin(9600);
}

void loop()
{
// Begin pressure sensor
   if (! baro.begin()) {
    Serial.println("Couldnt find sensor");
    delay(500);
  }
   
  //READ MLP3115
  float pascals = baro.getPressure();
  float tempC = baro.getTemperature();
  
  Serial.print(pascals/100);
  Serial.print(" "); 
  Serial.print(SHT2x.GetHumidity()); 
  Serial.print(" "); 
  Serial.print(tempC); 
  Serial.print(" "); 
  Serial.print(SHT2x.GetTemperature()); 
 
//End pressure sensor

// Begin SMT100 sensor

  if(mySerial.available() > 0){
    Serial.print(" myserial found ");
  }  

  
  Serial.print(" * ");
   
  str = mySerial.read();
  Serial.print(str);
  Serial.print(" ");
  str = mySerial.read();
  Serial.print(str);
  Serial.print(" ");
  str = mySerial.read();
  Serial.print(str);
  Serial.print(" ");
  str = mySerial.read();
  Serial.print(str);
  Serial.print(" ");
  str = mySerial.read();
  Serial.print(str);
  Serial.print(" ");
  str = mySerial.read();
  Serial.print(str);
  Serial.print(" ");
  
  Serial.println(" * ");
 
  
  delay(250);
}

// Comment section

/*
Actual response
* 57 56 55 46 50 56  * 
* 32 51 48 46 50 57  * 
* 32 50 49 46 57 52  * 
* 32 50 51 46 50 50  * 
* 32 109 121 115 101 114  * 
* 105 97 108 32 102 111  * 
*/

Now I get a response, see the “comment section” in the code, but it does not make sense to me. Also, there is no difference in the output when setting the CONTROL pin to either HIGH or LOW.

Could you help me out here?

#define TX 1
#define RX 2

Do not use pin 1 as it is used by the hardware Serial interface on a Nano

Okay, thanks.

So now I’m using a software serial. Just to clarify, I’d like to read out both sensors (pressure sensor via analog pins and the soil sensor via the SoftwareSerial).

// Test File für SHT25 + MPL3115

#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
#include <Sodaq_SHT2x.h>
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();

#include <Arduino.h>
#include <SoftwareSerial.h>

// 1 (TX) and 0 (RX) cannot be used since these are hardware serials. And are connected with USB port. Only on nano!
#define TX 7 // D7
#define RX 8 // D8
#define CONTROL 10 // D10

boolean RCV_ready = LOW;
boolean SND_ready = HIGH;
SoftwareSerial RS485Serial(TX, RX);

String str;


void setup()
{
  Wire.begin();
  Serial.begin(9600); 

  pinMode(CONTROL, OUTPUT); // Pin "Conrol" is in output mode, i.e. it will send to the RS485 module
  delay(20);
  digitalWrite(CONTROL, RCV_ready); // R485 module in recieve mode / funktioniert
  RS485Serial.begin(9600);
}

void loop()
{//Check if sensor is connected/working
   if (! baro.begin()) {
    Serial.println("Couldnt find sensor");
    delay(500);
  }
   
  //READ MLP3115
  float pascals = baro.getPressure();
  float tempC = baro.getTemperature();
  
  Serial.print(pascals/100);
  Serial.print(" "); 
  Serial.print(SHT2x.GetHumidity());  // not working // kein 
  Serial.print(" "); 
  Serial.print(tempC); 
  Serial.print(" "); 
  Serial.print(SHT2x.GetTemperature()); // not working

 //READ SMT100
  if(RS485Serial.available() == 0){
    Serial.print(" SMT100 not found! ");
    delay(500);
  }  
  
  Serial.print(" * ");
   
  str = RS485Serial.read();
  Serial.print(str);
  Serial.print(" "); 
  Serial.println(" * ");

  delay(250);
}

New problem arose: the SoftwareSerial “is not avialable” error. The print message from the SoftwareSerial is “-1”.

Any further advice?

#define TX 7 // D7
#define RX 8 // D8
SoftwareSerial RS485Serial(TX, RX);

If pin 7 (TX) is connected to Rx on the moisture sensor and pin 8 (RX) is connected to Tx on the soil sensor then you have the order of the pins wrong in the SoftwareSerial constructor

True. Also corrected.

Still "nothing".

Write a simple sketch that tests for serial data from the sensor using a SoftwareSeral object and if it is available print it to the Serial monitor

Please provide a link to the RS485 to TTL adapter and the soil moisture sensor that you are using

Hi,

solved it. I had to first send a command to the sensor, which then returns the value.

Cheers and thanks,
Robert