Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1072

Issues Connecting 7-in-1 Soil Sensor to ESP32 via RS485 with RE/DE

$
0
0

Hello everyone,

I’m attempting to connect a 7-in-1 soil sensor to my ESP32 through RS485 using the RE/DE pins for direction control. I followed the step-by-step instructions in this guide: 7-in-1 Soil Monitor - Instructables, but unfortunately, I couldn’t get it to work.

Here’s what I’ve done so far:

  1. Configured my ESP32 pins as follows:
  • RE pin: GPIO 4
  • RXD pin: GPIO 17
  • TXD pin: GPIO 16
  1. Used a baud rate of 4800 as specified.
  2. Followed the MODBUS commands and code provided in the guide.

Below is the code I used :

#include "Arduino.h"
#define RE 4
#define RXD2 17
#define TXD2 16

const byte humi[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0xe84, 0x0a};
const byte temp[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xd5, 0xca};
const byte cond[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xca};
const byte phph[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0a};
const byte nitro[] = {0x01, 0x03, 0x00, 0x04, 0x00, 0x01, 0xec5, 0xcb};
const byte phos[] = {0x01, 0x03, 0x00, 0x05, 0x00, 0x01, 0xe94, 0x0b};
const byte pota[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0xe64, 0x0b};
const byte sali[] = {0x01, 0x03, 0x00, 0x07, 0x00, 0x01, 0xe35, 0xcb};
const byte tds[] = {0x01, 0x03, 0x00, 0x08, 0x00, 0x01, 0xe05, 0xc8};
byte values[11];

void setup() {
    Serial.begin(4800);
    Serial1.begin(4800, SERIAL_8N1, RXD2, TXD2);
    pinMode(RE, OUTPUT);
    digitalWrite(RE, LOW);
    delay(1000);
}

