NPK Sensor to Arduino Mega 2560 Keeps Returning 255

First of all, before using the Arduino Mega 2560, I used the Arduino UNO to try the NPK sensor. The Arduino UNO has a wiring schematic like the picture below:

Then the code that I use is as below:

#include <SoftwareSerial.h>
const byte code[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
byte values[11];
SoftwareSerial mod(21, 20);
int nitrogen;
int phosphorous;
int potassium;

struct MyVariable
{
  byte nitrogen;
  byte phosphorous;
  byte potassium;
};
MyVariable variable;
 
void setup()
{
  Serial.begin(4800);
  mod.begin(4800);
}
 
void loop()
{
  byte val;
  delay(10);
  if (mod.write(code, sizeof(code)) == 8)
  {
    for (byte i = 0; i < 11; i++)
    {
      //Serial.print(mod.read(),HEX);
      values[i] = mod.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  nitrogen = values[4];
  phosphorous = values[6];
  potassium = values[8];
  delay(1500);

  variable.nitrogen = nitrogen;
  variable.phosphorous = phosphorous;
  variable.potassium = potassium;
  delay(1500);

  Serial.print("Nitrogen: ");
  Serial.print(variable.nitrogen);
  Serial.println(" mg/kg");
  Serial.print("Phosphorous: ");
  Serial.print(variable.phosphorous);
  Serial.println(" mg/kg");
  Serial.print("Potassium: ");
  Serial.print(variable.potassium);
  Serial.println(" mg/kg");
  delay(10000);
}

When using the Arduino UNO there are no problems at all. The program code can run properly and produce NPK measurement results. However, when I changed the board to Arduino Mega 2560, a problem arose, namely the value that appeared became 255. On the Serial monitor, initially it appeared FFFFFFFFFF and continued to issue 255. Meanwhile, on the Arduino UNO, numbers like 12351231 initially appeared, then continued with the NPK measurement value.

I've tried changing pins 2 and 3 to pins 18,19 then 16,17 then 14,15. However, it still produces output 255. Is there a solution to my problem. Thank you

The schematic is missing the RS485 ground connection (it is a three wire connection).

@kevinar-18 you've also asked your question here:
https://forum.arduino.cc/t/max485-ttl-to-rs-485-modules-for-soil-npk-sensor/853077/170

You should stick to one discussion, otherwise the people trying to help you could end up duplicating effort.

HI, i need help. First of all, before using the Arduino Mega 2560, I used the Arduino UNO to try the NPK sensor. The Arduino UNO has a wiring schematic like the picture below:

Then the code that I use is as below:

#include <SoftwareSerial.h>
const byte code[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
byte values[11];
SoftwareSerial mod(21, 20);
int nitrogen;
int phosphorous;
int potassium;

struct MyVariable
{
  byte nitrogen;
  byte phosphorous;
  byte potassium;
};
MyVariable variable;
 
void setup()
{
  Serial.begin(4800);
  mod.begin(4800);
}
 
void loop()
{
  byte val;
  delay(10);
  if (mod.write(code, sizeof(code)) == 8)
  {
    for (byte i = 0; i < 11; i++)
    {
      //Serial.print(mod.read(),HEX);
      values[i] = mod.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  nitrogen = values[4];
  phosphorous = values[6];
  potassium = values[8];
  delay(1500);

  variable.nitrogen = nitrogen;
  variable.phosphorous = phosphorous;
  variable.potassium = potassium;
  delay(1500);

  Serial.print("Nitrogen: ");
  Serial.print(variable.nitrogen);
  Serial.println(" mg/kg");
  Serial.print("Phosphorous: ");
  Serial.print(variable.phosphorous);
  Serial.println(" mg/kg");
  Serial.print("Potassium: ");
  Serial.print(variable.potassium);
  Serial.println(" mg/kg");
  delay(10000);
}

When using the Arduino UNO there are no problems at all. The program code can run properly and produce NPK measurement results. However, when I changed the board to Arduino Mega 2560, a problem arose, namely the value that appeared became 255. On the Serial monitor, initially it appeared FFFFFFFFFF and continued to issue 255. Meanwhile, on the Arduino UNO, numbers like 12351231 initially appeared, then continued with the NPK measurement value.

I've tried changing pins 2 and 3 to pins 18,19 then 16,17 then 14,15. However, it still produces output 255. Is there a solution to my problem. Thank you

The most likely explanation is that your code isn't receiving any data back from the sensor. Part of the problem is that this bit of code:

That bit of code (or code very similar) can be found all over the web and it is simply wrong! It doesn't check to see if there is a response from the sensor. It just assumes that there is a response instantly available and blindly reads in values. The software serial port is trying to tell you that there is no data by returning -1 - which is 0xFF - for every mod.read call, which is why you see lots of FFFFFFFF being printed.

You don't need to use any software serial code on a MEGA2560 - it has 4 hardware serial ports. Use Serial1 for your modbus comms and the pins TX1 and RX1.

My other piece of advice would be to move away from the horrible canned modbus messages and use a proper modbus library which will help in the long run. This library should work for you:

Oh and you may find that the code you think is working on your UNO isn't quite doing what you think it is. You may find that the parameter values you print out are mixed up - i.e. the value you think is Nitrogen is actually Phosphorous, and the value for Phosphorus is actually Potassium etc.

@kevinar-18,

Please create one topic only and carefully select the forum category. Please do not add your question to the end of someone else's topic. If you continue to do either of these things you risk a time out from the forum. I have merged your 2 topics here.

Please read the forum guide: How to get the best out of this forum

Thank you.

\Should i connect the ground to arduino?

Alright, i'm sorry before

Ok i will try using the modbusmaster

Already done. But you forgot to connect the MAX13847 GND to the sensor GND.

I have connected the MAX13847 GND to the sensor GND, but nothing happen. The results are still the same as before

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