Moisture Sensor (LM393) about faced results

I am using thsi moisture sensor as part of a greenhouse control system (trying at least). :( For some reason that has me baffled the readings for wet or dry are opposite of what you would expect.

600 shows 'dry' and 'wet' around 200. Yet, when I view all the other people's coding and results they manaage to show 0 as the dry state >500 for the wet state. I have tried a couple of new components and the results are the same. Another point, is what the fitted pot is supposed to do, turning it makes no difference to the readings! What the hell !! I've attached my code for completeness and as you will see I have massaged my sketch reflecting my very special results. :confused:

It would help me not to live in a confused state, thanks for looking and perhaps also helping me.

Serial.begin(9600); // open serial over USB
pinMode(soilPower, OUTPUT);// Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);// Set to LOW so no power is flowing through the sensor
pinMode(drySoil, OUTPUT);
pinMode(wetSoil, OUTPUT);
void loop()
if(readSoil() > 600){
digitalWrite(wetSoil, LOW);
digitalWrite (drySoil, HIGH);
digitalWrite(drySoil, LOW);
digitalWrite (wetSoil, HIGH);
Serial.print("Soil Moisture = "); //get soil moisture value from the function below and print it
//This 1 second time frame is used so you can test the sensor and see it change in real-time.
//For in-plant applications, you will want to take readings much less frequently.
delay(2000); //take a reading every second
//This is a function used to get the soil moisture content
int readSoil()
digitalWrite(soilPower, HIGH); //turn D7 "On"
delay(10); //wait 10 milliseconds
val = analogRead(soilPin); //Read the SIG value form sensor
digitalWrite(soilPower, LOW); //turn D7 "Off"
return val ;// send current moisture value

The sketch you post does not compile

Pleas uset CTRL-T in the IDE to autoformat and update your post to include the whole sketch

Sorry about that - code now below -

#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HTI2CLCD.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
// LCD address 0x27 20 X 4 lines
#define DHTPIN A0     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11 

int d = 0;
float h = 0.00;
float t = 0.00;
float v = 0.00;
float s = 0.0;
int soilPin = A2;
int pumpPin = 7;
int soilLed = 9;
void setup()
  digitalWrite (pumpPin, LOW);
  pinMode (soilLed, OUTPUT);

  //lcd.setCursor(0, 0);
  //lcd.print("IN SET-UP");
  //  First Screen
  lcd.setCursor(2, 0);
  lcd.print("DHT11  -  OK");
  lcd.setCursor(2, 1);
  lcd.print("LM393  -  OK");
  lcd.setCursor(2, 2);
  lcd.print(" ");
  lcd.setCursor(2, 3);
  lcd.print("FIRST SCREEN");

  lcd.setCursor(2, 2);
  lcd.print("SECOND SCREEN");
  lcd.setCursor(0, 0);
  lcd.print("Temp       C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity   %");
  lcd.setCursor(0, 2);
  lcd.print("Soil        ");
  lcd.setCursor(0, 3);
  lcd.print("sensor  ");

void loop()
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  // digitalWrite(soilPin, LOW);
  //digitalWrite(soilPin, HIGH);
  //digitalWrite (soilLed, HIGH);
  s = analogRead(soilPin);
  float v = analogRead(soilPin) / 10;
  if (s < 50) digitalWrite(soilLed, HIGH);
    digitalWrite(soilLed, LOW);
  lcd.setCursor(13, 0);
  lcd.setCursor(13, 1);
  lcd.setCursor(13, 2);
  lcd.setCursor(13, 3);


Dannyr: For some reason that has me baffled the readings for wet or dry are opposite of what you would expect.

What soil sensor. If the sensor is connected between analogue pin and ground, then the A/D value goes down with moisture. If connected between pin and 5volt, the value goes up with moisture. If readout is wrong, it can be easilly flipped with code. Leo..

Thanks for the response. The sensor is one with two probes connected to an LM393 and it is conected via pin A0 and ground. So, yes I flipping the readout in the code. I still have the problem with the pot which when tuned has no effect on the readings so, I can’t adjust the sensor. It is either high or low and nothing inbetween. It is the same with other new sensors of the same type. The soil is either dry or wet and nothing else. Bummer really!

What sensor (I think I already asked that).

The LM339 is most likely for the digital output only. The pot sets the wet/dry threshold, where the digital output changes from high to low. It has no influence on the analogue output.

Just read the A/D value of the sensor with an analogue pin, and convert that value into a percentage, or say five moisture stages. The 'map' function can do that, as well as flip polarity. See post 320-322 here: Leo..

There are 2 outputs, Digital and Analog.
The analog figure for dry for instance is 1023 and suspended in water (wet) maxes out at 328.

The analog figure for dry (digital pin) for instance is 1023 (in air) and suspended in water (wet) maxes out at 1023.


The Digital output is either High (close to Vcc volts) or low (close to 0) volts) the switch over point is set with the pot, connect it to a DIGITAL pin. It does not should not affect the analog reading.