void loop() {
    float val1, val2, val3, val4, val5, val6, val7, val8, val9;

    //********************* HUMIDITY *************************
    Serial.print("Humidity: ");
    digitalWrite(RE, HIGH);
    delay(10);
    for (uint8_t i = 0; i < sizeof(humi); i++) Serial1.write(humi[i]);
    Serial1.flush();
    digitalWrite(RE, LOW);
    delay(100);
    for (byte i = 0; i < 7; i++) values[i] = Serial1.read();
    val1 = int(values[3] << 8 | values[4]);
    val1 = val1 / 10;
    Serial.print(" = ");
    Serial.print(val1, 1);
    Serial.println(" %");
    delay(200);

    //********************* TEMPERATURE *************************
    Serial.print("Temperature: ");
    digitalWrite(RE, HIGH);
    delay(10);
    for (uint8_t i = 0; i < sizeof(temp); i++) Serial1.write(temp[i]);
    Serial1.flush();
    digitalWrite(RE, LOW);
    delay(100);
    for (byte i = 0; i < 7; i++) values[i] = Serial1.read();
    val2 = int(values[3] << 8 | values[4]);
    val2 = val2 / 10;
    Serial.print(" = ");
    Serial.print(val2, 1);
    Serial.println(" deg.C");
    delay(200);

    //********************* CONDUCTIVITY *************************
    Serial.print("Conductivity: ");
    digitalWrite(RE, HIGH);
    delay(10);
    for (uint8_t i = 0; i < sizeof(cond); i++) Serial1.write(cond[i]);
    Serial1.flush();
    digitalWrite(RE, LOW);
    delay(100);
    for (byte i = 0; i < 7; i++) values[i] = Serial1.read();
    val3 = int(values[3] << 8 | values[4]);
    Serial.print(" = ");
    Serial.print(val3);
    Serial.println(" uS/cm");
    delay(200);

    // Similarly process other sensors: pH, Nitrogen, Phosphorus, Potassium, Salinity, and TDS.
    // Repeat the same logic for the respective commands and values.
    
//********************* Ph *************************
 Serial.print("pH: ");
 digitalWrite(RE, HIGH);
 delay(10);
 for (uint8_t i = 0; i < sizeof(phph); i++ ) Serial1.write( phph[i] );
Serial1.flush();
 digitalWrite(RE, LOW);
 delay(100);
 for (byte i = 0; i < 7; i++) {
 values[i] = Serial1.read();
 // Serial.print(values[i], HEX);
// Serial.print(' ');
 }
val4 = int(values[3]<<8|values[4]);
val4 = val4/10;
 Serial.print(" = ");
 Serial.println(val4,1);
delay(200);
 //********************* NITROGEN *************************
 Serial.print("Nitrogen: ");
 digitalWrite(RE, HIGH);
 delay(10);
 for (uint8_t i = 0; i < sizeof(nitro); i++ ) Serial1.write( nitro[i] );
Serial1.flush();
 digitalWrite(RE, LOW);
 delay(100);
 for (byte i = 0; i < 7; i++) {
 values[i] = Serial1.read();
 // Serial.print(values[i], HEX);
// Serial.print(' ');
 }
val5 = int(values[3]<<8|values[4]);
 Serial.print(" = ");
 Serial.print(val5);
Serial.println(" mg/L");
 delay(200);
//********************* PHOSPHORUS *************************
 Serial.print("Phosphorus: ");
 digitalWrite(RE, HIGH);
 delay(10);
 for (uint8_t i = 0; i < sizeof(phos); i++ ) Serial1.write( phos[i] );
Serial1.flush();
 digitalWrite(RE, LOW);
 delay(100);
 for (byte i = 0; i < 7; i++) {
 values[i] = Serial1.read();
 // Serial.print(values[i], HEX);
// Serial.print(' ');
 }
val6 = int(values[3]<<8|values[4]);
 Serial.print(" = ");
 Serial.print(val6);
Serial.println(" mg/L");
 delay(200);
 //********************* POTASSIUM *************************

 Serial.print("Potassium: ");
 digitalWrite(RE, HIGH);
 delay(10);
 for (uint8_t i = 0; i < sizeof(pota); i++ ) Serial1.write( pota[i] );
Serial1.flush();
 digitalWrite(RE, LOW);
 delay(100);
 for (byte i = 0; i < 7; i++) {
 values[i] = Serial1.read();
 }
val7 = int(values[3]<<8|values[4]);
 Serial.print(" = ");
 Serial.print(val7);
Serial.println(" mg/L");
 delay(200);
 //********************* SALINITY *************************
 Serial.print("Salinity: ");
 digitalWrite(RE, HIGH);
 delay(10);
 for (uint8_t i = 0; i < sizeof(sali); i++ ) Serial1.write( sali[i] );
Serial1.flush();
 digitalWrite(RE, LOW);
 delay(100);
 for (byte i = 0; i < 7; i++) {
 values[i] = Serial1.read();
 // Serial.print(values[i], HEX);
// Serial.print(' ');
 }
val8 = int(values[3]<<8|values[4]);
 Serial.print(" = ");
 Serial.print(val8);
Serial.println(" g/L");
delay(200);
//********************* TDS *************************
 Serial.print("TDS: ");
 digitalWrite(RE, HIGH);
 delay(10);
 for (uint8_t i = 0; i < sizeof(tds); i++ ) Serial1.write( tds[i] );
Serial1.flush();
 digitalWrite(RE, LOW);
 delay(100);
 for (byte i = 0; i < 7; i++) {
 values[i] = Serial1.read();
  // Serial.print(values[i], HEX);
// Serial.print(' ');
 }
val9 = int(values[3]<<8|values[4]);
 Serial.print(" = ");
 Serial.print(val9);
 Serial.println(" mg/L");
 delay(200);

    delay(1000);
}

Observations:

  • I receive no valid data or just random values from the Serial1.read() buffer.
  • Double-checked the wiring and connections as per the guide, but the sensor remains unresponsive.

Questions:

  1. Is there any error in my code, especially in how I handle the RS485 communication or RE/DE toggling?
  2. Could there be a compatibility issue with my ESP32 or sensor?
  3. How can I debug and confirm if the MODBUS commands are being sent correctly?

Any help, suggestions, or troubleshooting steps would be greatly appreciated!

Thank you in advance for your time and expertise.

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1072

Trending Articles