Currently, I am doing a project for my school and I have decided to do a IoT based smart agriculture device which I am using the Arduino MKR WAN 1310 since it has LoRa capabilities. I am having a lot of issues with an NPK sensor that I am using that I have been tackling for weeks with no progress. The connections I have for my MAX485 are as goes: DE -> 1, RE ->2, DI -> TX(14), RO -> RX(13). The MAX485 is being powered via 5v which I believe is correct too. But the issue I am having is the arduino is giving this output:
FFFFFFFFFFFFFF
FFFFFFFFFFFFFF
FFFFFFFFFFFFFF
Nitrogen: 255 mg/kg
Phosphorous: 255 mg/kg
Potassium: 255 mg/kg
The NPK is being powered by 12v DC which is what is recommended from the manual that came with it. Here is the code that I am using for this project maybe someone can help me pinpoint if there is anything wrong with it (I am also very very new to this forum and arduinos themselves):
// Define RE and DE Pins to set the RS485 module to Receiver or Transmitter mode
#define RE 1
#define DE 2
// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xB5, 0xCC};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xE4, 0x0C};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xC0};
// Array to store NPK values
byte values[11];
void setup() {
// Set the baud rate for the Serial port
Serial.begin(9600);
// Set the baud rate for Serial1 (hardware serial on MKR WAN 1310)
Serial1.begin(9600);
// Define pin modes for RE and DE
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
delay(500);
}
void loop() {
// Read and print values
byte val1 = nitrogen();
delay(250);
byte val2 = phosphorous();
delay(250);
byte 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");
delay(2000);
}
byte nitrogen() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (Serial1.write(nitro, sizeof(nitro)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
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) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
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) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = Serial1.read();
Serial.print(values[i], HEX);
}
Serial.println();
}
return values[4];
}