MAX485 TTL to RS-485 Modules for soil NPK sensor

@markd833 @floppyoffritz The only Accurate reading is temperature and I can tell because I compared the readings with an actual temperature sensor device and the difference was -0.5C

@bright626 we are now at the limit of my knowledge of this type of sensor. I know how to sort out the comms with it, but whether the readings are correct or not is beyond my area of expertise.

Clearly temperature can be checked and you've verified that against another temperature sensor.

Whether moisture can be tested by putting the probes in a glass of water and then drying them off to get 2 readings is not something I can comment on.

Same with pH - would putting the probes in a glass of vinegar or sticking them into a lemon prove anything?

The obvious check, and probably not something you may have access to, is either another user of this type of sensor that you know of in your local area who can compare readings with you, or some reference soils. I don't know enough (actually very little!) about soils to know if various types of compost could be used as a crude way of determining anything useful or not.

@markd833 Thank you for your feedback. I'm really grateful for your help. So I already had the 7in1 NPK sensor from JXCT which worked fine for me. I took 10 samples of soil, labeled them accordingly and recorded every sample reading on an Excel sheet. I have been using that data to verify if the readings I am getting from this new sensor is correct or not. At the time, I had access to a soil sensor that measures the 7 parameters from the same company, JXCT. Since the sensor I had at that time was the same sensor that came with the soil sensor device, I compared the two readings i.e my code and that of the soil sensor device and they were very similar so that was a success. I tried ordering others from a shop on Alibaba that claims the sensor is from JXCT and I ended up with this one :smiling_face_with_tear: The image below is the device I compared my previous readings with

@markd833 So I have data that can help me verify if my readings with this sensor is accurate or not. Getting this sensor to work is the problem. If anyone got this sensor working, please share your results if possible. The sensor I have works with 4800 baud rate.

From your tests that you have carried out so far, I can see that electrically the sensor is responding correctly to requests over the RS485 bus.

If the sensor doesn't appear to be giving the expected readings, then I wonder if there is a calibration issue. Some NPK user manuals that I have seen have had additional registers documented that hold calibration information.

It could be that almost all NPK sensors have those registers, but the manufacturer (or seller of a re-badged one) doesn't want to document this in case the end user overwrites these register values.

It could also be possible that your sensor has slipped through a step in the production process and has not been calibrated, resulting is the readings you are seeing. I'm not saying that this is actually the case, but it's possible.

Do you have the option of returning the sensor as faulty?

2 Likes

Hi Bright!

I would be very interested seeing your documentation you have developed on the excel sheet for identifying reliability. Keep in mind I am not qualified in soil monitoring.

From my experience, the sensor does not operate out of soil which is very confusing. The temperature and other values are only sent when the probes are in moist soil. Putting it straight into water resulted in a pH reading, but a faulty humidity reading of 7%.

I have provided my documentation below so you can see if it really is the same sensor.

Some things to try in the mean time:

  • Put the prongs of your sensor into moist soil - then look at the readings coming through

  • "That's the configuration I have been using with my initial sensor from JXCT which operated at a baudrate of 9600. This sensor rather operates on 4800 baudrate." - this sentence confused me a little, try using different baud rates and seeing if the values change. Oddly, it was easier for me to debug everything if my board was set to 4800 as well.

  • The parsing of values received could also be in a different order. If you have previous data on soil readings, comparing them to each-other and finding out: eg. oh pH is actually EC or so and so. (This happened with me)

Good Luck!

: )

NPK type (5Pin probe) manual_V1.4.pdf (483.2 KB)

@markd833 Thanks for your reply. I have put in the arrangement to return the sensors but I haven't had any feedback yet. I have made another purchase but this time I made sure that I ordered from JXCT so that I can get the exact sensor I was using before I got these ones. And yes it is possible that my sensors could be faulty. Weird thing is I bought three of them and they all behave the same way. However I recently obtained new information that the issue could be related to the the max RS485 -TTLmodule I am using due to a voltage level mismatch. The creator I got this information from recommended the max 13487 for that particular sensor and his sensor operated on 4800 baudrate. His sensor was very identical to mine and he used a 7 in 1 version in another video of his. Here's a link to his youtube channel: Link

Thank you @floppyoffritz . I will try out your methods and provide feedback

I watched some of the youtube video - the auther appears to say that there is an issue with the voltage levels (5min 10sec). I think they say that the MAX485 has different voltage levels but I'm not sure of what he is talking about.

The datasheets for the 2 devices both indicate operation from a 5V supply and the drive voltages out of A & B are the same.

If your NPK sensor is responding with valid messages, then it's not an issue with the type of RS485 module you are using. The type of module you use cannot affect the digital values coming from the sensor, otherwise the message would become corrupted and the CRC checksum would fail.

It's possible that you have received a batch that has poor calibration data stored in it, or maybe the whole calibration process was missed out on a batch.

