How to read sensor values using the Arduino MKR 485 Shield?

Hello Arduino Community,

Perhaps with a little help of y'all I can get pointed into the right direction.

I'm trying to build an agricultural IoT sensor, but I can't seem to connect the sensor to the Arduino and read the values correctly.

IDE/libs:

  • Visual Studio Code with PlatformIO

  • Arduino.h

  • ArduinoRS485.h

Components:

Circuit Diagram:
(* in this diagram the GSM board is backwards)

Code:
(* to simplify the code, I'm only showing what I've written for 1 of the 7 measurements: nitrogen)

#include <Arduino.h>
#include <ArduinoRS485.h>

#define RE A5
#define DE A6

const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xB5, 0xCC};

byte values[11];

byte nitrogen()
{
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (RS485.write(nitro, sizeof(nitro)) == 8)
  {
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++)
    {
      //Serial.print(mod.read(),HEX);
      values[i] = RS485.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  return values[4];
}

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

  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
}

void loop()
{
  RS485.beginTransmission();
  byte val1;
  val1 = nitrogen();
  RS485.endTransmission();
  delay(250);

  Serial.print("Nitrogen: ");
  Serial.print(val1);
  Serial.println(" mg/kg");
  delay(2000);
}

Result:

Screen Shot 2021-09-23 at 19.43.28

I keep getting a value of 255 mg/kg and this result shows even if I have the sensor "unplugged" which tells me that it is code based instead of actually showing the sensor value.

Any help would be greatly appreciated.

Thanks!
CG

** Sensor datasheet:
RS485-Soil Multi-parameter Sensor.pdf (352.1 KB)

A pretty picture but not much help. You should post a schematic showing all connections and links to the technical information on the hardware parts. Hint: Look up termination on RS485 networks. In your case the network must be terminated to avoid data corruption and unwanted reflections. You will find the termination resistors need to be at the physical ends of the bus. 120 Ohm would be a good starting point. Good luck and let us know how you do.

Hi gilshultz,

I'm sorry but I didn't understand you, where exactly do I have to put the resistor? I thought the MKR shield was meant to just plug itself into GSM.

I didn't attach the schematics because the only componentes in the circuit (besides the sensor) are Arduino. I'm attaching both schematics now though.

MKRGSM_V2.3_sch.pdf (1.6 MB)
MKR485ShieldV3.0_sch.pdf (44.6 KB)

Thanks!
CG

Apparently you have not had time to look up termination on RS485 networks, in that you should find your answer. Your schematics are for the components but they tell us nothing on how you have connected the parts and how you have powered them. The schematic I am looking for is how you actually wired it.

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