Hello guys I have an npk sensor which i tried every code i found on it always either gives me all the values 0 or 255 can some one help? this is the information for the sensor as well...Download 6 in 1 EC-T-H-NKP.pdf | LimeWire
Welcome to the forum
Please post your sketch, using code tags when you do, and a schematic of your project showing how all of the components are interconnected and powered
There is no such thing as a hobbyist NPK sensor. There are a few professional units for some measurements that cost thousands of dollars. We have seen countless posts here about this scam.
Only soil moisture, pH, and temperature work and the moisture sensor will not last long, not even one growing season.
Then you have wiring error or wrong parameters. Error 255 means time out, no communication.
i have fixed it and it worked how ever i didnt not know how to add the other parameters to the code: ```#include <SoftwareSerial.h>
#define RE 7
#define DE 6
const uint32_t TIMEOUT = 500UL;
//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01, 0x03, 0x00, 0x00, 0xFF, 0x9B, 0x05, 0xCB};
const byte phos[] = {0x01, 0x03, 0x00, 0x04, 0x00, 0x01, 0xC5, 0xCB};
const byte pota[] = {0x01, 0x03, 0x00, 0x05, 0x00, 0x01, 0x94, 0x0B};
byte values[11];
SoftwareSerial mod(8, 9); // Rx pin, Tx pin
void setup() {
Serial.begin(9600);
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
delay(500);
}
void loop() {
byte val1, val2, val3;
Serial.print("Nitrogen: ");
val1 = nitrogen();
Serial.print(val1);
Serial.print(" mg/kg\n\n");
delay(1000);
Serial.print("Phosphorous: ");
val2 = phosphorous();
Serial.print(val2);
Serial.print(" mg/kg\n\n");
delay(1000);
Serial.print("Potassium: ");
val3 = potassium();
delay(1000);
Serial.print(val3);
Serial.print(" mg/kg\n\n");
delay(5000);
}
byte nitrogen() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(nitro, sizeof(nitro));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while ( millis() - startTime <= TIMEOUT ) {
if (mod.available() && byteCount<sizeof(values) ) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount-1]);
}
}
Serial.println();
return values[4];
}
byte phosphorous() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(phos, sizeof(phos));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while ( millis() - startTime <= TIMEOUT ) {
if (mod.available() && byteCount<sizeof(values) ) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount-1]);
}
}
Serial.println();
return values[4];
}
byte potassium() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(pota, sizeof(pota));
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
startTime = millis();
while ( millis() - startTime <= TIMEOUT ) {
if (mod.available() && byteCount<sizeof(values) ) {
values[byteCount++] = mod.read();
printHexByte(values[byteCount-1]);
}
}
Serial.println();
return values[4];
}
void printHexByte(byte b)
{
Serial.print((b >> 4) & 0xF, HEX);
Serial.print(b & 0xF, HEX);
Serial.print(' ');
}````
From the file i understood for the NPK values what about the temp, ec and moisture?
Are you sure about that?
Modbus messages contain 16-bit data but your routines only return an 8-bit value.
Do you have the user manual for your specific sensor. It will detail the actual registers you need to query to get the remaining parameters including any data conversion that needs to be done on the values received.
Please re-edit your post #5 and place the code between code tags.
That code is a mess. From where you got it? I can't imagine it outputs any values that make sense (not referring to the fact that the sensor doesn't output real NPK in the first place).
For example here you are requesting 65435 registers starting from the first one and the CRC is not matching, so you probably get exception response...
It looks similar to some diagnostic code I put on here some time back when I was trying to help somebody out with their NPK sensor but the canned modbus messages have been changed... ![]()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.