Last active
June 15, 2018 20:48
-
-
Save CircuitSetup/823fabb7a76caeec37c8d10a9889613d to your computer and use it in GitHub Desktop.
Arduino Smart Watering with 4 water sensors & solenoids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//****************************************************************************************** | |
// File: smart_watering_v2.ino | |
// Author: John deGlavina | |
// | |
// The following sketch is used to water a garden depending on the readings of 4 moisture sensors. | |
// These moisture sensors correspond to 4 solenoid valves that will open when the water_threshold | |
// falls below the percentage that is set. When the threshold is reached again, the solenoid will | |
// close. The level of the moisture sensors will be checked every check_interval concurrently. | |
// | |
// The original setup uses a DF Robot 4 relay shield, moisture sensors v1.2 (found on ebay), | |
// and 12v normally closed solenoid water valves. | |
// | |
// Be sure to set the const values to what you want below. | |
//****************************************************************************************** | |
//how long between checking water sensor values in ms | |
const long check_interval = 10000; | |
//soil moisture percentage from 0-100 - 100 being soaking in water, 0 being completely dry | |
const int water_threshold = 60; | |
//the lower limit that can almost never be reached unless someone goes wrong | |
//this is a safeguard to prevent a solenoid from staying open forever | |
//If the value is less than 0 then it is higher or lower than the value set in | |
//moistX_high or low | |
const int water_lower_limit = 0; | |
//values for moisture sensor completely dry and soaking in water. Every sensor is different, so be sure | |
//to take readings for both cases. | |
const int moist1_high = 643; | |
const int moist1_low = 357; | |
const int moist2_high = 623; | |
const int moist2_low = 351; | |
const int moist3_high = 645; | |
const int moist3_low = 363; | |
const int moist4_high = 633; | |
const int moist4_low = 356; | |
//set all moisture sensors analog pins | |
const int moisture1 = A1; | |
const int moisture2 = A2; | |
const int moisture3 = A3; | |
const int moisture4 = A4; | |
// set digital pins for water relays | |
// the DF robot relay shield has these pins set by default | |
const int relay1 = 2; | |
const int relay2 = 7; | |
const int relay3 = 8; | |
const int relay4 = 10; | |
unsigned long currentMillis = 0; | |
//will store last time relays were updated | |
unsigned long previous1Millis = 0; | |
unsigned long previous2Millis = 0; | |
unsigned long previous3Millis = 0; | |
unsigned long previous4Millis = 0; | |
//used to set the state of the relay | |
byte relay1State = LOW; | |
byte relay2State = LOW; | |
byte relay3State = LOW; | |
byte relay4State = LOW; | |
//declare moisture values | |
int moisture1_value = 0; | |
int moisture2_value = 0; | |
int moisture3_value = 0; | |
int moisture4_value = 0; | |
// for printing output | |
int print1 = 0; | |
int print2 = 0; | |
int print3 = 0; | |
int print4 = 0; | |
void setup() { | |
// declare relay as output | |
pinMode(relay1, OUTPUT); | |
pinMode(relay2, OUTPUT); | |
pinMode(relay3, OUTPUT); | |
pinMode(relay4, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
// check to see if it's time to open the relays | |
// if the difference between the current time and last time relays were | |
// opened is bigger than the interval at which we want to open the relays | |
currentMillis = millis(); | |
readMoistureValues(); | |
updateRelay1(); | |
updateRelay2(); | |
updateRelay3(); | |
updateRelay4(); | |
switchRelays(); | |
delay(500); //let everything catch up - prevents crosstalk | |
} | |
void readMoistureValues() { | |
//for some reason this needs to be done first to prevent crosstalk | |
analogRead(moisture1); | |
analogRead(moisture2); | |
analogRead(moisture3); | |
analogRead(moisture4); | |
//read the value from the moisture sensors: | |
moisture1_value = analogRead(moisture1); | |
moisture2_value = analogRead(moisture2); | |
moisture3_value = analogRead(moisture3); | |
moisture4_value = analogRead(moisture4); | |
//store in another set of variables for printing output | |
print1 = analogRead(moisture1); | |
print2 = analogRead(moisture2); | |
print3 = analogRead(moisture3); | |
print4 = analogRead(moisture4); | |
} | |
void updateRelay1() { | |
if(currentMillis - previous1Millis >= check_interval) { | |
previous1Millis = currentMillis; | |
print1 = map(print1,moist1_high,moist1_low,0,100); | |
Serial.print("Moisture 1: "); | |
Serial.print(print1); | |
Serial.print("% "); | |
Serial.println(moisture1_value); | |
if(print1<=water_threshold && print1 > water_lower_limit){ | |
relay1State = HIGH; | |
} else { | |
relay1State = LOW; | |
} | |
} | |
} | |
void updateRelay2() { | |
if(currentMillis - previous2Millis >= check_interval) { | |
previous2Millis = currentMillis; | |
print2 = map(print2,moist2_high,moist2_low,0,100); | |
Serial.print("Moisture 2: "); | |
Serial.print(print2); | |
Serial.print("% "); | |
Serial.println(moisture2_value); | |
if(print2<=water_threshold && print2 > water_lower_limit){ | |
relay2State = HIGH; | |
} else { | |
relay2State = LOW; | |
} | |
} | |
} | |
void updateRelay3() { | |
if(currentMillis - previous3Millis >= check_interval) { | |
previous3Millis = currentMillis; | |
print3 = map(print3,moist3_high,moist3_low,0,100); | |
Serial.print("Moisture 3: "); | |
Serial.print(print3); | |
Serial.print("% "); | |
Serial.println(moisture3_value); | |
if(print3<=water_threshold && print3 > water_lower_limit){ | |
relay3State = HIGH; | |
} else { | |
relay3State = LOW; | |
} | |
} | |
} | |
void updateRelay4() { | |
if(currentMillis - previous4Millis >= check_interval) { | |
previous4Millis = currentMillis; | |
print4 = map(print4,moist4_high,moist4_low,0,100); | |
Serial.print("Moisture 4: "); | |
Serial.print(print4); | |
Serial.print("% "); | |
Serial.println(moisture4_value); | |
Serial.println(); | |
if(print4<=water_threshold && print4 > water_lower_limit){ | |
relay4State = HIGH; | |
} else { | |
relay4State = LOW; | |
} | |
} | |
} | |
void switchRelays() { | |
digitalWrite(relay1, relay1State); | |
digitalWrite(relay2, relay2State); | |
digitalWrite(relay3, relay3State); | |
digitalWrite(relay4, relay4State); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment