Components and supplies
CR1220 - 3V Lithium Coin Cell Battery
Capacitive Soil Moisture Sensor
Male/Female Jumper Wires
12V Solenoid Valve - Normally closed
Connector Adapter, DC Power - 2.1mm
Pressure Pump
12V 2A switching power supply
Terminal block - 12 positions
Flash Memory Card, SDHC Card
Holesaw
SCHNEIDER ELECTRIC XB7-NA5 NSNP
Plastic Enclosure, Project Box
3 pin plug
SCHNEIDER ELECTRIC XB7-NH-5 NSNP
Datalogger shield
Sprinkler System
4 CHANNEL 5V RELAY MODULE
Arduino UNO
Wires
Tools and machines
Drill / Driver, Cordless
Multitool, Screwdriver
Mastech MS8217 Autorange Digital Multimeter
Project description
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.)
1#include <RTClib.h> 2#include <SPI.h> 3#include <SD.h> 4#include <Wire.h> 5 6const char* filename = "datalog.csv"; 7File file; 8RTC_DS1307 rtc; 9char Time[20]; 10 11const int dry1 = 544; // Minimum sensor1 dryness to turn electric valve on 12const int wet1 = 270; // Maximum sensor1 wetness to turn electric valve off 13 14const int dry2 = 544; // Minimum sensor2 dryness to turn electric valve on 15const int wet2 = 270; // Maximum sensor2 wetness to turn electric valve off 16 17const int dry3 = 544; // Minimum sensor3 dryness to turn electric valve on 18const int wet3 = 270; // Maximum sensor3 wetness to turn electric valve off 19 20const int dry4 = 544; // Minimum sensor4 dryness to turn electric valve on 21const int wet4 = 270; // Maximum sensor4 wetness to turn electric valve off 22 23const int LowMoisture = 20; // Minimum moisture level before misting 24const int HighMoisture = 75; // Maximum moisture level before counting 1 25const int maxwatercount = 2; // Maximum times to water plants a day 26 27const int pps= 2; // Setting pressure pump switch to Pin 2 28const int ms = 3; // Setting manual watering switch to Pin 3 29const int ev = 4; // Setting electrical valve to Pin 4 30const int pp = 5; // Setting pressure pump to Pin 5 31 32int watercount = 0; // Counting the number of watering sessions 33int watercond = 0; // Condition to water (TRUE or FALSE) 34 35void setup() { 36 Serial.begin(9600); 37 Wire.begin(); 38 rtc.begin(); 39 rtc.adjust(DateTime(__DATE__, __TIME__)); 40 41 pinMode(pps,INPUT); // PRESSURE PUMP SWITCH 42 pinMode(ms,INPUT); // MANUAL SWITCH 43 pinMode(ev,OUTPUT); // ELECTRICAL VALVE 44 pinMode(pp,OUTPUT); // PRESSURE PUMP 45 pinMode(10,OUTPUT); // SD WRITE 46 digitalWrite(ev,HIGH); // TURN VALVE OFF INITIALLY 47 digitalWrite(pp,HIGH); // TURN PRESSURE PUMP OFF INITIALLY 48 49 if (!SD.begin(10)) { 50 Serial.println("Error : Push the reset button"); 51 for (;;); 52 } 53 file = SD.open(filename, FILE_WRITE); 54 if (file.size() == 0) { 55 file.print("Watering Time, Moisture 1, Moisture 2, Moisture 3, Moisture 4, Water count"); 56 } 57} 58 59void loop() { 60 61 int manualState = digitalRead(ms); // Read the INPUT VOLTAGE OF PIN 3 62 int ppState = digitalRead(pps); // READ THE INPUT VOLTAGE OF PIN 2 63 int ActppSt = digitalRead(pp); // READ ACTUAL PUMP STATE 64 int sensorVal1 = analogRead(A0); // READ THE MOISTURE SENSOR 1 65 int sensorVal2 = analogRead(A1); // READ THE MOISTURE SENSOR 2 66 int sensorVal3 = analogRead(A2); // READ THE MOISTURE SENSOR 3 67 int sensorVal4 = analogRead(A3); // READ THE MOISTURE SENSOR 4 68 69 int Moisturelvl1 = map(sensorVal1, wet1, dry1, 100, 0); 70 int Moisturelvl2 = map(sensorVal2, wet2, dry2, 100, 0); 71 int Moisturelvl3 = map(sensorVal3, wet3, dry3, 100, 0); 72 int Moisturelvl4 = map(sensorVal4, wet4, dry4, 100, 0); 73 74 75 76 if ((Moisturelvl1<=LowMoisture)||(Moisturelvl2<=LowMoisture)||(Moisturelvl3<=LowMoisture)||(Moisturelvl4<=LowMoisture)) { 77 watercond = 1; // Condition to water 78 } 79 else if ((Moisturelvl1>=HighMoisture)||(Moisturelvl2>=HighMoisture)||(Moisturelvl3>=HighMoisture)||(Moisturelvl4>=HighMoisture)){ 80 watercond = 0; // Condition to stop water 81 } 82 83 DateTime now = rtc.now(); 84 sprintf(Time, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year()); 85 86 if ((now.hour()==0) && (now.minute()==0)){ 87 watercount = 0; 88 } 89 90 Serial.print(Time); 91 Serial.print(" "); 92 Serial.print(Moisturelvl1); 93 Serial.print(","); 94 Serial.print(Moisturelvl2); 95 Serial.print(","); 96 Serial.print(Moisturelvl3); 97 Serial.print(","); 98 Serial.print(Moisturelvl4); 99 Serial.print(", Watercount:"); 100 Serial.print(watercount); 101 Serial.print(", Manual Switch State:"); 102 if (manualState == HIGH){ 103 Serial.print ("OFF"); 104 } 105 else { 106 Serial.print ("ON"); 107 } 108 Serial.print(", Pump Switch State:"); 109 if (ppState == HIGH){ 110 Serial.print ("OFF"); 111 } 112 else { 113 Serial.print ("ON"); 114 } 115 Serial.print(", Pump State:"); 116 if (ActppSt == HIGH){ 117 Serial.println ("OFF"); 118 } 119 else { 120 Serial.println ("ON"); 121 } 122 123 if (ppState == LOW){ 124 if ( now.hour()>=7 && now.hour()<=23){ 125 digitalWrite(pp,LOW); // TURN PRESSURE PUMP ON 126 delay(200); 127 } 128 } 129 130 else { 131 digitalWrite(pp,HIGH); // TURN PRESSURE PUMP OFF 132 delay(200); 133 } 134 135if (ppState == LOW ){ // ONLY START THE AUTO-WATERING WHEN PPS IS ON 136 if (manualState == LOW || watercount<maxwatercount){ 137 if (manualState == LOW || watercond == 1){ 138 139 if (file) { 140 file.print(Time); 141 file.print(","); 142 file.print(Moisturelvl1); 143 file.print(","); 144 file.print(Moisturelvl2); 145 file.print(","); 146 file.print(Moisturelvl3); 147 file.print(","); 148 file.print(Moisturelvl4); 149 file.print(","); 150 file.println(watercount); 151 file.flush(); 152 } 153 else { 154 Serial.println("Error opening datalog.txt"); 155 } 156 157 digitalWrite(ev,LOW); // TURN VALVE ON 158 digitalWrite(pp,LOW); // TURN PRESSURE PUMP ON 159 delay(300000); // WATERING TIMER (5 MINS) 160 watercount = watercount+1; // ADD THE WATERCOUNT FOR THIS DAY 161 162 } 163 else { 164 digitalWrite(ev,HIGH); // TURN VALVE OFF 165 delay(200); 166 } 167 } 168} 169 170 else { 171 digitalWrite(ev,HIGH); // TURN VALVE OFF 172 delay(200); 173 } 174 175 delay(600); 176}
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.)
1#include <RTClib.h> 2#include <SPI.h> 3#include <SD.h> 4#include 5 <Wire.h> 6 7const char* filename = "datalog.csv"; 8File file; 9RTC_DS1307 10 rtc; 11char Time[20]; 12 13const int dry1 = 544; // Minimum sensor1 dryness to 14 turn electric valve on 15const int wet1 = 270; // Maximum sensor1 wetness to turn 16 electric valve off 17 18const int dry2 = 544; // Minimum sensor2 dryness to turn 19 electric valve on 20const int wet2 = 270; // Maximum sensor2 wetness to turn electric 21 valve off 22 23const int dry3 = 544; // Minimum sensor3 dryness to turn electric 24 valve on 25const int wet3 = 270; // Maximum sensor3 wetness to turn electric valve 26 off 27 28const int dry4 = 544; // Minimum sensor4 dryness to turn electric valve 29 on 30const int wet4 = 270; // Maximum sensor4 wetness to turn electric valve off 31 32const 33 int LowMoisture = 20; // Minimum moisture level before misting 34const int HighMoisture 35 = 75; // Maximum moisture level before counting 1 36const int maxwatercount 37 = 2; // Maximum times to water plants a day 38 39const int pps= 2; // 40 Setting pressure pump switch to Pin 2 41const int ms = 3; // Setting 42 manual watering switch to Pin 3 43const int ev = 4; // Setting electrical 44 valve to Pin 4 45const int pp = 5; // Setting pressure pump to Pin 46 5 47 48int watercount = 0; // Counting the number of watering sessions 49int 50 watercond = 0; // Condition to water (TRUE or FALSE) 51 52void setup() 53 { 54 Serial.begin(9600); 55 Wire.begin(); 56 rtc.begin(); 57 rtc.adjust(DateTime(__DATE__, 58 __TIME__)); 59 60 pinMode(pps,INPUT); // PRESSURE PUMP SWITCH 61 pinMode(ms,INPUT); 62 // MANUAL SWITCH 63 pinMode(ev,OUTPUT); // ELECTRICAL VALVE 64 pinMode(pp,OUTPUT); 65 // PRESSURE PUMP 66 pinMode(10,OUTPUT); // SD WRITE 67 digitalWrite(ev,HIGH); 68 // TURN VALVE OFF INITIALLY 69 digitalWrite(pp,HIGH); // TURN PRESSURE PUMP 70 OFF INITIALLY 71 72 if (!SD.begin(10)) { 73 Serial.println("Error : 74 Push the reset button"); 75 for (;;); 76 } 77 file = SD.open(filename, 78 FILE_WRITE); 79 if (file.size() == 0) { 80 file.print("Watering Time, Moisture 81 1, Moisture 2, Moisture 3, Moisture 4, Water count"); 82 } 83} 84 85void 86 loop() { 87 88 int manualState = digitalRead(ms); // Read the INPUT 89 VOLTAGE OF PIN 3 90 int ppState = digitalRead(pps); // READ THE INPUT 91 VOLTAGE OF PIN 2 92 int ActppSt = digitalRead(pp); // READ ACTUAL 93 PUMP STATE 94 int sensorVal1 = analogRead(A0); // READ THE MOISTURE 95 SENSOR 1 96 int sensorVal2 = analogRead(A1); // READ THE MOISTURE 97 SENSOR 2 98 int sensorVal3 = analogRead(A2); // READ THE MOISTURE 99 SENSOR 3 100 int sensorVal4 = analogRead(A3); // READ THE MOISTURE 101 SENSOR 4 102 103 int Moisturelvl1 = map(sensorVal1, wet1, dry1, 100, 0); 104 int 105 Moisturelvl2 = map(sensorVal2, wet2, dry2, 100, 0); 106 int Moisturelvl3 = map(sensorVal3, 107 wet3, dry3, 100, 0); 108 int Moisturelvl4 = map(sensorVal4, wet4, dry4, 100, 0); 109 110 111 112 113 if ((Moisturelvl1<=LowMoisture)||(Moisturelvl2<=LowMoisture)||(Moisturelvl3<=LowMoisture)||(Moisturelvl4<=LowMoisture)) 114 { 115 watercond = 1; // Condition to water 116 } 117 else if 118 ((Moisturelvl1>=HighMoisture)||(Moisturelvl2>=HighMoisture)||(Moisturelvl3>=HighMoisture)||(Moisturelvl4>=HighMoisture)){ 119 120 watercond = 0; // Condition to stop water 121 } 122 123 DateTime 124 now = rtc.now(); 125 sprintf(Time, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), 126 now.minute(), now.second(), now.day(), now.month(), now.year()); 127 128 if ((now.hour()==0) 129 && (now.minute()==0)){ 130 watercount = 0; 131 } 132 133 Serial.print(Time); 134 135 Serial.print(" "); 136 Serial.print(Moisturelvl1); 137 Serial.print(","); 138 139 Serial.print(Moisturelvl2); 140 Serial.print(","); 141 Serial.print(Moisturelvl3); 142 143 Serial.print(","); 144 Serial.print(Moisturelvl4); 145 Serial.print(", Watercount:"); 146 147 Serial.print(watercount); 148 Serial.print(", Manual Switch State:"); 149 if 150 (manualState == HIGH){ 151 Serial.print ("OFF"); 152 } 153 else { 154 Serial.print 155 ("ON"); 156 } 157 Serial.print(", Pump Switch State:"); 158 if (ppState == 159 HIGH){ 160 Serial.print ("OFF"); 161 } 162 else { 163 Serial.print ("ON"); 164 165 } 166 Serial.print(", Pump State:"); 167 if (ActppSt == HIGH){ 168 Serial.println 169 ("OFF"); 170 } 171 else { 172 Serial.println ("ON"); 173 } 174 175 if (ppState 176 == LOW){ 177 if ( now.hour()>=7 && now.hour()<=23){ 178 digitalWrite(pp,LOW); 179 // TURN PRESSURE PUMP ON 180 delay(200); 181 } 182 } 183 184 185 else { 186 digitalWrite(pp,HIGH); // TURN PRESSURE PUMP OFF 187 delay(200); 188 189 } 190 191if (ppState == LOW ){ // ONLY START THE AUTO-WATERING 192 WHEN PPS IS ON 193 if (manualState == LOW || watercount<maxwatercount){ 194 if 195 (manualState == LOW || watercond == 1){ 196 197 if (file) { 198 file.print(Time); 199 200 file.print(","); 201 file.print(Moisturelvl1); 202 file.print(","); 203 204 file.print(Moisturelvl2); 205 file.print(","); 206 file.print(Moisturelvl3); 207 208 file.print(","); 209 file.print(Moisturelvl4); 210 file.print(","); 211 212 file.println(watercount); 213 file.flush(); 214 } 215 else 216 { 217 Serial.println("Error opening datalog.txt"); 218 } 219 220 221 digitalWrite(ev,LOW); // TURN VALVE ON 222 digitalWrite(pp,LOW); 223 // TURN PRESSURE PUMP ON 224 delay(300000); // 225 WATERING TIMER (5 MINS) 226 watercount = watercount+1; // ADD THE 227 WATERCOUNT FOR THIS DAY 228 229 } 230 else { 231 digitalWrite(ev,HIGH); 232 // TURN VALVE OFF 233 delay(200); 234 } 235 } 236} 237 238 239 else { 240 digitalWrite(ev,HIGH); // TURN VALVE OFF 241 delay(200); 242 243 } 244 245 delay(600); 246}
Downloadable files
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.
Automatic Watering
Comments
Only logged in users can leave comments
xerivar
0 Followers
•0 Projects
Table of contents
Intro
7
0