Npk-arduino mega sensor problem, only reads phosphor

hi, i have a problem where only the phosphor value is read, this is my arduino code:

#define RE 8  
#define DE 7  

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
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 };

byte values[11];

void setup() {
  Serial.begin(9600);
  Serial1.begin(4800);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  digitalWrite(DE, LOW); // Set DE and RE pins to receive mode
  digitalWrite(RE, LOW);
}

void loop() {
  byte val1, val2, val3;
  val1 = nitrogen();
  delay(250);
  val2 = phosphorous();
  delay(250);
  val3 = potassium();
  delay(250);

  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");
}

byte nitrogen() {
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (Serial1.write(nitro, sizeof(nitro)) == 8) {
    Serial1.flush();
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++) {
      //Serial.print(Serial1.read(),HEX);
      values[i] = Serial1.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  return values[4];
}

byte phosphorous() {
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (Serial1.write(phos, sizeof(phos)) == 8) {
    Serial1.flush();
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++) {
      //Serial.print(Serial1.read(),HEX);
      values[i] = Serial1.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  return values[4];
}

byte potassium() {
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (Serial1.write(pota, sizeof(pota)) == 8) {
    Serial1.flush();
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++) {
      //Serial.print(Serial1.read(),HEX);
      values[i] = Serial1.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  return values[4];
}

this is the output:
image

this is the schematic :

In future, please use the English language in the English sections of the forum; use google translate or similar if necessary,

I've translated your topic.

1 Like

Did you realise you can tie DE and RE together, and use a single pin to control them both !

They’re alternate polarity, so toggling the control but will enable one or the other,

thanks for reminding me

All responses are valid.
So either values are zero or you have wrong address on your request.
Also
return values[4]
gives only low byte of the response, in case of phosphorous it gives 0xCB (203 in dec), while the whole response is 0x0DCB

Hi, I am a beginner, I have a problem where the NPK sensor only reads phosphor, please help

this is my arduino code:

#define RE 8  
#define DE 7  

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
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 };

byte values[11];

void setup() {
  Serial.begin(9600);
  Serial1.begin(4800);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  digitalWrite(DE, LOW); // Set DE and RE pins to receive mode
  digitalWrite(RE, LOW);
}

void loop() {
  byte val1, val2, val3;
  val1 = nitrogen();
  delay(250);
  val2 = phosphorous();
  delay(250);
  val3 = potassium();
  delay(250);

  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");
}

byte nitrogen() {
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (Serial1.write(nitro, sizeof(nitro)) == 8) {
    Serial1.flush();
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++) {
      //Serial.print(Serial1.read(),HEX);
      values[i] = Serial1.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  return values[4];
}

byte phosphorous() {
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (Serial1.write(phos, sizeof(phos)) == 8) {
    Serial1.flush();
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++) {
      //Serial.print(Serial1.read(),HEX);
      values[i] = Serial1.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  return values[4];
}

byte potassium() {
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (Serial1.write(pota, sizeof(pota)) == 8) {
    Serial1.flush();
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++) {
      //Serial.print(Serial1.read(),HEX);
      values[i] = Serial1.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  return values[4];
}

this is the output:
image

this is the schematic :

I've merged your two topics on the same subject. Please do not cross post, it wastes people's time.

Did you read my answer on post#7 ?

A couple of things:

Firstly, if you are going to print out the values, then put a space between them so you know what the values actually are. Take this output of yours for example:

13200B844

What were the actual values? Were they 1,3,2.. or 13,2.. or maybe 1,32..

In this case, the actual values were 01 03 02 00 00 B8 44.

Secondly, you need to wait for your NPK sensor to respond before reading the returned values.

I think your code, as it stands, is asking for nitrogen - probably getting a bunch of 255 (or FF) the first time. It then asks for phosphorous and likely reads the values already in the receive buffer (i.e. nitrogen). It then asks for potassium and reads in the values for phosphorous and so on.

Also note what you've been told in post #5.

EDIT: Any reason you don't want to use the ModbusMaster library that takes care of a lot of the hard work for you?

sorry before I didn't understand how to use this forum, when I started it again after a few days and I just realized that today I made a new post on the same topic.

sorry I just saw it, thanks in advance for the answer, I will try to reset the wrong address in the request

thanks for the suggestion, I will also try using the modbusmaster library

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