Electric Conductivity Sensor Data

Hey all. My first project is an EC/Soil Salinity sensor using an Arduino nano, OLED display, and Modbus. I followed this tutorial: DIY Soil EC/Salinity Meter || Measure Soil Conductivity & Salinity using Arduino & Soil EC Sensor - YouTube. The EC value stays at -1 instead of changing as it should.

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define RE 8
#define DE 7
 
const byte ec[] = {0x01, 0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xCE};
const byte salinity[] = {0x01, 0x03, 0x00, 0x14, 0x00, 0x01, 0xC4, 0x0E};
byte values[8];
 
SoftwareSerial mod(2, 3);
 
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET    -1  // Reset pin # (or -1 if sharing reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
 
void setup()
{
  Serial.begin(9600);
  mod.begin(9600);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.display();
  delay(100);
  display.clearDisplay();
 
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(20, 30);
  display.print("Soil Analysis");
  display.display();
  delay(2000);
}
 
void loop()
{
/**************Soil EC Reading*******************/  
 digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
 
  if (mod.write(ec, sizeof(ec)) == 8)
  {
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++)
    {
      values[i] = mod.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  int soil_ec = int(values[3]<<8|values[4]);
  delay(1000);
/**************Soil Salinity Reading*******************/ 
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  if (mod.write(salinity, sizeof(salinity)) == 8)
  {
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);
    for (byte i = 0; i < 7; i++)
    {
      values[i] = mod.read();
      Serial.print(values[i], HEX);
    }
    Serial.println();
  }
  int soil_salinity = int(values[4]);
  delay(100);
 
  
 
  Serial.print("Soil EC: ");
  Serial.println(soil_ec);
  
  Serial.print("Soil Salinity: ");
  Serial.println(soil_salinity);
 
 
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print("EC:");
  display.print(soil_ec);
  display.setTextSize(1);
  display.print(" us/cm");
 
   display.setTextSize(2);
  display.setCursor(0,40);
  display.print("SL:");
  display.print(soil_salinity);
  display.setTextSize(1);
  display.print(" mg/L");
 
  display.display();
  
  delay(200);
}

Welcome to the forum, and thanks for using code tags on your first post.

Please post links to the sensor that you bought, to the serial communications adapter you are using and a photo of your wiring.

Does the sensor and serial adapter actually work? Post the values that this statement prints.

      Serial.print(values[i], HEX);

Are you aware that "soil salinity" and EC values are simply related by a constant multiplier (determined by the actual ions present in the sample)?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.