General Arduino

RS485 MODBUS-RTU Soil NPK Measure Sensor

userHead Hatarok 2025-06-12 03:56:52 326 Views1 Replies

Hello all,

 

The RS485 MODBUS-RTU Soil NPK Measure Sensor from DFRobot was purchased. I would appreciate tips to get readings from this sensor.

 

I want to use the Serial Monitor on Arduino IDE to display NPK values. I am using a MAX 485 modbus and Arduino Nano. 

 

The documentation for data formatting for the NPK sensor is here: 

https://wiki.dfrobot.com/RS485_Soil_Sensor_N_P_K_SKU_SEN0605

 

Currently, powering the sensor with 5 V on the Arduino Nano, and I have tried using a 9 V battery in addition, but no luck getting values. 

 

The sensor is in some outdoor soil but getting 0's for all readings with the current code below:

 

#include <ModbusMaster.h>

#include <SoftwareSerial.h>



 

SoftwareSerial rs485Serial(2, 3); // RX, TX


 

ModbusMaster node;


 

// RS485 Direction Control

#define MAX485_DE 7

#define MAX485_RE 8


 

void preTransmission() {

digitalWrite(MAX485_RE, HIGH);

digitalWrite(MAX485_DE, HIGH);

}


 

void postTransmission() {

digitalWrite(MAX485_RE, LOW);

digitalWrite(MAX485_DE, LOW);

}


 

void setup() {

Serial.begin(9600);

rs485Serial.begin(9600);


 

 

pinMode(MAX485_RE, OUTPUT);

pinMode(MAX485_DE, OUTPUT);

digitalWrite(MAX485_RE, LOW);

digitalWrite(MAX485_DE, LOW);


 

 

node.begin(1, rs485Serial);

node.preTransmission(preTransmission);

node.postTransmission(postTransmission);


 

Serial.println("NPK Sensor Initialized via RS485");

}


 

void loop() {

uint8_t result;

result = node.readInputRegisters(0x00, 3);


 

if (result == node.ku8MBSuccess) {

Serial.print("Nitrogen (N): ");

Serial.println(node.getResponseBuffer(0));


 

Serial.print("Phosphorus (P): ");

Serial.println(node.getResponseBuffer(1));


 

Serial.print("Potassium (K): ");

Serial.println(node.getResponseBuffer(2));

} else {

Serial.print("Read error. Code: ");

Serial.println(result);

}


 

delay(2000);

}




 

2025-06-15 23:10:45

Try indoors with the sensor in damp soil, not bone-dry dirt.

Use a USB to RS-485 adapter and a PC tool like Modbus Poll to test if needed.

Add a Modbus termination resistor (like 120 ohms) between A and B if communication is unreliable.

userHeadPic ahsrab.rifat