immikimaru:
It has a digital output, but only triggers 1 or 0 if the moisture level is reached ... That is not really what i want. I want a precise value of the soil moisture :S
Did you ever get your setup working?
I bought some similar sensors but they have digital and analog outputs. I have my code posted in my blog here:
This has all of the materials and what not that I was using. Hopefully you got a setup working but if not, this will do the trick. I am only using the analog portion of my sensor output to have a measurement via the LED of how "moist" the soil is. The actual watering still happens based on the digital output and adjustment of the potentiometer but that statement in the code could easily be modified for the 0-100% value of the mapped analog sensor output.
Also, here is what I used:
Arduino Code:
//ββββStart All Code Hereββββ-
#include <Servo.h>
Servo waterServo; //creates the name of servo
int val = 0; //holds the value of the digital sensor reading
int val2 = 0; //used to activate the watering after 5 loops of being βdryβ
int servoPin = 3; //sets servo pin, must be a PWM pin for activating servo position
int waterPosition = 110; //watering position of the servo, in degrees from vertical
int waterHold = 0; //holding position to return to when done watering
int output_value; //this is the output value used in the analog sensor measurement of moisture
int sensor_pin = A0; //this is the analog input on the board from the sensor
//βββββββββββ
void setup() {
pinMode(4,INPUT); //enable digital pin 4 for sensor input readings
pinMode(7,OUTPUT); //enable digital pin 7 output for sensor power
pinMode(13,OUTPUT); //enable on-board LED
Serial.begin(9600); //start serial output at 9600 bitrate β for readings
delay(50);
Serial.println(βStarting Codeβ); //notes in serial monitor
waterServo.attach(servoPin); //sets the servo pin as output
delay(50);
waterServo.write(waterHold); //sets the servo to the holding position specified at start
digitalWrite(7,HIGH); //set PIN 7 high to power the moisture sensor
}
//βββββββββββ
void loop() {
//digital
delay(1800*1000);
/include commented out β000);β in order to include the
30 minute delay between readings/
//analog reading of moisture levels from sensor
output_value= analogRead(sensor_pin);//read analog values from sensor
output_value = map(output_value,980,250,0,100);//map analog raw values to the scale of the sensor
Serial.print(βMoisture: β); //print in serial monitor
Serial.print(output_value);
Serial.println(β%β);
output_value = output_value/10; //divide output_value by 10 for LED blinking purposes
for (int led=0;led<=output_value;led++){
digitalWrite(13,1);//turn on the on-board LED (and the wired LED if using one)
delay(250);
digitalWrite(13,0);//turn off the on-board LED (and the wired LED if using one)
delay(250);
}
val = digitalRead(4); //read soil moisture sensor digital output
//Serial.println(val);
if(val == 1){ // if soil is dry, increment val2 in order to keep from watering too often
val2++;// = val2 + 1;
}
if(val2 == 5){ //if soil is dry for 5 consecutive readings, add water
waterServo.write(waterPosition); //start watering
delay(2*1000); //allow watering to occur for 2 seconds
val2 = 0;
//ββ
//this is used to make the servo less jerky
//when returning to original position to stop water flow
for (int s=waterPosition; s>=waterHold; sβ){
waterServo.write(s);
delay(10);
} //end of for loop to control servo return speed
//ββ
}// end of if statement to check for 5 consecutive readings of βdryβ condition
}//end of loop
//ββββEnd All Code Hereββββ-