xerivar
Published © GPL3+

Automatic watering

Moisture controlled watering system

IntermediateFull instructions provided3,460
Automatic watering

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Datalogger shield
×1
12V Solenoid Valve - Normally closed
×1
12V 2A switching power supply
×1
Connector Adapter, DC Power - 2.1mm
Connector Adapter, DC Power - 2.1mm
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Terminal block - 12 positions
×2
Flash Memory Card, SDHC Card
Flash Memory Card, SDHC Card
×1
Panasonic CR1220 - 3V Lithium Coin Cell Battery
×1
4 CHANNEL 5V RELAY MODULE
×1
3 pin plug
×1
Capacitive Soil Moisture Sensor
×4
Plastic Enclosure, Project Box
Plastic Enclosure, Project Box
×1
Holesaw
×1
SCHNEIDER ELECTRIC XB7-NH-5 NSNP
×1
SCHNEIDER ELECTRIC XB7-NA5 NSNP
×1
Wires
×1
Sprinkler System
×1
Pressure Pump
×1

Hand tools and fabrication machines

Drill / Driver, Cordless
Drill / Driver, Cordless
Multitool, Screwdriver
Multitool, Screwdriver
Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter

Story

Read more

Schematics

Automatic Watering

I tried my best to show how it all connected. Just refer to schematics. Importantly, I used ground (GND) from the 12V switching power supply and connected to the Arduino Uno board.

Code

Automatic Watering

Arduino
Moisture sensors will detect whether the soil is dry, if that is the case, the controller will turn on both pressure pump and solenoid valve. You can manually turn on the watering system by pressing momentary push button. Another push button is used to switch on the whole controller. (Note that even if the pump is will stop running at night, if the controller wants to water the plant, the controller will power the pump.)
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>

const char* filename = "datalog.csv";
File file;
RTC_DS1307 rtc;
char Time[20];

const int dry1 = 544; // Minimum sensor1 dryness to turn electric valve on
const int wet1 = 270; // Maximum sensor1 wetness to turn electric valve off

const int dry2 = 544; // Minimum sensor2 dryness to turn electric valve on
const int wet2 = 270; // Maximum sensor2 wetness to turn electric valve off

const int dry3 = 544; // Minimum sensor3 dryness to turn electric valve on
const int wet3 = 270; // Maximum sensor3 wetness to turn electric valve off

const int dry4 = 544; // Minimum sensor4 dryness to turn electric valve on
const int wet4 = 270; // Maximum sensor4 wetness to turn electric valve off

const int LowMoisture = 20;    // Minimum moisture level before misting
const int HighMoisture = 75;   // Maximum moisture level before counting 1 
const int maxwatercount = 2;   // Maximum times to water plants a day

const int pps= 2;              // Setting pressure pump switch to Pin 2
const int ms = 3;              // Setting manual watering switch to Pin 3
const int ev = 4;              // Setting electrical valve to Pin 4
const int pp = 5;              // Setting pressure pump to Pin 5

int watercount = 0;      // Counting the number of watering sessions
int watercond = 0;       // Condition to water (TRUE or FALSE) 

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(__DATE__, __TIME__));

  pinMode(pps,INPUT);      // PRESSURE PUMP SWITCH
  pinMode(ms,INPUT);       // MANUAL SWITCH
  pinMode(ev,OUTPUT);      // ELECTRICAL VALVE
  pinMode(pp,OUTPUT);      // PRESSURE PUMP
  pinMode(10,OUTPUT);      // SD WRITE
  digitalWrite(ev,HIGH);   // TURN VALVE OFF INITIALLY
  digitalWrite(pp,HIGH);   // TURN PRESSURE PUMP OFF INITIALLY
    
  if (!SD.begin(10)) {
    Serial.println("Error : Push the reset button");
    for (;;);
  }  
  file = SD.open(filename, FILE_WRITE); 
  if (file.size() == 0) {
    file.print("Watering Time, Moisture 1, Moisture 2, Moisture 3, Moisture 4, Water count");
  }
} 

