1 /* Arduino Projects for Dummies
4 * Chapter 10: Building and Automated Garden
5 * A system that detects soil moisture and
6 * triggers a solenoid valve
11 int sensorPin = A0; // The input pin for the moisture sensor
12 int valvePin = 9; // The output pin to actuate the valve
14 const int dryThreshold = 950; // Set this threshold based upon your sensor tests
15 // 1023 = bone dry, < 100 totally submerged
16 const long sampleInterval = 600; // Sample every 10 minutes
17 const int irrigationTime = 5000; // Duration to let the water flow
18 boolean DEBUG=true; // Set to true if you want to read output on the Serial port
21 if(DEBUG){Serial.begin(9600);}
23 pinMode(sensorPin, INPUT); // Declare the sensor pin as an OUTPUT:
24 pinMode(valvePin, OUTPUT); // Declare the valve pin as an OUTPUT:
25 digitalWrite(valvePin, LOW); // Make sure the valve is closed.
30 int sensorValue = analogRead(sensorPin); // read the value from the sensor:
33 Serial.print("Sensor value: ");
34 Serial.println(sensorValue);
37 if (sensorValue>dryThreshold){
38 digitalWrite(valvePin, HIGH); // Open the water valve
39 delay(irrigationTime); // Keep it open for the irrigation time
40 digitalWrite(valvePin, LOW); // Close the valve
42 delay(sampleInterval); // wait until the next time to take a reading