Soil Moisture Sensor - Troubles!

I recently took the plundge and bought an Arduino Uno and soil moisture sensor. Like this one
And used the supplied code, however, when I hook it up do D0 for digital it always reads 0 regardless of whether the sensor is in water or not. And when I hook up the analog the numbers seem to fluctuate randomly regardless of whether the sensor is even connected. As a bit of a new guy to all this im not sure where im going wrong. Any hints?

// Soil Moisture Sensor Hygrometer example code
// Code author:  aafent. This code is copy free, if you want please mention the author's name and/or the site
//Interface Description (4-wire)
//VCC: 3.3v-5v
//GND: GND
//DO: Digital output interface (0 and 1) threshold taken from potentiometer
//AO: Analog output interface

const int moistureAO = 1;
const int moistureDO = 31;

int AO = 0;
int DO = 0;
int tmp = 0;

void setup (){
  Serial.begin(9600);
  Serial.println("Soil moisture sensor");
  pinMode(moistureAO, INPUT);
  pinMode(moistureDO, INPUT);
}

void loop ()
{
  tmp=analogRead( moistureAO );
  if ( tmp != AO ) 
  {
    AO=tmp;
    Serial.print("A=");
    Serial.println(AO);
  }
  
  tmp=digitalRead( moistureDO );
  if ( tmp != DO ) 
  {
    DO=tmp;
    Serial.print("D=");
    Serial.println(DO);
  }
  
  
  delay (1000);

}

Inputs are floating if not connected, they will pick up any electrical noise
from the environment, you need to make sure everything is connected correctly.

Don't use water, use soil. Water can be a very poor conductor(*) if free of salts, this
device is designed to work in soil. Failing that try dissolving some salt in a glass of
water and use that.

(*) the conductivity of tap water from different regions can vary by orders of
magnitude. Conductivity (electrolytic) - Wikipedia

Thanks for the advice!. Sadly even after a mixture of testing with salt water, dry plants, soggy plants the logged results are always the same even if its not inserted into anything, looking like this

Soil moisture sensor
A=357
D=1
A=313
A=281
A=256
A=238
A=224
A=214
A=207
A=202
A=198
A=196
A=195
A=193
A=192
A=191
A=192
A=190
A=192
A=191
A=189
A=187
A=190
A=186
A=189
A=188
A=187
A=191
A=193
A=194
A=192
A=194
A=193
A=194
A=193
A=192
A=195
A=194
A=193
A=194
A=195
A=194
A=195
A=194
A=196
A=197
A=199
A=200
A=202
A=204
A=217
A=216
A=214
A=213
A=212
A=211
A=210
A=209
A=208

I've connected to 3.3v, GRND and A0. Everything seems to "work" im just getting numbers that make no sensce with no diffrence if soil is wet or dry...

If you connect the analog input to ground, does your sketch output a value close to zero? If you connect it to logic HIGH, does it return a value close to 1023? If it does, then the problem is in the way your probe is connected; if it doesn't then the problem is in the Arduino itself or (more likely) your sketch.

1 Like

If you are connecting the analog input to A0 then const int moistureAO = 1; is not required! Just use A0 eg analogRead(A0);

Don't use A0,D0 etc as variable names these are already defined, change them!.

Mark

f you connect the analog input to ground, does your sketch output a value close to zero? If you connect it to logic HIGH, does it return a value close to 1023? If it does, then the problem is in the way your probe is connected; if it doesn't then the problem is in the Arduino itself or (more likely) your sketch.

If I connect it to ground, nothing changes, same results.

I'm not aware what "logic high" means so can't test that

Even if i remove the sensor completely i get the same results :S. Dead sensor?

Todwik:
I'm not aware what "logic high" means so can't test that

It's the voltage that the Arduino's microcontroller is running at. If you're running at 5V, it's 5V. If you're running at 3.3V, it's 3.3V. And so on. It's what you get on an output pin if you set it HIGH.

Try this simple sketch:

/*
  # Example code for the moisture sensor
  # Editor     : Lauren
  # Date       : 13.01.2012
  # Version    : 1.0
  # Connect the sensor analog out to the A0(Analog 0) pin on the Arduino board
  # Connect the sensor VCC pin to 5v pin on Arduino the board
  # Connect the sensor GND pin to GND pin on the Arduino board.
  # the sensor value description
  # 0  ~300     dry soil
  # 300~700     humid soil
  # 700~950     in water
*/

void setup(){
  
  Serial.begin(57600);
  
}

void loop(){
  
  Serial.print("Moisture Sensor Value:");
  Serial.println(analogRead(0));  
  delay(100);
  
}

If im not mistaken a digital pin can only read and write an output/input of high or low. So what that meens, is that it can tell wether there is any tempiture/moisture or not...that thresh hold is most likely predefined by the hardware.

Another iconcisity that i found was the moistureDO was set equal to 31...or the 31st pin. depending on what type of arduino your using most dont even have a 31st pin there for when it tries to read it there is no pin and comes back with zero.

I would try to set the the moistureDO equal to pin 2

hope this helps

If im not mistaken a digital pin can only read and write an output/input of high or low. So what that meens, is that it can tell wether there is any tempiture/moisture or not...that thresh hold is most likely predefined by the hardware.

Another iconcisity that i found was the moistureDO was set equal to 31...or the 31st pin. depending on what type of arduino your using most dont even have a 31st pin there for when it tries to read it there is no pin and comes back with zero.

I would try to set the the moistureDO equal to pin 2

hope this helps

Hmm, even after chaing it to "const int moistureDO = 2" the digital read out ALWAYS reads 1, regardless of how tuned the potentiomiter is. The sensor it self lights up when powered (green led) and when in a moist enviroment a second lights up (red led) but nothing seems to be getting to the arduino.

I think a photo of how you've wired this all up might be useful - or an accurate
diagram. Probably something really basic is wrong...

digital read out ALWAYS reads 1

Well a digital pin can only read high or low, there is no inbetween. therefor it will only show either 0 or 1.

try making setting it to const int moistureDO = 3; and use an analogRead instead of a digital wread and then see what happens

hope this helps

Alex Valente
Dos Pueblos High School
Engineering Academy Class of 2015

hello there.. can you guys help me in coding the source code for soil moisture sensor? i will use it on my study. please do me a favor on it.

May_Lopez1234:
hello there.. can you guys help me in coding the source code for soil moisture sensor? i will use it on my study. please do me a favor on it.

When is our assignment due to be handed in ?