RS485 Soil Sensor(EC&PH) Arduino WiKi- DFRobot

Introduction

This sensor is suitable for soil measurement and observation. It has a wide voltage power supply of 5-30V and RS485 output. It can detect soil Electrical Conductivity (EC)and PH. It has fast response and stable output. It can be expanded with Arduino UNO R3 and TTL to 485. board to quickly build and test.

The protection level of the soil sensor is IP68, and it is vacuum filled and sealed with black flame-retardant epoxy resin. The probe material is made of 316 stainless steel, which is rust-proof, waterproof, anti-corrosion, salt-alkali corrosion resistance, and long-term electrolysis resistance. It is more affected by the soil salt content. It is small, can be buried in the soil for a long time, and is suitable for various soil types.

The soil sensor has an automatic temperature compensation function for conductivity, which can ensure the accuracy of measurement no matter how the environment changes. It is widely used in agricultural irrigation, greenhouses, flowers and vegetables, grassland pastures, rapid soil testing, plant cultivation, scientific experiments, grain Storage and measurement of moisture content and temperature of various particulate matter.

Soil electrical conductivity (EC) is the level of salt in the soil (salinity). These parameters are important indicators of soil fertility and health, and ultimately affect plants. When soil has high salt content, the salt makes it difficult for plants to absorb water, even if the moisture content in the soil is high. This condition will eventually "burn" the plant. Plants grow best in their preferred environment within the optimal range of soil moisture, temperature and EC levels. Growers need data on these parameters and understand what actions need to be taken to ensure plant health and higher yields.

Soil pH, also known as "soil reaction". It is an acid-base reaction of the soil solution, which mainly depends on the concentration of hydrogen ions in the soil solution, expressed by the pH value. A solution with a pH value equal to 7 is a neutral solution; a solution with a pH value less than 7 is an acidic reaction; a solution with a pH value greater than 7 is an alkaline reaction. Soil pH can generally be divided into the following levels:

PH value Soil acidity and alkalinity

<4.5 Extremely acidic

4.5-5.5 Strongly acidic

5.5-6.5 acidic

6.5-7.5 Neutral

7.5-8.5 alkaline

8.5-9.5 Strongly alkaline

>9.5 Extremely alkaline

Features

Specifications:

Board Overview

Num Label Description
Brown line VCC Power input positive pole, DC5-30V power supply
Black line GND Power ground wire
Yellow line 485-A RS485 data line A
Blue line 485-B RS485 data line B

Dimensional Drawing

Dimensional Drawing

Communication Protocol

1. Basic communication parameters

Interface Encoding Data bits Parity bits Stop bits Error checking Baud rate
RS485 8-bit binary 8 None 1 CRC 2400bit/s, 4800bit/s, 9600 bit/s configurable, default 9600bit/s

2. Data frame format definition

Using ModBus-RTU communication protocol, the format is as follows:

Host query frame structure:

Address code Function code Register starting address Register length Check code low bit Check code high bit
1byte 1byte 2byte 2byte 1byte 1byte

Slave response frame structure:

Address code Function code Number of valid bytes Data area 1 Data area 2 Nth data area Check code
1byte 1byte 1byte 2byte 2byte 2byte 2byte

3. Communication protocol examples and explanations

Example: Read the parameter values of the EC and PH two-in-one device (address 0x01)

Inquiry frame (hexadecimal):

Address code Function code Register starting address Register length Check code low bit Check code high bit
0x01 0x03 0x00 0x00 0x00 0x04 0x44 0x09

Response frame (hexadecimal):

Address code Function code Return the number of valid bytes Humidity value Temperature value EC value PH value Low bit of check code High bit of check code
0x01 0x03 0x08 0x00 0x00 0x00 0x00 0x03 0xE8 0x00 0x38 0x57 0xB6

EC calculation:

PH calculatio:

4. Register address

Register address PLC or configuration address Content Operation Definition description
0000H 40001 (decimal) Moisture content Read-only Real-time value of moisture content (expanded 10 times)
0001H 40002 (decimal) Temperature value Read-only Temperature real-time value (expanded 10 times)
0002H 40003(decimal) Conductivity Read-only Real-time value of conductivity
0003H 40004(decimal) Salinity Read-only Salinity real-time value
0004H 40005(decimal) Total dissolved solids TDS Read only TDS real-time value
0022H 40035 (decimal) Conductivity temperature coefficient Read and write 0-100 corresponds to 0.0%-10.0%, default 0.0%
0023H 40036 (decimal) Salinity coefficient Read and write 0-100 corresponds to 0.00-1.00 Default 55 (0.55)
0024H 40037 (decimal) TDS coefficient Read and write 0-100 corresponds to 0.00-1.00, default 50 (0.5)
0050H 40081 (decimal) Temperature calibration value Read and write Integer (expanded 10 times)
0051H 40082 (decimal) Moisture content calibration value Read and write Integer (expanded 10 times)
0052H 40083(decimal) Conductivity calibration value Read and write Integer
07D0H 42001 (decimal) Device address Read and write 1-254 (factory default 1)
07D1H 42002 (decimal) Device baud rate Read and write 0 represents 2400 1 represents 4800 2 represents 9600

Tutorial

Requirements

Connection Diagram

Note: Before burning the code, please switch the transceiver mode switch of the expansion board to AUTO, and switch the run/compile switch to OFF. After burning the code, switch the run/compile switch to ON, and select the serial port baud rate to 9600.

Arduino Wiring diagram

Sample Code

uint8_t Com[8] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09 };
float ph;
int ec;
void setup() {
  Serial.begin(9600);  
}
void loop() {
  readECPH();
  Serial.print("EC = ");
  Serial.print(ec, 1);
  Serial.print(" us/cm ");
  Serial.print("PH = ");
  Serial.println(ph, 1);
  delay(1000);
}
void readECPH(void) {
  uint8_t Data[13] = { 0 };
  uint8_t ch = 0;
  bool flag = 1;
  while (flag) {
    delay(100);
    Serial.write(Com, 8);
    delay(10);
    if (readN(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readN(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readN(&ch, 1) == 1) {
              if (ch == 0x08) {
                Data[2] = ch;
                if (readN(&Data[3], 10) == 10) {
                  if (CRC16_2(Data, 11) == (Data[11] * 256 + Data[12])) {
                    ec = Data[7] * 256 + Data[8];
                    ph = (Data[9] * 256 + Data[10]) /10.00;
                    flag = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();
  }
}

uint8_t readN(uint8_t *buf, size_t len) {
  size_t offset = 0, left = len;
  int16_t Tineout = 500;
  uint8_t *buffer = buf;
  long curr = millis();
  while (left) {
    if (Serial.available()) {
      buffer[offset] = Serial.read();
      offset++;
      left--;
    }
    if (millis() - curr > Tineout) {
      break;
    }
  }
  return offset;
}

unsigned int CRC16_2(unsigned char *buf, int len) {
  unsigned int crc = 0xFFFF;
  for (int pos = 0; pos < len; pos++) {
    crc ^= (unsigned int)buf[pos];
    for (int i = 8; i != 0; i--) {
      if ((crc & 0x0001) != 0) {
        crc >>= 1;
        crc ^= 0xA001;
      } else {
        crc >>= 1;
      }
    }
  }

  crc = ((crc & 0x00ff) << 8) | ((crc & 0xff00) >> 8);
  return crc;
}

Expected Results

Insert the soil sensor into the soil, and the serial port prints out the EC and PH values detected by the sensor.

Arduino serial port print data map

How to install and use

1、Quick test method

Select a suitable measurement location, avoid stones, and ensure that the steel needle does not touch hard objects. Throw away the topsoil according to the required measurement depth and maintain the original tightness of the soil below. Hold the sensor tightly and insert it vertically into the soil. Do not move left and right when measuring. It is recommended to measure multiple times within a small range of a measuring point and average it.

Quick test method

2、Buried measurement method

Dig a pit with a diameter >20cm vertically, insert the sensor steel needle horizontally into the pit wall at a predetermined depth, fill the pit tightly, and after a period of stabilization, measurements and recordings can be made for days, months or even longer.

Quick test method

3、Things to note

FAQ

Possible reasons for no output or output errors:

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents