NPK Sensor only giving 255 as a result

Hi all. Followed the guide from this website(Arduino Soil NPK Sensor: Maximizing Plant Nutrition | ARDUINOKIT PROJECT) to use a RS485 to NPK sensor to get data into an arduino r4 wifi. However, the only results I am getting are 255. I am unsure of how to edit the code to incorporate the data from the attached data sheet. Thank you!

Attached here is the data sheet:

Attached here is the code:

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

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

// 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];
}


This is the exact same schematic as mine.

If you read the disclaimer at the website you referenced:

The JXCT soil NPK sensor is an electrical conductivity sensor (EC sensor). It does not directly measure the soil’s NPK content, but rather estimates it based on the electrical conductivity of the soil.
Be aware that this method is tricky and prone to producing inaccurate results.

you shouldn’t be surprised. It means: This sensor cannot measure NPK and there is no way to fix that. Search the forum here for reports about such “sensors” to get the full picture.

1 Like

The reason you get 255 is probably because you read and not verify if there is data available.

Please read and learn about how to use a serial port properly.

https://docs.arduino.cc/language-reference/en/functions/communication/serial/

PS, fully agree with @bfobferer these sensors are not NPK sensors at all. Someone is earning a lot of money with such “overpromising” sensors.

Where did you get the code?

Why is value[4] both N as K as P?
I would at least expect that other bytes of the 8 would stand for different elements, but OK I did not read the datasheet.

1 Like

This could very well be because different byte sequences are used for the request for N or P or K.
But overall, this whole code looks weird. Why are there exactly 7 bytes in the response? Is that a strange number? What are the other bytes for?

This has cropped up before.
N (Nitrogen), K (Potassium) and P(Phosphorous) are so diverse in properties, that I would be very sceptical of a 3-electrode sensor that says it can analyse any of them.
I certainly wouldn't place much store in any data produced.
If it's critical, it's either a lab job, test strips or specific ion electrodes.
Otherwise, it's spoon bending.

Edit
Nitrogen alone is the sum total of nitrates, nitrites, ammonia for starters.

@b707
Educated guess: (7 bytes)

  • 2 bytes for N, 2 bytes for K, 2 bytes for P and a 8 bit checksum.
  • start byte, 2 bytes for N, 2 bytes for K, 2 bytes for P.

The latter would be most robust, but as said no datasheet…

(updated)

Datasheet please …

Standard modbus response for single register is 7 bytes.

They are from separate requests, but yes OP should combine both 3 and 4 (high and low bytes).

:slight_smile: a new interesting feature for those sensors :slight_smile:

(thinking out loud)
I do not know how these devices - could - work, (thinking out loud) maybe they heat the ground and have a chemical sniffer build in, or the pins are from different metal that react differently, or the pins are hollow, or they measure specific corrosion (if that exist).

You should call mod.flush() before you set de/re LOW and wait until you have something available in the buffer before mod.read().

And 90% of similar setups here on this forum failed because of incorrect or flimsy wiring, so double check everything.

They really don't even try to "work" for NPK. The registers are actually writeable..

Description of one honest supplier:

"NPK storage.The normal use method is that you measure NPK by other methods and then write the data to the device through RS485 command.
It cannot be measured actually, because the nitrogen, phosphorus and potassium values measured by the equipment are not used as a reference. It is ok if it is only for project acceptance.
"

1 Like

So the clever folks have avoided a lawsuit. Great find, I knew it was a scam way back, just never saw the honest statement.

I suppose anything is possible.
The soil equivalent of a Star Trek diagnostic wand.
Here's Google says

How do soil NPK sensors work?
The soil NPK sensor is suitable for detecting the content of nitrogen, phosphorus and potassium in the soil, and judges the fertility of the soil by detecting the conductivity transformation caused by different nitrogen, phosphorus and potassium concentrations in the soil.

How it Works (General Principle)

  • Electrodes: The probes contain specialized alloy electrodes, often using different materials or coatings, that interact with ions in the soil solution.
  • Electrical Signal: An AC voltage or specific current is applied, and the soil's electrical properties (conductivity/resistance) change in response to N, P, and K ion concentrations.
  • Data Conversion: Internal circuitry converts these subtle electrical changes into digital values, often using Modbus RTU over RS485 for reliable communication, representing N, P, and K content.

As far as the electrodes go, another site just says stainless steel.

I may be wrong, but it just seems too good to be true. Why do soil specialists like John Innes Institute spend ÂŁm's on instrumentation when ÂŁ30 or less can do it?

I ran a laboratory for some years specialising in high quality analyses that qualified for the EC/EU Harmonised Monitoring Scheme for environmental waters testing.
We did conductivity as part of a suite of chemical and physical parameters running into hundreds.
The analysis of NPK is complex. The US Environmental Protection Agency publish standard methods of analysis, and are used globally.

Potassium is analysed by flame spectroscopy or atomic absorption spectroscopy.
Total nitrogen is analysed by converting nitrates, nitrites, organic and inorganic nitrogen, ammonia, to ammonia by reduction. Ammonia is analysed colorimetrically or by specific ion electrode.
Phosphorous can be inorganic or organic. This is more complex, but phosphorous can be determined colorimetrically. Some organic compounds like pesticides have to be analysed by gas liquid chromatography..

If you want results that stand scrutiny, you must have deep pockets.

When conductivity (water or soil) is measured, it does not discriminate the ions producing that conductivity. Sodium, potassium, lithium etc., can all give the same result. Sodium nitrate the same as potassium nitrate.

I just think there are too many possibilities for obtaining numbers without even going close to calibrating it.

But as a cautious scientist, I never say never until it's put to the test and chemical analysis has moved on. since I was involved.

agree 100% - keep an open mind but trust the facts, I have never seen a working NPK sensor.

Neither. But I wouldn't be surprised to see one in near future. Demand is good, since lab tests are PITA and on-field devices cost like luxury car.
For sure it's not gonna be "two nails in the ground"...