The 2 common baud rates for the NPK sensor that I have seen are 4800 and 9600 baud - but that can sometimes be changed if the documentation says so.

Hi, can you share the code to use npk sensor with esp32? I have problems with it :

mohammedi.lafta@st.tu.edu.iq

You need to tell us which NPK sensor you have. There are several different ones out there that have differing measurement capabilities and differing register addresses.

Hi @HPsunil . Can you please share the sensor monitoring software you recieved from JXCT with me?

Can anyone help me, I'm getting 0 value for everything, I don't know what else to do
image

#include <SoftwareSerial.h>

#define RE 4
#define DE 3

const uint32_t TIMEOUT = 500UL;
byte values[21]; // Array to store response bytes
SoftwareSerial mod(6, 5);

const byte readAllParamsRequest[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};

void setup() {
  Serial.begin(9600);
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  delay(500);
}

void loop() {
  byte val1, val2, val3, val4, val5, val6, val7;

  // Send a request to read all parameters together
  readModbusValue(readAllParamsRequest, 21);

  Serial.print("Humidity: ");
  val1 = (values[3] << 8) | values[4];
  Serial.print(val1 / 10.0);
  Serial.println("%");

  Serial.print("Temperature: ");
  val2 = (values[5] << 8) | values[6];
  Serial.print(val2 / 10.0);
  Serial.println(" °C");

  Serial.print("Conductivity: ");
  val3 = (values[7] << 8) | values[8];
  Serial.print(val3);
  Serial.println(" uS/cm");

  Serial.print("PH: ");
  val4 = (values[9] << 8) | values[10];
  Serial.println(val4 / 10.0);

  Serial.print("Nitrogen (N): ");
  val5 = (values[11] << 8) | values[12];
  Serial.print(val5);
  Serial.println(" mg/kg");

  Serial.print("Phosphorus (P): ");
  val6 = (values[13] << 8) | values[14];
  Serial.print(val6);
  Serial.println(" mg/kg");

  Serial.print("Potassium (K): ");
  val7 = (values[15] << 8) | values[16];
  Serial.print(val7);
  Serial.println(" mg/kg");

  Serial.println();

  delay(5000);
}

void readModbusValue(const byte* request, byte numBytes) {
  uint32_t startTime = 0;
  uint8_t byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(request, 8);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while (millis() - startTime <= TIMEOUT && byteCount < numBytes) {
    if (mod.available()) {
      values[byteCount++] = mod.read();
    }
  }
}

my sensor is the 7 in 1, from cwt-soil, could someone help me, as it is a college project
manufacturer's manual:

dejavu..

~q

Hi there,
did you figured to solve this error?

I am using the following code of yours and esp32:

#include <SoftwareSerial.h>
#include <Wire.h>
#define RE 33
#define DE 32

const uint32_t TIMEOUT = 500UL;
byte values[21]; // Array to store response bytes

SoftwareSerial mod(26,27); //RO,DI

const byte readAllParamsRequest[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};

void setup() {
  Serial.begin(4800);
  mod.begin(4800);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  delay(500);
}

void loop() {
  byte val1, val2, val3, val4, val5, val6, val7;

  // Send a request to read all parameters together
  readModbusValue(readAllParamsRequest, 21);

  Serial.print("Humidity: ");
  val1 = (values[3] << 8) | values[4];
  Serial.print(val1 / 10.0);
  Serial.println("%");

  Serial.print("Temperature: ");
  val2 = (values[5] << 8) | values[6];
  Serial.print(val2 / 10.0);
  Serial.println(" °C");

  Serial.print("Conductivity: ");
  val3 = (values[7] << 8) | values[8];
  Serial.print(val3);
  Serial.println(" uS/cm");

  Serial.print("PH: ");
  val4 = (values[9] << 8) | values[10];
  Serial.println(val4 / 10.0);

  Serial.print("Nitrogen (N): ");
  val5 = (values[11] << 8) | values[12];
  Serial.print(val5);
  Serial.println(" mg/kg");

  Serial.print("Phosphorus (P): ");
  val6 = (values[13] << 8) | values[14];
  Serial.print(val6);
  Serial.println(" mg/kg");

  Serial.print("Potassium (K): ");
  val7 = (values[15] << 8) | values[16];
  Serial.print(val7);
  Serial.println(" mg/kg");

  Serial.println();

  delay(5000);
}


void readModbusValue(const byte* request, byte numBytes) {
  uint32_t startTime = 0;
  uint8_t byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(request, 8);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while (millis() - startTime <= TIMEOUT && byteCount < numBytes) {
    if (mod.available()) {
      values[byteCount++] = mod.read();
    }
  }
}

but the readings that I am getting are:
image
The water is soapy water, I don't think so the humidity and ph values are correct

thirdeye11197@gmail.com Kindly share me the mail

I had similar problems. When I disconnected the GND from the RS485 from the "circuit GND" everything worked !

pramanaputra375@gmail.com