RS485 Soil NPK Sensor Calibration

Hi everyone,

I'm currently working on a project that involves using an NPK sensor via RS485 Modbus communication. Based on the sensor's user manual, the calibration formula for the sensor is Y = AX + B,

where:
Y is the calibrated value (the output I need after calibration),
X is the raw reading from the sensor,
A is the coefficient (slope) for each parameter (N, P, K),
B is the calibration offset for each parameter (N, P, K).

Also, according to the manual, I can access the coefficient and calibration values from specific Modbus registers:

  • Nitrogen coefficient (high byte: 0x04E8, low byte: 0x04E9),
  • Nitrogen calibration value (0x04EA),
  • Phosphorus and Potassium have similar registers for their coefficients and calibration values.

I am using an Arduino to read the raw sensor data and communicate with the sensor via Modbus. I’m a bit unsure about the best way to incorporate this calibration formula into my code. Could someone help me apply the calibration formula to adjust the sensor readings?

Code:

#include <SoftwareSerial.h>
#include <Wire.h>

// RE and DE Pins set the RS485 module
// to Receiver or Transmitter mode
#define RE 8
#define DE 7

// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};

// A variable used to store NPK values
byte values[11];

// Sets up a new SoftwareSerial object
// Digital pins 10 and 11 should be used with a Mega or Mega 2560
SoftwareSerial mod(2, 3);
//SoftwareSerial mod(10, 11);
 
void setup() {
  // Set the baud rate for the Serial port
  Serial.begin(9600);

  // Set the baud rate for the SerialSoftware object
  mod.begin(9600);

  // Define pin modes for RE and DE
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  
  delay(500);
}
 
void loop() {
  // Read values
  byte val1,val2,val3;
  val1 = nitrogen();
  delay(250);
  val2 = phosphorous();
  delay(250);
  val3 = potassium();
  delay(250);

  // Print values to the serial monitor
  Serial.print("Nitrogen: ");
  Serial.print(val1);
  Serial.println(" mg/kg");
  Serial.print("Phosphorous: ");
  Serial.print(val2);
  Serial.println(" mg/kg");
  Serial.print("Potassium: ");
  Serial.print(val3);
  Serial.println(" mg/kg");
  
  delay(2000);
}
 
byte nitrogen(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.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] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}
 
byte phosphorous(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(phos,sizeof(phos))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}
 
byte potassium(){
  digitalWrite(DE,HIGH);
  digitalWrite(RE,HIGH);
  delay(10);
  if(mod.write(pota,sizeof(pota))==8){
    digitalWrite(DE,LOW);
    digitalWrite(RE,LOW);
    for(byte i=0;i<7;i++){
    //Serial.print(mod.read(),HEX);
    values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
  }
  return values[4];
}

I have no agricultural experience/knowledge myself, but the general consensus on the forum is that those NPK sensors don't do what they say they do. There are forum members with agricultural knowledge who have stated in the past that it's not possible to measure the N,P & K content of the soil using some metal probes stuck in the ground.

Regarding the calibration - is it not all taken care of for you? If the slope and offset values are programmed into the "sensor", then I would expect the sensor to take these into account and output the corrected value based on them.

3 Likes

Indeed, the only way to get meaningful N, P and K soil readings is chemical analysis, performed by your local soil testing lab, for example.

The metal probe sensors measure soil conductivity and make up some numbers for N, P and K.

3 Likes

Lots of folks say the device is a scam.

The device appears to measure conductivity and do some transform of that to give the numbers.

Given a carefully controlled experimental system where you hold moisture, N, P, etc all constant, you could measure conductivity as you vary K concentration, plot a graph and get a y_K=A_k*x+b_k calibration. But if moisture changes, or any of the other variables in the system change, or you sprinkle in a little salt to increase conductivity and change x, you can't tell whether the change you see in the results of the y_k is a result of the conductivity X being changed by changes in moisture, N, P, K, salinity, etc..

It is as if a person's height was correlated with their age, weight, sex, nutrition, country, and socio-economic status, and you measured someone's height as 1.600m. Could you reliably predict their age, sex, weight, sex, nutrition country, and wealth all from the one number?

Since they are all Y = AX + B calibrations, where X is conductivity, you could just read conductivity and calibrate your own As and Bs inside the Arduino.

2 Likes

@DaveX indeed. There's just no way to get the value of 3 independent variables from 1 measurement of a dependent variable. If that were possible, the variables wouldn't be independent and there would have to be a confounder that predicts all 3. Since N, P and K levels are not associated and are only related very weakly (through choice of salts dissolved, solubility limits etc.), there's no way to compute N, P and K levels from just a conductivity measurement. There's just not enough data, and that problem is fundamental.

I recently took a look at the "official manual" for the NBL-S-TMC-7/7-in-1 Soil Integrated Sensor by Changsha Zoko Link Technology Co., Ltd to see what the manufacturer actually says about N, P and K, and as expected, it doesn't make much sense.

Here is the actual text, from which one might guess that the parameter "x" for N or P or K might come from conductivity measurements using different combinations of the 5 metal electrodes, respectively.

C. Changes in nitrogen, phosphorus and
potassium coefficients
The coefficients are in the form of ax+b, multiples
of a and constants of b. For nitrogen,
phosphorus, and potassium, a all defaults to
0064 (100 which is 1.00 times) and b all defaults
to 0000 (which is 0)

The manufacturer does allow one change those calibration constants, suggesting that the delusion runs deep.

1 Like

I tried to find a something about the calibration of these sensors, and the best I could find was https://www.researchgate.net/publication/380035256_Exploration_of_electrode_materials_for_in-situ_soil_fertilizer_concentration_measurement_by_electrochemical_method But it finds that conductivity correlates with varying concentration in some mixed-chemistry test plots and presents stuff like this:

If you measure 36mV with graphite electrodes, what are the concentrations? 4g of N, 3g P, 50g K? And then they show graphs of there experiments where the concentrations all vary by less than a gram. I think there's a hidden layer of re-calibration for each site where they do another y=Ax+b for each 90 day run in each plot, and all you end up seeing is that N,P,K and conductivity all decrease together. If you were trying to use this process, you'd need to measure the N,P,K in a chemistry lab at the beginning and ending of deployment, and then fit the y=Ax+B models for each conductivity model.

Since the Dow Jones Industrial Average and the S&P500 are correlated at 0.95, I can sell you a model that predicts the S&P500 or the DJIA 95% (if you happen to be able to predict the other one) Same thing with the NPK sensors -- If you correlate the actual N,P,K and conductivity over the period of interest, you can correlate them with each other and model them all off of conductivity to the extent they are correlated.

1 Like

The problem with N and P is that these elements are present in soil in many different chemical combinations, some of which are efficiently utilized by plants and others that are not. "N" is by far the most variable, and some of those combinations are not ionic and thus do not contribute to soil conductivity.

N2 gas is the best example, which is very abundant, very nonreactive and useless to most plants. However, with the help of special bacteria, "nitrogen fixing" plants can extract N2 from the environment and convert it into forms that are useful to other plants.

No one sensor can adequately measure or quantitate "plant-available N", which is why chemical soil analysis by trained technicians is required.

This is an important problem, and a lot of companies, along with some academic chemists, are working on designing new technologies and detection methods that might solve it, but a couple of wires stuck in the ground are not going to do it.

1 Like

They don't have to. If they can show a correlation in one lab experiment, even if the correlation is to some third process like dilution, then they can sell the causation scam. Even sell it multiple times in a 7-in-1 sensor.

For direct uptake, ammonium and nitrate are most important. But amino acids, etc. are eventually available to plants via bacterial action, so those species should in theory be taken into account.

I challenge anyone to tell potassium from sodium

Excellent point!

Some time ago I had several discussions with an organic chemist friend who is developing molecular cages as fluorescent indicators of bound species, and is interested in this topic.

He told me that all the major ag companies are putting a major effort into machine learning experiments, testing and cataloging soils all over the nation, in the hopes that a few simple measurements like pH, concentrations of selected metal ions, conductivity, etc. along with precise geographic location, might work to predict "plant available nutrients" with useful accuracy.

Agree completely!

1 Like

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