Last active
November 7, 2022 19:20
-
-
Save RuckertSolutions/f55f9684636dceafa05d3d2d9889f3fe to your computer and use it in GitHub Desktop.
Arduino Auto Plant Watering
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
#include <LiquidCrystal_I2C.h> | |
/* | |
Automatic chili & tomato plant watering | |
Analog humidity sensors connected to A1 and A2 | |
Mini 3V pumps connected to relay | |
Relay inputs connected to Digital pins 1 and 2 | |
Relay ground connected to Arduino if not externaly powered (5V), if yes connect ground of external power supply (+ on JD-VCC, - on GRD) | |
Humidity sensory values: | |
+-580 and more completely dry, | |
+- 500 begins to need watering | |
+- 300 and less completely under water | |
2021 CHAUSSY Jhang | |
*/ | |
//initialize the liquid crystal library | |
//the first parameter is the I2C address | |
//the second parameter is how many rows are on your screen | |
//the third parameter is how many columns are on your screen | |
LiquidCrystal_I2C lcd(0x27, 20, 4); | |
// Init pins | |
int plantSensor1 = A0; | |
int plantSensor2 = A1; | |
// int lightSensor = A3; | |
// int tempSensor = A4; | |
int pump1 = 8; | |
int pump2 = 9; | |
// int buttonToggle = 5; | |
// int display1 = 6; | |
// int display2 = 7; | |
// Init vars | |
int plantSensor1Val = 0; | |
int plantSensor1ValPercent = 0; | |
int plantSensor2Val = 0; | |
int plantSensor2ValPercent = 0; | |
int pump1Counter = 0; | |
int pump1CounterRun = 0; | |
int pump2Counter = 0; | |
int pump2CounterRun = 0; | |
void setup() | |
{ | |
Serial.begin(9600); // DEBUG | |
// Set digital pins mode | |
pinMode(pump1, OUTPUT); | |
pinMode(pump2, OUTPUT); | |
// pinMode(buttonToggle, INPUT_PULLUP); | |
// Init pump relays HIGH (off) | |
digitalWrite(pump1, HIGH); // Pump relays switch on on LOW | |
digitalWrite(pump2, HIGH); | |
//initialize lcd screen | |
lcd.init(); | |
// turn on the backlight | |
lcd.backlight(); | |
Serial.println("Setup done."); // DEBUG | |
} | |
void loop() | |
{ | |
// Read humidity sensors | |
plantSensor1Val = analogRead(plantSensor1); | |
plantSensor2Val = analogRead(plantSensor2); | |
// Map to percentage | |
plantSensor1ValPercent = map(plantSensor1Val, 300, 600, 0, 100); | |
plantSensor2ValPercent = map(plantSensor2Val, 300, 600, 0, 100); | |
String x = String(plantSensor1ValPercent); | |
String y = String(plantSensor2ValPercent); | |
lcd.setCursor(0, 0); | |
String t1 = "Plant 1 Raw: "; | |
String t2 = t1 + plantSensor1Val; | |
lcd.print(t2); | |
lcd.setCursor(0, 1); | |
String t3 = "Plant 1 %: "; | |
String t4 = t3 + plantSensor1ValPercent + " " + pump1CounterRun; | |
lcd.print(t4); | |
lcd.setCursor(0, 2); | |
String t5 = "Plant 2 Raw: "; | |
String t6 = t5 + plantSensor2Val; | |
lcd.print(t6); | |
lcd.setCursor(0, 3); | |
String t7 = "Plant 2 %: "; | |
String t8 = t7 + plantSensor2ValPercent + " " + pump2CounterRun; | |
lcd.print(t8); | |
Serial.print("plantSensor1: "); // DEBUG | |
Serial.print(plantSensor1Val); // DEBUG | |
Serial.print(" plantSensor2: "); // DEBUG | |
Serial.print(plantSensor2Val); // DEBUG | |
Serial.println(" "); | |
Serial.print("plantSensor1%: "); // DEBUG | |
Serial.print(plantSensor1ValPercent); // DEBUG | |
Serial.print(" plantSensor2%: "); // DEBUG | |
Serial.print(plantSensor2ValPercent); // DEBUG | |
Serial.println(" "); | |
// Decrement counters | |
if (pump1Counter != 0) | |
{ | |
pump1Counter--; | |
} | |
if (pump2Counter != 0) | |
{ | |
pump2Counter--; | |
} | |
if (plantSensor1Val > 480 && pump1Counter == 0) | |
{ | |
Serial.println("Watering plant1"); // DEBUG | |
digitalWrite(pump1, LOW); | |
delay(5000); | |
digitalWrite(pump1, HIGH); | |
// Set countdown to 10 to wait 10 iterations before watering again | |
pump1Counter = 900; | |
pump1CounterRun++; | |
} | |
if (plantSensor2Val > 450 && pump2Counter == 0) | |
{ | |
Serial.println("Watering plant2"); // DEBUG | |
digitalWrite(pump2, LOW); | |
delay(5000); | |
digitalWrite(pump2, HIGH); | |
// Set countdown to 10 to wait 10 iterations before watering again | |
pump2Counter = 900; | |
pump2CounterRun++; | |
} | |
// Check once every minute | |
delay(1000 * 60); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment