Example Code for Arduino-Read Soil Temp, Humidity, EC

This project demonstrates how to use the RS485 Soil Sensor (Temperature&Humidity&EC) with Arduino UNO R3 to read real-time soil temperature, humidity, and electrical conductivity (EC) values. Users will learn how to establish RS485 communication using the ModBus-RTU protocol, send query commands to the sensor, and parse the returned data to obtain usable environmental parameters.

Hardware Preparation

  • DFRduino UNO R3, Product-838, x1
  • RS485 Shield for Arduino, Product-1024, x1
  • RS485 Soil Sensor(Temperature&Humidity&EC) (SEN0601), x1

Software Preparation

Wiring Diagram

Arduino Wiring diagram

Other Preparation Work

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

Sample Code

uint8_t Com[8] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x05, 0xCB };
float tem, hum;
int ec;
void setup() {
  Serial.begin(9600);  
}
void loop() {
  readHumitureEC();
  Serial.print("TEM = ");
  Serial.print(tem, 1);
  Serial.print(" °C  ");
  Serial.print("HUM = ");
  Serial.print(hum, 1);
  Serial.print(" %RH  ");
  Serial.print("EC = ");
  Serial.print(ec, 1);
  Serial.println(" us/cm ");
  delay(1000);
}
void readHumitureEC(void) {
  uint8_t Data[10] = { 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 == 0x06) {
                Data[2] = ch;
                if (readN(&Data[3], 8) == 8) {
                  if (CRC16_2(Data, 9) == (Data[9] * 256 + Data[10])) {
                    hum = (Data[3] * 256 + Data[4]) / 10.00;
                    tem = (Data[5] * 256 + Data[6]) / 10.00;
                    ec = Data[7] * 256 + Data[8];
                    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;
}

Result

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

Was this article helpful?

TOP