void loop() {
  
  int manualState = digitalRead(ms);           // Read the INPUT VOLTAGE OF PIN 3
  int ppState = digitalRead(pps);              // READ THE INPUT VOLTAGE OF PIN 2
  int ActppSt = digitalRead(pp);               // READ ACTUAL PUMP STATE
  int sensorVal1 = analogRead(A0);             // READ THE MOISTURE SENSOR 1
  int sensorVal2 = analogRead(A1);             // READ THE MOISTURE SENSOR 2
  int sensorVal3 = analogRead(A2);             // READ THE MOISTURE SENSOR 3
  int sensorVal4 = analogRead(A3);             // READ THE MOISTURE SENSOR 4

  int Moisturelvl1 = map(sensorVal1, wet1, dry1, 100, 0);
  int Moisturelvl2 = map(sensorVal2, wet2, dry2, 100, 0);
  int Moisturelvl3 = map(sensorVal3, wet3, dry3, 100, 0);
  int Moisturelvl4 = map(sensorVal4, wet4, dry4, 100, 0);



  if ((Moisturelvl1<=LowMoisture)||(Moisturelvl2<=LowMoisture)||(Moisturelvl3<=LowMoisture)||(Moisturelvl4<=LowMoisture)) {
    watercond = 1;                 // Condition to water
  }
  else if ((Moisturelvl1>=HighMoisture)||(Moisturelvl2>=HighMoisture)||(Moisturelvl3>=HighMoisture)||(Moisturelvl4>=HighMoisture)){
    watercond = 0;                // Condition to stop water
  }
  
  DateTime now = rtc.now();
  sprintf(Time, "%02d:%02d:%02d %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
  
  if ((now.hour()==0) && (now.minute()==0)){
  watercount = 0;
  }

  Serial.print(Time);
  Serial.print("  ");
  Serial.print(Moisturelvl1);
  Serial.print(",");
  Serial.print(Moisturelvl2);
  Serial.print(",");
  Serial.print(Moisturelvl3);
  Serial.print(",");
  Serial.print(Moisturelvl4);
  Serial.print(", Watercount:");
  Serial.print(watercount);
  Serial.print(", Manual Switch State:");
  if (manualState == HIGH){
  Serial.print ("OFF");
  }
  else {
  Serial.print ("ON");
  }
  Serial.print(", Pump Switch State:");
  if (ppState == HIGH){
  Serial.print ("OFF");
  }
  else {
  Serial.print ("ON");
  }
  Serial.print(", Pump State:");
  if (ActppSt == HIGH){
  Serial.println ("OFF");
  }
  else {
  Serial.println ("ON");
  }

  if (ppState == LOW){
    if ( now.hour()>=7 && now.hour()<=23){
          digitalWrite(pp,LOW);      // TURN PRESSURE PUMP ON
          delay(200);
    }
  }
  
  else {
    digitalWrite(pp,HIGH);          // TURN PRESSURE PUMP OFF
    delay(200);
  }
  
if (ppState == LOW ){              // ONLY START THE AUTO-WATERING WHEN PPS IS ON
  if (manualState == LOW || watercount<maxwatercount){
    if (manualState == LOW || watercond == 1){
      
       if (file) {
        file.print(Time);
        file.print(",");
        file.print(Moisturelvl1);
        file.print(",");
        file.print(Moisturelvl2);
        file.print(",");
        file.print(Moisturelvl3);
        file.print(",");
        file.print(Moisturelvl4);
        file.print(",");
        file.println(watercount);
        file.flush();
        }
       else {
        Serial.println("Error opening datalog.txt");
         }
         
       digitalWrite(ev,LOW);                 // TURN VALVE ON
       digitalWrite(pp,LOW);                 // TURN PRESSURE PUMP ON
       delay(300000);                        // WATERING TIMER (5 MINS)
       watercount = watercount+1;            // ADD THE WATERCOUNT FOR THIS DAY
       
    }
      else {
       digitalWrite(ev,HIGH);             // TURN VALVE OFF
       delay(200);
       }
  }
}

  else {
  digitalWrite(ev,HIGH);                // TURN VALVE OFF
  delay(200);
  }

  delay(600);
}

Credits

xerivar
0 projects • 2 followers

Comments