Hello, I'm following this tutorial Tutorial to interface a NPK sensor 7 in 1 with my Arduino, i pretty much sure that my wiring is correct, i follow the schematic in the tutorial and verify many time so i guess some issue on my code.
#include <SoftwareSerial.h>
#include <Wire.h>
// RE and DE Pins set the RS485 module
// to Receiver or Transmitter mode
#define RE 8
#define DE 7
// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01,0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x0C};
// A variable used to store NPK values
byte values[11];
// Sets up a new SoftwareSerial object
// Digital pins 10 and 11 should be used with a Mega or Mega 2560
SoftwareSerial mod(2, 3);
//SoftwareSerial mod(10, 11);
void setup() {
// Set the baud rate for the Serial port
Serial.begin(9600);
// Set the baud rate for the SerialSoftware object
mod.begin(9600);
// Define pin modes for RE and DE
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
delay(500);
}
void loop() {
// Read values
byte val1;
val1 = nitrogen();
delay(250);
// Print values to the serial monitor
// Serial.print("Nitrogen: ");
Serial.print(val1);
delay(2000);
}
byte nitrogen(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(nitro,sizeof(nitro))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
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[1];
}
I simplify the code in order to read only nitrogen, the value i get print out is always 255 .... something is wrong but cant find what.. any suggestion... i don't know where to start look into.