Automatic Watering project

Hello,

I am trying to create a modified version of this project.

The modification being I dont need a servo to move/aim the nozzle output and I only have one sensor / one plant.

https://dl.dropboxusercontent.com/u/726782/AA%20...%20Projects/Arduino/Plant%20Project/2014-09-07%2015.33.15.jpg
https://dl.dropboxusercontent.com/u/726782/AA%20...%20Projects/Arduino/Plant%20Project/2014-09-07%2015.33.59.jpg

So far, I've managed to connect:

1 Arduino Uno
1 Sensor shield

1 Mini water pump
http://www.ebay.co.uk/itm/121425001384?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
1 Soil Hygrometer
http://www.ebay.co.uk/itm/350961143799?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
1 L298N Dual H Bridge Stepper Motor Driver
http://www.ebay.co.uk/itm/281239216460?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

Im pretty sure its wired correctly.

For the soil sensor I connected it up to the Analogue 0 input ... tested this code

// define humidity variable to hold the final value
int humidity = 0;
void setup() {
 Serial.begin(9600);
}
void loop() {
 // read soil moisture from sensor
 int sensorValue = analogRead(A0);
 
 /* constraint function will limit the values we get so we can work better with map
 * since I only need values in the bottom limit of 300, will make it the outer limit and 1023 the other limit
 */
 sensorValue = constrain (sensorValue, 300,1023);
 // print the values returned by the sensor
 //Serial.println(sensorValue);
 // create a map with the values
 // You can think we have the 100% to 300 and 0 % to 1023 wrong, but no !
 // 300 - or 100 % - is the target value for moisture value in the soil
 // 0% of humidity - or moisture - is when the soil is dry
 humidity = map (sensorValue, 300, 1023, 100, 0);
 Serial.print (humidity);
 Serial.println ("%");
 delay(1000); //collecting values between seconds
 }

on it to make sure it all works ... and it does, I opened up the serial monitor and it correctly displays the moisture percentage level

Now for the bit I'm struggling with ...

I left the moisture sensor as is, and connected up the motor driver

Driver <--> Sensor Shield
??IN3 <--> D6
??IN4 <--> D7
??+5V <--> VIN
??GND <--> GND

I used the following code

#define triger 250
//Define the parameters of the pump
#define triggerValue0 135  //the trigger value of the moisture, the minimum value is 0, means the moisture is 0, the soil is very dry. the maximum value is 1024.
#define wateringTime0 35  //the time for watering, millisecond. delay 35ms, uses about 70ML water

//Define the moisture sensor pins
const int analogInPin0 = A0;  // 

//the value read from each moisture sensor
int moistureValue0 = 0;        

//the sum of the 30 times sampling
long int moistureSum0 = 0;   //sampling the moisture 30 times to get its average value, this variable is used to store the sum of the 30 times sampled value

//Define the water pump control pins
const int pumpAnodePin =  6;      //pin 6 connect to the anode of the pump
const int pumpCathodePin =  7;   //pin 7 connect to the cathode of the pump

void setup() 
{ 
  Serial.begin(9600); 
  delay(500);
  pinMode(pumpAnodePin, OUTPUT);
  pinMode(pumpCathodePin, OUTPUT);
} 

void loop() 
{
  
int   
  moistureSampling();
 
  if(moistureValue0 < triggerValue0)
  {
    proWatering(wateringTime0);
    return;
   } 
   
}

void moistureSampling()// read the value of the soil moisture
{
  for(int i = 0; i < 30; i++)//sampling 30 time within 3 seconds
  {
    moistureSum0 = moistureSum0 + analogRead(analogInPin0);  
    delay(100);
  }
  moistureValue0 = moistureSum0 / 30;//get the average value

  // print the results to the serial monitor:
  Serial.print("Moisture0 = " );                       
  Serial.print(moistureValue0);      
  Serial.println(); 
  
  moistureSum0 = 0;//reset the variable
  delay(4000);     //delay 4 seconds 
}


void proWatering(int wateringTime)  //sweep watering 
{
  digitalWrite(pumpAnodePin, HIGH);  //the pump start working
  digitalWrite(pumpCathodePin, LOW);
  delay(5000);
  digitalWrite(pumpAnodePin, LOW);
  digitalWrite(pumpCathodePin, LOW);
  
}

void watering(int wateringTime)
{
  delay(1000);
 digitalWrite(pumpAnodePin, HIGH);
  delay(10000);
  digitalWrite(pumpAnodePin, LOW);
  delay(3000);
}

When I monitor the serial ... it reads 1024 when there is no moisture, and a lower value when there is ... why is this happening ?

Two codes, does different calculations with sensor readings. This is why you getting different values.

Hi Omer, thanks for your reply, Im a little bit lost with what you mean ... do you mean between the two codes I posted? If so, yes I know they achieve different results ... Im just using the second code, and from what I understand, the soil moisture sensor should be reading 0 when dry, and 1024 when completely wet ... but it seems to be the opposite ?

int   
  moistureSampling();

Despite being on two lines, this code defines a function prototype. It does NOT call the function. The function prototype is useless here.

    return;

As opposed to doing what?

I don't even know WHY motors are used ? I think a pump / sensor / arduino would be enough - and for a single pump it would be better to use a single motor driver .

laithmarmash:
Hi Omer, thanks for your reply, Im a little bit lost with what you mean ... do you mean between the two codes I posted? If so, yes I know they achieve different results ... Im just using the second code, and from what I understand, the soil moisture sensor should be reading 0 when dry, and 1024 when completely wet ... but it seems to be the opposite ?

