XCSource Moisture Sensor - Strange Anolog inout readings

Hi All, I’ve been using this instructable to attempt my first project on arduino uno using arduino 4 (i’m using an old imac and the latest version wasn’t supported) - http://www.instructables.com/id/No-Pump-Automatic-Watering/?ALLSTEPS.

I’ve followed his wiring schematic as per below:

Analog Input 0 - Sensor Pin
Digital I/O 2 - Sensor + (This output pin is configured to power the sensor).
GND - Sensor GND
Digital I/O 3 - Orange Servo Wire
5V - Red Servo Wire
GND - Brown Servo Wire

The code used is as per his download - although I amended the delays from
delay(51000) to (31000) and delay(5601000) to delay(511000) to speed up the loop to try and understand the readings and see if I need to amend the wet and dry values.

I’m struggling with understanding the moisture sensor readings. I was expecting a 0-1023 reading but I’m getting things like 289.30 when wet, then 1003.00 when dry, then Sensor value: 1104.301104.30. As per the screenshot. Is that due to the averaging?

When that changes the servo kicks in and spins rather than moving from the assigned value.

Any help would be much appreciated.

FYI - i’m using this moisture sensor and servo

#include <Servo.h>  
Servo waterServo;  //creates the name of your servo
int sensPin = 0;  //assignes a pin for the soil sensor
int servoPin = 3; //sets the servo pin, this must be a PWM pin
int powerPin = 2; //assignes a pin to power the sensor. optional
int wetVal = 500; //the sensor value where the soil is sufficiently wet
int dryVal = 300; //the value where the soil is dry and needs watering
int dirtVal;    //variable to store one sensor reading
int waterPosition = 170;
int waterHold = 10;
double avgDirt; //variable to store average of 10 sensor readings

void setup() 
{
  Serial.begin(9600);   //serial monitor for debugging purposes
  waterServo.attach(servoPin);  //sets the servo pin as output
  pinMode(sensPin, INPUT);  //sets the sensor pin as input
  pinMode(powerPin, OUTPUT);
  delay(10);
  digitalWrite(powerPin, HIGH); //provides power to the sensor
}

void loop() 
{
  for(int i = 0; i < 10; i++)   //for loop to calculate average of ten soil readings
  {
    dirtVal = analogRead(sensPin);
    delay(3*1000);
    avgDirt += dirtVal;
  }
  avgDirt = avgDirt / 10.0;     //gets average value of 10 sensor readings
  Serial.print("Sensor value: "); //for debugging
  Serial.print(avgDirt);

  if(avgDirt <= dryVal)   //if the average soil value is dry
  {
    waterServo.write(waterPosition);  //start watering the plants
    while(dirtVal <= wetVal)  //while loop to keep watering until the soil is wet
    {
      dirtVal = analogRead(sensPin);
      Serial.println(avgDirt);
      delay(20);
    }
    waterServo.write(waterHold);  //stops watering the plants
    avgDirt = dirtVal;  //resets average soil value
  }
  Serial.println(avgDirt);
  delay(5*1*1000);
}

The values with fractions result from averaging. The Sensor value is output twice in sequence, without a delimiter. Replace the first print(avgDirt) by println(avgDirt).

The soil is never found dry, that’s why avgDirt is never updated. Check your comparisons and values. If dryVal is lower than wetVal, the second comparison (dirtVal <= wetVal) will always be true if the first one (dirtVal <= dryVal) is true.

For the servo, move it to waterHold position in setup().