NPK soil sensor won't answer

Hello guys, first time here! I hope you could help me with an issue about an NPK sensor I'm working with for my degree's final project.
This is the code from this tutorial that I'm using but I keep getting the same 255 answer for all three parameters:

#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'm also using a MAX RS485 module to connect it to my arduino uno R4 wifi.
Also, as I'm a new user I'm not allowed to upload files so I'll post the datasheets download link over here. I've also checked the wiring several times and I'm perfectly sure that it's fine.

The datasheet mentions that the power supply must be between 4.5V and 30V, so I've conected the arduino's V_in to the power supply of both the sensor and the MAX RS485 module.

I've already seen quite a few posts about this kind of sensors but I have not been able to solve this issue.

There's plenty of things you've left out, like Vin = ? Arduino, which one? A schematics or diagram would help a lot.

But :+1: since you posted the code exactly as requested in the How to get the most out of... post, and linking to the datasheet.

We'll see if you show us....

How should we read this? There is one or more power supplies?

Hi, @jorkas
Welcome to the forum.
Thanks for using code tags.

Have you searched this forum for threads on the NPK sensor.
It appears to be difficult to get working and not that accurate.
You cannot really leave the sensor permanently in the ground due to electrolytic corrosion.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Hello! I'm usuing an Arduino UNO R4 wifi, and this is the schematics of this section of my project:


With the Vin I ment I'm using arduinos 5V pin for the circuit and not external power supplies.

Thank you! And yes... I've read about it, but it was too late for me as I already bought the sensor and it's implementation has become an important part for my project's goals.
Btw I've uploaded the diagram of my circuit so let me know if you need more!

Your wiring looks ok.
I didn't open the zip file you linked for documentation (for security reasons).
Anyway, I have seen (too) many of them and wouldn't be surprised if the default baud rate for your sensor is 4800 instead of 9600. Try at least.

I´ve been granted the "Basic" status so I´m now allowed to upload files. Here you have the datasheet.
NPK-S (3Pin probe) manual_V1.4.pdf (369,5 KB)
Indeed my sensor is 4800, but I've already tried that and it keeps printing the same.

I know it says the sensor can run from 5 V, but have you tried higher voltage?

No, I have not. I will try next week in the lab.

Compare NPK readings in soil versus reading in amended soil, or air These devices are considered fraudulent.

Yes, if that degree is not just electronics you should consider also that this sensor is not capable of measuring real NPK levels.
Some vendors even honestly describe it, most don't...

How much water did you add to your soil sample and did you mix thoroughly?

How much time did you spend on the data sheet/documentation for the sensor? Obviously the author of your tutorial did not understand the documentation.

The documentation for the sensor tells us to first use the usual scientific methods to determine the actual NPK values for the soil being tested.

Second, download those values to the sensor controller.

Third, use the variation point values from the sensor to get the points differences from the standard values that you downloaded previously.

Perhaps the sensor used in the tutorial is NOT the sensor you have the documentation for!

The issue right now is that I’m not getting any kind of response from the sensor, so I can’t compare it with anything. Everything seems to be fine but it isn’t.

My project is purely about electronics therefore what really matters to me is the implementation of this kind of devices. So even if the sensor doesn’t really meassure exact NPK levels at least I could use them in an orientative manner along with other sensors.

Post a picture of your wiring.

Verify the Arduino works.

Im testing it in a regular plant of my house, so I expect te water to be distributed evenly all over the soil.

Posted it in post#5! The arduino is working well as other sensors connected to it are doing fine.

"That" is neither a readable drawing nor a picture to verify your wiring. Part of your problem is not reading instructions.