Need Help with soil pH sensor RS485

Hi, i am working on a capstone project of mine and im stuck on this soil pH sensor part that i have. The readings that is showing in my serial monitor are all "Soil ph: 25.5" and it isnt changing at all. The sensor that i have is a soil ph sensor with RS-485 output.

This is my code:

#define RE 8
#define DE 7

const byte ph[] = {0x01, 0x03, 0x00, 0x0d, 0x00, 0x01, 0x15, 0xC9};
byte values[11];

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

void loop() {
  byte val;
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);

  if (Serial1.write(ph, sizeof(ph)) == 8) {
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);

    for (byte i = 0; i < 11; i++) {
      values[i] = Serial1.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();

    float soil_ph = float(values[4]) / 10;
    Serial.print("Soil Ph: ");
    Serial.println(soil_ph, 1);
  }

  delay(3000);
}

The wiring diagram of the MAX485 goes like this:
DI - TX1 (18)
DE - 7
RE - 8
RO - RX1 (19)

This is the output that is showing in the serial monitor

this is the datasheeet for the sensor provided by the seller:
Datasheet

Curious! There's suddenly been a few posts regarding soil pH recently.

With reference to your code, there's some issues that you need to fix.

If you are using Serial1 for your RS485 comms, then you should have a Serial1.begin in your setup code.

The response from the pH sensor isn't going to be instantly available the moment the request has been transmitted. You have to wait for the response which I think has to be less than 200 milliseconds (Modbus protocol defines this).

Hello @markd833 @raffcutie4ever I'm also working on a final project, I can't find much documentation and I have exactly the same problem.

I wanted to know if you have found any solution that you can provide me to continue with my project.

This is my code that I am currently working on

#include <Wire.h>
#include <Adafruit_SH110X.h>


#define i2c_Address 0x3C // Ajusta la dirección I2C según tu pantalla (0x3C o 0x3D)

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define RE 26  // Cambia el número de pin según tu configuración
#define DE 27  // Cambia el número de pin según tu configuración

const byte ph[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
byte values[11];


void setup() {
  Serial.begin(9600);
  Serial2.begin(4800); // Inicializa el puerto serial 2

  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

  delay(250);
  display.begin(i2c_Address, true);
  display.display(); // Mostrar el buffer en la pantalla
  delay(2000);
}

void loop() {

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  Serial2.write(ph, sizeof(ph));  // Utiliza Serial2 para la comunicación serial
  delay(10);
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  for (byte i = 0; i < 11; i++) {
    values[i] = Serial2.read();  // Utiliza Serial2 para la lectura
    Serial.print(values[i], HEX);
  }
  Serial.println();

  float soil_ph = float(values[4]) / 10;
  Serial.print("Soil Ph: ");
  Serial.println(soil_ph, 1);

  delay(3000); // Ajusta el tiempo de espera según sea necesario

  display.clearDisplay();

  // Configuración de tamaño de texto y color
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);


  display.setCursor(0, 0);
  display.print("Soil Ph: ");
  display.print(soil_ph, 1);


  // Mostrar en la pantalla
  display.display();

  // Esperar un breve período antes de la próxima lectura
  delay(1000);
}

MAX485 connections:

  • VCC - 5v ( External connection)
  • GND - GND (External connection)
  • DI - GPIO17
  • DE - GPIO 27
  • RE - GPIO 26
  • RO - GPIO 16

I also include a 5V - 3.3V logic state converter at the output of the MAX485 to control the voltage of the GPIO pins of the esp32.

Posting an annotated schematic would help us help you, Many of us do not have the parts you are using. Be sure to post links to each of the devices.

Do you have documentation for the sensor you are using? You need some details that are specific to your sensor such as the default baud rate, default sensor address and the register address(es) for the parameter(s) you want to read.

Also be careful with the use of delay, especially after transmitting the message as your setup needs to be in receive mode ready for the response from the sensor.

Also take note of what I said previously:

Can you posta link?

Hello Thank you for your reply.
I share the data sheet that was sent by the distributor of the Product.
PH RS485.pdf (895,9 KB)

I will be reviewing your previous suggestions. Thank you for commenting if you have any additional suggestions

I'm afraid you've been duped into buying a fake sensor, like so many others recently. They all appear to be from the same manufacturer, looking very much alike. Well, maybe there exist fakes of the fake as well, it's very well possible that other factories have started to produce fake copies of the original fake.

You can not measure pH with two metal prongs, like that sensor has. It's simply impossible. At best it can measure soil conductivity (which is a reasonable measure for soil moisture) and soil temperature, but that's all it can actually do.

Can someone help me with the same problem. I am getting 25.5 as well

@roshinik welcome to the forum.

Which sensor do you have? Do you have the user manual for it?

Have you searched these forums for "NPK sensor". The code is almost identical. You will also find drawings of how other forum members have connected up their Arduino board, RS485 module and the sensor.

If you are getting 25.5, then I assume that you may have divided the reported value by 10 - indicating that the actual value may have been 255. This is the value that is returned from the serial port read function if there is no data to be read.

Baud Rate Configuration : RS485 communication requires both devices (Arduino Uno and sensor) to use the same baud rate. Ensure that the baud rate settings on both devices match. If they don't, you may receive garbage data or no data at all.
Power Supply : Ensure that the power supply to the sensor is stable and meets the required specifications. Inadequate power supply can lead to erratic behavior or incorrect readings.
Calibration : Check if the sensor requires calibration or initialization before providing accurate readings. Follow the calibration procedure outlined in the sensor's datasheet or user manual.
Data Parsing : Make sure that you're correctly parsing the data received from the sensor. Incorrect parsing of data can result in misinterpretation of readings.