6 in 1 JXCT Multi-parameter Soil Sensor using Arduino Nano

I've been having this problem for weeks in displaying the right values from my 6 in 1 soil sensor. I've read so many forums and tried all of them but the output is still the same.
This is always the output that I am receiving.

When it comes to the schematic diagram, I followed this schematic from How2Electronics.

I used the same components in the schematic and connected my sensor to 12V and I have them all in common ground.

This is the code that I used in getting the values from the sensor.

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET -1     // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define RE 8
#define DE 7

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
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 };

const byte ph[] = { 0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b };  // New
const byte mois[] = { 0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0F };
const byte temp[] = { 0x01, 0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf };  // New

byte values[11];
SoftwareSerial mod(2, 3);

float envhumidity = 0.0, envtemperature = 0.0, soil_ph = 0.0, soil_mois = 0.0, soil_temp = 0.0;  // New
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0;                       // New

void setup() {
  Serial.begin(4800);
  mod.begin(4800);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //initialize with the I2C addr 0x3C (128x64)
  delay(500);
  display.clearDisplay();
  display.setCursor(25, 15);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.println(" NPK Sensor");
  display.setCursor(25, 35);
  display.setTextSize(1);
  display.print("Initializing");
  display.display();
  // put RS-485 into receive mode
  digitalWrite(DE, LOW);  // New
  digitalWrite(RE, LOW);  // New

  delay(3000);
}

void loop() {
  byte val1, val2, val3, val4, val5, val6;
  val1 = nitrogen();
  delay(300);
  val2 = phosphorous();
  delay(300);
  val3 = potassium();
  delay(300);
  val4 = phydrogen() / 10;  // New
  soil_ph = val4;
  delay(300);
  val5 = moisture();
  soil_mois = val5 / 10.0;
  delay(300);
  val6 = temperature();
  soil_temp = temperature() / 10.0;  // New
  delay(300);

  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");
  Serial.print("ph: ");  // New
  Serial.print(soil_ph);
  Serial.println(" ph");
  Serial.print("Moisture: ");
  Serial.print(soil_mois);
  Serial.println(" %");  //
  Serial.print("Temperature: ");
  Serial.print(soil_temp);
  Serial.println(" C");
  Serial.println();  // New
  delay(2000);

  display.clearDisplay();

  display.setTextSize(1);
  display.setCursor(0, 5);
  display.print("N: ");
  display.print(val1);
  display.setTextSize(1);
  display.print(" mg/kg");

  display.setTextSize(1);
  display.setCursor(0, 15);
  display.print("P: ");
  display.print(val2);
  display.setTextSize(1);
  display.print(" mg/kg");

  display.setTextSize(1);
  display.setCursor(0, 25);
  display.print("K: ");
  display.print(val3);
  display.setTextSize(1);
  display.print(" mg/kg");

  display.setTextSize(1); // New
  display.setCursor(0, 35);
  display.print("pH: ");
  display.print(soil_ph);
  display.setTextSize(1);
  display.print(" pH");

  display.setTextSize(1);
  display.setCursor(0, 45);
  display.print("Moisture: ");
  display.print(soil_mois);
  display.setTextSize(1);
  display.print(" %");

  display.setTextSize(1);
  display.setCursor(0, 55);
  display.print("Temperature: ");
  display.print(soil_temp);
  display.setTextSize(1);
  display.print(" C"); // New

  display.display();
}

byte nitrogen() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(nitro); i++) mod.write(nitro[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(200);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phosphorous() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(phos); i++) mod.write(phos[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte potassium() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(pota); i++) mod.write(pota[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phydrogen() {
  // clear the receive buffer
  mod.flush();
  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(ph); i++) mod.write(ph[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte moisture() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(mois); i++) mod.write(mois[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte temperature() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(temp); i++) mod.write(temp[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[3] << 8 | values[4];
}

Hope that someone can help me because it's been giving me headaches for weeks and the outputs are still the same and I can't figure out what to do in order to troubleshoot this anymore.

looks like the soil sensor interface is rs485? can you give a link to the device?
if you have a multimeter what are the voltages on the A and B signal lines?
if would be good idea to make the Serial baudrate faster than the RS485, e.g. 115200
for RS485 projects I would recommend you get a USB-RS485 dongle to help with debugging - I tend to use a FTDI usb-rs485-we-1800-bt but there are plenty of alternatives on ebay

@jairojoaquinaf i've responded in the other thread you posted in.

I see you also have an OLED connected as well. You should start by removing all the display code and getting the sensor working using simple prints to the hardware serial port first.

Note that there are a few variations of the NPK sensor and the parameters are at different addresses. Also some users indicate that their sensor was configured for 4800 baud.

You need to ask the store where you bought it for the datasheet that details the modbus comms interface.

hi, this is the link to the device. the voltage values on the A and B signals is around 2.37V. I already tried increasing the baudrate faster but its still the same

hi mark, I already tried the 4800 baud rate but its still the same. on the other hand though, as I removed the display codes. the serial outputs are now all zero.

This is now my current code:

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET -1     // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define RE 8
#define DE 7

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
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 };

const byte ph[] = { 0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b };  // New
const byte mois[] = { 0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0F };
const byte temp[] = { 0x01, 0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf };  // New

byte values[11];
SoftwareSerial mod(2, 3);

float soil_ph = 0.0, soil_mois = 0.0, soil_temp = 0.0;  // New
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0;                       // New

void setup() {
  Serial.begin(9600);
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

//  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //initialize with the I2C addr 0x3C (128x64)
//  delay(500);
//  display.clearDisplay();
//  display.setCursor(25, 15);
//  display.setTextSize(1);
//  display.setTextColor(WHITE);
//  display.println(" NPK Sensor");
//  display.setCursor(25, 35);
//  display.setTextSize(1);
//  display.print("Initializing");
//  display.display();
  // put RS-485 into receive mode
  digitalWrite(DE, LOW);  // New
  digitalWrite(RE, LOW);  // New
//
//  delay(3000);
}

void loop() {
  byte val1, val2, val3, val4, val5, val6;
//  val1 = nitrogen();
//  delay(300);
//  val2 = phosphorous();
//  delay(300);
//  val3 = potassium();
//  delay(300);
//  val4 = phydrogen() / 10;  // New
//  soil_ph = val4;
//  delay(300);
//  val5 = moisture();
//  soil_mois = val5 / 10.0;
//  delay(300);
//  val6 = temperature();
//  soil_temp = temperature() / 10.0;  // New
//  delay(300);

  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");
  Serial.print("ph: ");  // New
  Serial.print(soil_ph);
  Serial.println(" ph");
  Serial.print("Moisture: ");
  Serial.print(soil_mois);
  Serial.println(" %");  //
  Serial.print("Temperature: ");
  Serial.print(soil_temp);
  Serial.println(" C");
  Serial.println();  // New
  delay(2000);

//  display.clearDisplay();
//
//  display.setTextSize(1);
//  display.setCursor(0, 5);
//  display.print("N: ");
//  display.print(val1);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1);
//  display.setCursor(0, 15);
//  display.print("P: ");
//  display.print(val2);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1);
//  display.setCursor(0, 25);
//  display.print("K: ");
//  display.print(val3);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1); // New
//  display.setCursor(0, 35);
//  display.print("pH: ");
//  display.print(soil_ph);
//  display.setTextSize(1);
//  display.print(" pH");
//
//  display.setTextSize(1);
//  display.setCursor(0, 45);
//  display.print("Moisture: ");
//  display.print(soil_mois);
//  display.setTextSize(1);
//  display.print(" %");
//
//  display.setTextSize(1);
//  display.setCursor(0, 55);
//  display.print("Temperature: ");
//  display.print(soil_temp);
//  display.setTextSize(1);
//  display.print(" C"); // New
//
//  display.display();
}

byte nitrogen() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(nitro); i++) mod.write(nitro[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(200);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phosphorous() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(phos); i++) mod.write(phos[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte potassium() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(pota); i++) mod.write(pota[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phydrogen() {
  // clear the receive buffer
  mod.flush();
  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(ph); i++) mod.write(ph[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte moisture() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(mois); i++) mod.write(mois[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte temperature() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(temp); i++) mod.write(temp[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[3] << 8 | values[4];
}

I already asked the store for the datasheet and I'm only waiting for a response.

I would expect zeros as you have commented out the calls to query the sensor.

after removing all the display codes. the output is still the same.

this is now the updated code that I used:

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET -1     // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define RE 8
#define DE 7

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
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 };

const byte ph[] = { 0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b };  // New
const byte mois[] = { 0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0F };
const byte temp[] = { 0x01, 0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf };  // New

byte values[11];
SoftwareSerial mod(2, 3);

float soil_ph = 0.0, soil_mois = 0.0, soil_temp = 0.0;  // New
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0;                       // New

void setup() {
  Serial.begin(9600);
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

//  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //initialize with the I2C addr 0x3C (128x64)
//  delay(500);
//  display.clearDisplay();
//  display.setCursor(25, 15);
//  display.setTextSize(1);
//  display.setTextColor(WHITE);
//  display.println(" NPK Sensor");
//  display.setCursor(25, 35);
//  display.setTextSize(1);
//  display.print("Initializing");
//  display.display();
  // put RS-485 into receive mode
  digitalWrite(DE, LOW);  // New
  digitalWrite(RE, LOW);  // New
//
  delay(1000);
}

void loop() {
  byte val1, val2, val3, val4, val5, val6;
  val1 = nitrogen();
  delay(300);
  val2 = phosphorous();
  delay(300);
  val3 = potassium();
  delay(300);
  val4 = phydrogen() / 10;  // New
  soil_ph = val4;
  delay(300);
  val5 = moisture();
  soil_mois = val5 / 10.0;
  delay(300);
  val6 = temperature();
  soil_temp = temperature() / 10.0;  // New
  delay(300);

  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");
  Serial.print("ph: ");  // New
  Serial.print(soil_ph);
  Serial.println(" ph");
  Serial.print("Moisture: ");
  Serial.print(soil_mois);
  Serial.println(" %");  //
  Serial.print("Temperature: ");
  Serial.print(soil_temp);
  Serial.println(" C");
  Serial.println();  // New
  delay(2000);

//  display.clearDisplay();
//
//  display.setTextSize(1);
//  display.setCursor(0, 5);
//  display.print("N: ");
//  display.print(val1);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1);
//  display.setCursor(0, 15);
//  display.print("P: ");
//  display.print(val2);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1);
//  display.setCursor(0, 25);
//  display.print("K: ");
//  display.print(val3);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1); // New
//  display.setCursor(0, 35);
//  display.print("pH: ");
//  display.print(soil_ph);
//  display.setTextSize(1);
//  display.print(" pH");
//
//  display.setTextSize(1);
//  display.setCursor(0, 45);
//  display.print("Moisture: ");
//  display.print(soil_mois);
//  display.setTextSize(1);
//  display.print(" %");
//
//  display.setTextSize(1);
//  display.setCursor(0, 55);
//  display.print("Temperature: ");
//  display.print(soil_temp);
//  display.setTextSize(1);
//  display.print(" C"); // New
//
//  display.display();
}

byte nitrogen() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(nitro); i++) mod.write(nitro[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(200);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phosphorous() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(phos); i++) mod.write(phos[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte potassium() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(pota); i++) mod.write(pota[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phydrogen() {
  // clear the receive buffer
  mod.flush();
  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(ph); i++) mod.write(ph[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte moisture() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(mois); i++) mod.write(mois[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte temperature() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(temp); i++) mod.write(temp[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[3] << 8 | values[4];
}

I also used your code in this reply and there are no RX texts in the serial monitor. how would I know the address of the sensor because I already tried changing the baud rate and it still doesn't have a RX text.

The datasheet should tell you the default sensor address as well as the register addresses for the various parameters. You need both pieces of information to get a successful response from the sensor.

okay i will get back to you when the store responds and sends me the datasheet of the sensor. thank you mark!

hello mark, attached here is the datasheet that was sent by the store. can you help me in identifying the addresses that I need to change with my current code?
datasheet.pdf (410.1 KB)

this is my current code:

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET -1     // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define RE 8
#define DE 7

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
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 };

const byte ph[] = { 0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b };  // New
const byte mois[] = { 0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0F };
const byte temp[] = { 0x01, 0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf };  // New

byte values[11];
SoftwareSerial mod(2, 3);

float soil_ph = 0.0, soil_mois = 0.0, soil_temp = 0.0;  // New
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0;                       // New

void setup() {
  Serial.begin(9600);
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

//  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //initialize with the I2C addr 0x3C (128x64)
//  delay(500);
//  display.clearDisplay();
//  display.setCursor(25, 15);
//  display.setTextSize(1);
//  display.setTextColor(WHITE);
//  display.println(" NPK Sensor");
//  display.setCursor(25, 35);
//  display.setTextSize(1);
//  display.print("Initializing");
//  display.display();
  // put RS-485 into receive mode
  digitalWrite(DE, LOW);  // New
  digitalWrite(RE, LOW);  // New
//
  delay(1000);
}

void loop() {
  byte val1, val2, val3, val4, val5, val6;
  val1 = nitrogen();
  delay(300);
  val2 = phosphorous();
  delay(300);
  val3 = potassium();
  delay(300);
  val4 = phydrogen() / 10;  // New
  soil_ph = val4;
  delay(300);
  val5 = moisture();
  soil_mois = val5 / 10.0;
  delay(300);
  val6 = temperature();
  soil_temp = temperature() / 10.0;  // New
  delay(300);

  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");
  Serial.print("ph: ");  // New
  Serial.print(soil_ph);
  Serial.println(" ph");
  Serial.print("Moisture: ");
  Serial.print(soil_mois);
  Serial.println(" %");  //
  Serial.print("Temperature: ");
  Serial.print(soil_temp);
  Serial.println(" C");
  Serial.println();  // New
  delay(2000);

//  display.clearDisplay();
//
//  display.setTextSize(1);
//  display.setCursor(0, 5);
//  display.print("N: ");
//  display.print(val1);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1);
//  display.setCursor(0, 15);
//  display.print("P: ");
//  display.print(val2);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1);
//  display.setCursor(0, 25);
//  display.print("K: ");
//  display.print(val3);
//  display.setTextSize(1);
//  display.print(" mg/kg");
//
//  display.setTextSize(1); // New
//  display.setCursor(0, 35);
//  display.print("pH: ");
//  display.print(soil_ph);
//  display.setTextSize(1);
//  display.print(" pH");
//
//  display.setTextSize(1);
//  display.setCursor(0, 45);
//  display.print("Moisture: ");
//  display.print(soil_mois);
//  display.setTextSize(1);
//  display.print(" %");
//
//  display.setTextSize(1);
//  display.setCursor(0, 55);
//  display.print("Temperature: ");
//  display.print(soil_temp);
//  display.setTextSize(1);
//  display.print(" C"); // New
//
//  display.display();
}

byte nitrogen() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(nitro); i++) mod.write(nitro[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(200);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phosphorous() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(phos); i++) mod.write(phos[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte potassium() {
  mod.flush();
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);
  for (uint8_t i = 0; i < sizeof(pota); i++) mod.write(pota[i]);
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);
  // delay to allow response bytes to be received!
  delay(200);
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    // Serial.print(values[i], HEX);
    // Serial.print(' ');
  }
  return values[4];
}

byte phydrogen() {
  // clear the receive buffer
  mod.flush();
  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(ph); i++) mod.write(ph[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte moisture() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(mois); i++) mod.write(mois[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[4];
}

byte temperature() {
  // clear the receive buffer
  mod.flush();

  // switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(1);

  // write out the message
  for (uint8_t i = 0; i < sizeof(temp); i++) mod.write(temp[i]);

  // wait for the transmission to complete
  mod.flush();

  // switching RS485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  // delay to allow response bytes to be received!
  delay(1000);

  // read in the received bytes
  for (byte i = 0; i < 7; i++) {
    values[i] = mod.read();
    Serial.print(values[i], HEX);
    Serial.print(' ');
  }
  return values[3] << 8 | values[4];
}

Thank you in advance

That datasheet says the default address is 1 and the baud rate is 4800 baud.

Column 1 on pages 7 & 8 gives the register addresses you need for the various parameters.

I didn't see any mention of Nitrogen, Phosphate or Potassium values.

Can you help me in integrating the said register addresses in my Arduino code because honestly I'm new to this and I have no idea how to integrate the addresses on the table.

I think the Nitrogen, Phosphate, and Potassium values are classified in the table as Total Dissolved Solids (TDS). But that is just my guess

I'd start with register 1 - temperature. I think the byte sequence you need is:

0x01 0x03 0x00 0x01 0x00 0x01 0xD5 0xCA

Put those values into the array called nitro (for now) and see what response you get. It should be the actual temperature x10 - so 15C should be 150.

hello, after changing the byte sequence of the temperature, it still outputs the same value which is 25.50. You can see it in the screenshot below:


I already changed the baudrate to 4800.

Have you also changed your code to send out the contents of the temp array?

No, I haven't which code should I change in sending out the contents of the temperature array? I'm sorry if I'm having too many questions.

If you do the above, you shouldn't need to change any other code.

Yes, I already did that as you can see here in this screenshot:


but unfortunately, it still returns the same nitrogen value

Ok. When I get back to my big screen I will look up the really simple test code to check Comms with an npk sensor.