There is a difference between sensor reading and calculated value. You are seeing a calculated value of your sensor readings on serial monitor, you can use any calculation or don't use a calculation at all. Simply read a sensor value with analogRead();. You may ask why i need calculations? It depends what you want to do with sensor readings. You can choose to get value readings as percents or "map" reading between 0 to 10 etc...

Hi All,

Many thanks for all your responses!

PaulS - regarding the function prototype, I thought I would need it for the average value calculation ?

regarding the return ... ah, I didnt realise I didnt need it if there are no other conditions.

Arman - I need motors drivers ... because they are motor pumps and this seemed like the easiest way to drive them (ultimately there will be 2)

Omer,many thanks, I will be reading the raw values for this ... so please ignore the first code ... thats was something I just cut and pasted to test that the moisture sensor worked.

All ... if its ok, I would like to cover this one step at a time ... does anyone know why I am getting 1024 values off the serial print output ... when the sensor is dry, and lower values when it is wet ? shouldnt it be the other way around.

PaulS, again, thanks for your input, the revised code is as follows

#define triger 250
#define triggerValue 580  //the trigger value of the moisture, the minimum value is 0, means the moisture is 0, the soil is very dry. the maximum value is 1024.
#define wateringTime 35  //the time for watering the flower1, millisecond. delay 35ms, uses about 70ML water

int analogInPin = A0;  

int moistureValue=0;        
long int moistureSum=0 ; 

 int pumpAnodePin =  6;      //pin 6 connect to the anode of the pump
 int pumpCathodePin =  7;   //pin 7 connect to the cathode of the pump

void setup() 
{ 
  Serial.begin(9600); 
  delay(500);
  pinMode(pumpAnodePin, OUTPUT);
  pinMode(pumpCathodePin, OUTPUT);
} 

void loop() 
{
    
  delay(1000);
  moistureSampling();
 
  if(moistureValue > triggerValue)
  {
    proWatering(wateringTime);
    return;
   } 
   
}

void moistureSampling()// read the value of the soil moisture
{
  for(int i = 0; i < 30; i++)//samping 30 time within 3 seconds
  {
    moistureSum = moistureSum + analogRead(analogInPin);  
    delay(100);
  }
  moistureValue = moistureSum / 30;//get the average value
  // print the results to the serial monitor:
  Serial.print("Moisture = " );                       
  Serial.print(moistureValue);      
 
  Serial.println(); 
  
  moistureSum = 0;//reset the variable

  delay(4000);     //delay 4 seconds 
}


void proWatering(int wateringTimea)  //sweep watering 
{
  
  digitalWrite(pumpAnodePin, HIGH);  //the pump start working
  digitalWrite(pumpCathodePin, LOW);
  
  delay(wateringTimea);
  digitalWrite(pumpAnodePin, LOW);
  digitalWrite(pumpCathodePin, LOW);


}

Hello all, can someone help me with this ?

Two hours -- and you're impatient. Wait at least a day before "bump"ing your thread.

PualS' comment is that you defined a prototype, where you probably intended to define a function. In the Arduino IDE, it automatically generates prototypes, so you dont need to. This is so you do not have to worry about the order of defining your functions.

It may well be the sensor reads lower values for more moisture, as longs as it notices the difference. You're not doing an absolute measurment. You just test for high value, rather than a low.

What are you on about !?

Last post: September 08, 2014, 12:22:01 pm
"Bump": Today at 02:30:50 pm

How is that 2 hours ?

Anyway, thanks for your reply and clarification on the prototype. That makes sense.

Good call on reversing the values, I'll give that a crack.

laithmarmash:
Last post: September 08, 2014, 12:22:01 pm "Bump": Today at 02:30:50 pm

My appologies - I missed the date change.

Hello all, can someone help me with this ?

Hey, everybody, I've got some new code. I'm not going to tell you what it does, or what problems I'm having, but I need some help.

Did I paraphrase your post correctly?

All ... if its ok, I would like to cover this one step at a time ... does anyone know why I am getting 1024 values off the serial print output ... when the sensor is dry, and lower values when it is wet ? shouldnt it be the other way around.

This is the problem, but i can't figure it out.

I think you should, connect your sensor alone. And read values with different conditions. Maybe sensor working exactly how it is working.

You said

on it to make sure it all works ... and it does, I opened up the serial monitor and it correctly displays the moisture percentage level

Maybe you want use like that.

MSquare - no worries, thanks for the suggestion, its fixed my problem.

PaulS - Love the sarcasm ... I'm pleased to see it made it across the pond.

If you worked your way through the messages you'll come across my original problem (which you kindly replied to but never actually helped me fix). You may even come across the latest code I'm using! Seeing as I haven't posted another issue since my original one, common sense would say I'm still having the same problem.

(For your convenience ... the serial feedback readings off the hygrometer were the wrong way around).

Msquare kindly highlighted that as long as I'm getting variations off the readings (regardless of which way around they are) I could get it to work, so I've just flipped the conditions and all is good.

Maybe instead of making sarcastic comments that don't actually help peoples problems, you could actually offer some help? ... just a thought!

Omer - many thanks for the suggestion, I decided to go back to the raw data and it seems to be working well!

Thanks again all for your help. ! I'll probably be back when I'm trying to get the lights and the RTC working !

laithmarmash:
All ... if its ok, I would like to cover this one step at a time ... does anyone know why I am getting 1024 values off the serial print output ... when the sensor is dry, and lower values when it is wet ? shouldnt it be the other way around.

According to the eBay page you linked:

This is a summary of the soil moisture sensor can be used to detect moisture, when the soil is dry, the module outputs a high level, whereas output low.

It says it outputs a high level when the soil is dry, which is what you have found, right?

haha ! You're right Nick ... there was me thinking there was something wrong with my code !! Thanks :slight_smile: