NPK Sensor Five-pin soil multiparameter sensors: CodeISSUE

void loop() {
  byte queryData[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};
  byte receivedData[19];
  float nit_val, phos_val, potas_val, ph_val, ec_val, mois_val;
  int loopCounter = 15;  // Counter to keep track of loop iterations with data
  const int maxIterations = 20;  
  mySerial.write(queryData, sizeof(queryData));  // Send the query data to the NPK sensor
  delay(1000);  // Wait for 1 second

  if (mySerial.available() >= sizeof(receivedData)) {  // Check if there are enough bytes available to read
    mySerial.readBytes(receivedData, sizeof(receivedData));  // Read the received data into the receivedData array
    // Parse and print the received data in decimal format
    unsigned int soilHumidity = (receivedData[3] << 8) | receivedData[4];
    // unsigned int soilTemperature = (receivedData[5] << 8) | receivedData[6];
    unsigned int soilConductivity = (receivedData[7] << 8) | receivedData[8];
    unsigned int soilPH = (receivedData[9] << 8) | receivedData[10];
    unsigned int nitrogen = (receivedData[11] << 8) | receivedData[12];
    unsigned int phosphorus = (receivedData[13] << 8) | receivedData[14];
    unsigned int potassium = (receivedData[15] << 8) | receivedData[16];

    
    float moisture = soilHumidity / 10.0;
    float ec = soilConductivity / 1000.0; 
    float pH = soilPH / 10.0;
    float nitro = nitrogen / 1000.0 * 10.0;
    float phos = phosphorus;
    float potas =  potassium / 1000.0 / 39.0983 * 100.0;

I have a code here to get the Nitrogen, Phosphorus, and Potassium but I have a doubt in computation on how to get the exact value of nitrogen in percentage also in other phos, potas,ec,pH and moisture, can anybody help me this is my thesis, Our team compare the result in soils lab in our device but in the end the result is inacccurate, can somebody help me, thanks.

Do you have a user manual for your sensor? It should explain the comms parameters as well as the register addresses for the various measurements you are interested in.

The usual difficulty that appears on these forums is that the sensor doesn't respond at all. Does your code receive a response from the sensor?

The values reported for N,P & K are usually 10x the actual value to give 1 DP (i.e. divide response by 10 to get the real value), but the user manual will tell you this.

There are some forum members that seem to have agricultural knowledge and it is their opinion that these 3-pin (or 5-pin) NPK sensors do not work. I've no experience of the actual measurement side of this sensor so I can't comment either way. I simply try and help out with the comms side.

Those sensors estimate soil conductivity, and make up numbers for values of N, P and K.

That was exactly the right thing to do, and all you needed to know.

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