NPK sensor(5v-30v), Arduino Uno, RS485 to ttl converter

I have been working on a project where NPK sensor(5v-30v), Arduino Uno, RS485 to ttl converter is used i have given the connection and use the code

#include <SoftwareSerial.h>


// 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[8];

SoftwareSerial mod(2, 3);
 
void setup() {
  // Set the baud rate for the Serial port
  Serial.begin(4800);

  // Set the baud rate for the SerialSoftware object
  mod.begin(4800);
  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(){
   mod.write(nitro,8);
  delay(100);
     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(){

  mod.write(phos,8);
  for(byte i=0;i<7;i++){
   values[i] = mod.read();
    Serial.print(values[i],HEX);
    }
    Serial.println();
    return values[4];
}
 
byte potassium(){
  mod.write(pota,8);
  for(byte i=0;i<7;i++){
   values[i] = mod.read();
    Serial.print(values[i],HEX);
  }
      Serial.println();
    return values[4];
}

connection
RS485 to ttl converter TX to Arduino TX, RX to RX

A to NPK A, B to NPK b, 12v 2 A power to NPK,
RS485 to ttl converter 5V to arduino uno 5v and GND to GND

the result is coming as 255 continously.
please help
rs485 to ttl used
RS485 to TTL converter

Description :

  With this board you can  connect your microcontroller serially that’s far away from each other than this module interfaces RS-485 to TTL or vice-versa.It has Auto Direction control, making it easier to use as RS-232/TTL Serial Interface replacement. RS485 is used for Serial Communications over longer distances. The converter is designed for office and industrial applications (non-isolated), and provides superior characteristics/features normally found only on more expensive units.

Specification & Features :

• Compatibility: converts rs485 to ttl and vice- versa. (Transceiver Facility )
• Interface RS485 side: 2-pin (A+,B-) terminal connector.
• Interface TTL side:4-pin burg stick (5v,GND,tx,rx) .
• Transmission rate: 300 - 115200bps
• Transmission distance: RS485 max. 1200m,
• Working mode: asynchronous, half-duplex, differential transmission.
• Operating Voltage - 5volts DC

Welcome. I would suggest you move your post out of the Uncategorized section and into either the Networking or Sensors section before a Moderator spots it.

Have a search of these forums for NPK as there are many discussions involving an Arduino UNO, RS485 module and that type of sensor.

You should also have a copy of the user manual for your specific sensor as they are not all the same.

You should be using pins 2 and 3 that the sketch is using.

After you get it working, don't put any value to the numbers you're getting. Those sensors are fake... unfortunately there are too many sellers that either don't know or don't care but are more than happy to take your money.

I have connected to pin 2 and 3. The values of potassium is changing, but the values of phosphorus remain 255 and Nitrogen remain 2.

Sry.. i didn't get it which value not to be added

The code prints out some information. Copy it into a reply so we can see what is being shown to you.

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