I need help with soil ph sensor and RS485

Hi, i am working on a final 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
Soil pH: -614.7
Soil pH: -25.8
Soil pH: -0.7
Soil pH: 3276.7
Soil pH: -168.1
Soil pH: -308.5
Soil pH: -819.3
Soil pH: -284.9
Soil pH: -52.9. The sensor that i have is a soil ph sensor with RS-485 output.
Here is my code.I ve changed the baud rate as 4800 .But it hasn't changed anything.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define RE 8
#define DE 7

LiquidCrystal_I2C lcd(0x27, 16, 2);

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

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

  lcd.begin(16, 2);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Smart");
  delay(2000);
  lcd.clear();
}

void loop() {
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  
  if (Serial.write(ph, sizeof(ph)) == sizeof(ph)) {
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);

    delay(10); 

 
    unsigned long startTime = millis();
    byte bytesReceived = 0;
    while (bytesReceived < 8 && millis() - startTime < 5000) { // 5 saniye içinde tüm veriyi alamazsa çık
      if (Serial.available()) {
        values[bytesReceived++] = Serial.read();
      }
    }

    if (bytesReceived == 8) {
      int highByte = values[3]; 
      int lowByte = values[4]; 
      float soil_ph = ((highByte << 8) | lowByte) / 10.0; 

      Serial.print("Soil pH: ");
      Serial.println(soil_ph, 1);

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Ph Value: ");
      lcd.setCursor(0, 1);
      lcd.print(soil_ph, 1);
    } else {
      Serial.println("Error: Incomplete data received.");
     }
  }  else {
    Serial.println("Error: Failed to send command.");
     }
  
  delay(3000);
}

I have connection problem also.When i connect gnd and vcc pins of rs485 to arduino uno , i get Error:Incomplate data received. On the other hand , if i don't connect those pins, i got those responses which begins -/+ random numbers.

The wiring diagram of the MAX485 goes like this:
DI - RX
DE - 7
RE - 8
RO - TX
A - yellow cable of soil ph sensor(it says this one for pH signal)
B - blue cable of soil ph sensor
GND -
VCC -
My soil ph sensor is ConWinTop. It has two probe.

Ekran görüntüsü 2024-05-13 154943

Please help me with that problem. I really need help :persevere:

Hi @melda_karasu ,

Welcome to the forum..

The request sent looks wrong..
Check you doc, should be like "E.G. master read PH"..

What board are you using??
Might want to fire up a softwareserial and get off of the rx, tx as it's also used for programming / debugging..

good luck.. ~q

Worked on allot of them 7 in 1's..
Here's my final sketch..
Uno7n1 Modbus Sensor

maybe it helps..

~q

1 Like

I'm using the Arduino Uno. To be honest when i changed my cables i always have the same error which is Incomplete data recieved. Btw thanks for warned me about request adress. I ve changed it. However still i'm been having the same -/+ random numbers. I have no any idea why i've been through with this. i should use arduino uno, rs485 and soil ph sensor. Maybe someone can tell me how can i connect pins of Rs485.

Looks like you got RO and DI are reversed..
I'm still thinking you should be using software serial and moving those pins off your tx,rx pins..

did you try the sketch i posted a link too??
getting MOIST would be your PH, looking at the request bytes anyways..

also per your doc the default baud is 4800, you're using 9600..

good luck.. ~q

Software serial doesn't work for my project.I ve already tried it )) When i use RX,TX pins ,i have +/- random numbers at least. But if i use software serial and use 2/3 pins i have Error:Incomplete data recieved :frowning: Even though reversing RX TX pins , i have Error:Incomplete data recieved. I also have gnd vcc pins problem also. It shows me Error:Incomplete data recieved almost in every step.I'm tired see that ERROR message agan and over again.

When you Serial.print for debug then this also goes out the tx pin and you're probably reading some of that back in..
I guess you could use the rx, tx , just don't do any serial prints, do a lcd.print instead..
But i'm still thinking the underlying problem will remain..
Last one was just a bad connection..

software serial should work just fine for this project..
there's something else..

how about a pic, let's see it??

~q

I've found the problem. My sensor has 0-5V output.It means it has analog output.I was thinking that it has RS485 output.However i have some problems again.Firstly i'll share sensor features.
PH-A (analog type).pdf (281.5 KB)
Secondly here is my code ,

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int phPin = A0;


float voltage = 0.0;
float phValue = 0.0;


LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(4800);

  lcd.begin(16,2);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("pH Sensor");
}

void loop() {

  int sensorValue = analogRead(phPin);
  
  voltage = sensorValue * (5.0 / 1023.0);

  // Formula: measuring PH value = ((9-3)/(5-0) * voltage) + 3
  phValue = ((9.0 - 3.0) / (5.0 - 0.0)) * voltage + 3.0;

  Serial.print("Voltage: ");
  Serial.print(voltage);
  Serial.print(" V, pH Value: ");
  Serial.println(phValue);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Voltage: ");
  lcd.print(voltage, 2);
  lcd.print(" V");
  lcd.setCursor(0, 1);
  lcd.print("pH Value: ");
  lcd.print(phValue, 2);

   
  delay(2000);
}

The problem is when the sensor is idle, it shows pH values ​​approaching 9. It measures the pH of CaC03 at 7.That's why i thought that there is smthg wrong.
CaC03 pH
Would you please help me ?

Calcium carbonate at 7 sounds right..
Vinegar should be around 2-3..
Baking soda around 9..

I simulated your code..

Seems good, maybe a wiring or power supply issue??

good luck.. ~q

I used a 12V adaptor for black and brown cables of soil pH sensor. I need asidic and basic soil. That's why i bought sulfur and CaC03 . As far i know CaCO3 should be between 7,5-9,5 pH.

google says 7..

on the connections I was thinking more on the lines of just a bad connection..
the connections are obviously correct as you are getting some valid readings..
what do you mean specifically when you say..

Is it still submersed in the substance??

faulty sensor??

strange.. ~q

One issue with digitising an analogue signal is that there may well be a small DC offset lurking around in your system. That would give you a higher (or lower) reading than you expect.

You would need to expose your sensor to known accurate pH levels in order to determine if there was an offset in order to compensate for it.

Also make sure that you have a common ground between your sensor and your Arduino too.

My project is about smart fertilizing system.There are two part of soil . One part should include asidic soil ,other part should include basic soil.Sensor need able to mesure ph,after that project is have to do fertilizng.However i couldn't passed the other section of project.Cause firstly sensor should mesure asidic and basic ph of soil.I don't know how to decrease or increase ph of soil.I ve already tried adding sulfur and CaCO3 and measured it. But it was showing me that around 7.20pH for soil with CaCO3.Maybe it shows right.I don't know.But i need to find the way how to decrase and increase ph of soil and then i can mesure them.

when i took the sensor out , pH value is getting quickly high.

I bought soil from florist.It was showing that pH of that soil is between 6-7pH. I measured and result was around 6.80.

what exactly is it??
soil, water, what is IT??

yes, sulfur lowers, lime raises..

but in soil i do believe the process takes some time..

Soil bacteria change the sulfur to sulfuric acid, lowering the soil pH

~q

What does it mean ??

soil.

The pH value on a pH meter for calcium carbonate in pure water will be nearly 7 making it neutral since the solubility of this compound is extremely low

above is from google..
your readings sound right..

~q

i guess tell your soil bacteria to hurry up.. :slight_smile:

~q