Hello everyone,
I'm trying to make an Arduino UNO watering device that will water my plants twice a day on a given schedule. Here is how it will work first, the uno will read the soil moisture level using the digital reading of the hl 69 soil moisture sensor and will display it on the 16x2 i2c LCD. Then it will check the time in the ds3231 RTC module, if the time matches the given schedule and the soil moisture is dry it will turn on the pump until the soil is wet. My problem is when the first function is completed, if I lift the soil moisture sensor the Uno turns on the water pump even if the time doesn't match the given schedule.
I also want to be able to turn on/off the water pump using Bluetooth if I send a character, for example "W" for turn on and "S" or if the soil is wet for off. The problem is the water pump does not turn off even when the soil is wet. Your help will be greatly appreciated.
here is my code
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
// Define the I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the DS3231 RTC
RTC_DS3231 rtc;
const int hc05_rxPin = 10; // SoftwareSerial rx pin
const int hc05_txPin = 11; // SoftwareSerial tx pin
const int waterPumpPin = 8; // Relay pin
const int soilMoistureSensor = 6; // Soil Moisture pin
// Define the watering times
const int wateringHour1 = 8; // 8 AM
const int wateringHour2 = 20; // 8 PM
// Initialize software serial for HC-05 Bluetooth module
SoftwareSerial bluetooth(hc05_rxPin, hc05_txPin);
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize the RTC
rtc.begin();
pinMode(waterPumpPin, OUTPUT); // Set the water pump pin as an output
digitalWrite(waterPumpPin, HIGH); // Set the water pump as turned off initially
Serial.begin(9600); // Initialize serial communication with the computer (USB)
bluetooth.begin(9600); // Initialize serial communication with the HC-05 Bluetooth module
Serial.println("Bluetooth Serial started at 9600 baud");
}
void loop() {
// Read the current time
DateTime now = rtc.now();
// Read the soil moisture level
bool soilIsDry = digitalRead(soilMoistureSensor);
// Display the soil moisture level on the LCD
lcd.setCursor(0, 0);
lcd.print("Soil Moisture: ");
lcd.setCursor(0, 1);
lcd.print(soilIsDry ? "Low" : "High");
// Bluetooth function
if (bluetooth.available()) {
char receivedChar = bluetooth.read();
// Turn on the water pump
if (receivedChar == 'W') {
digitalWrite(waterPumpPin, LOW);
Serial.println("Water pump turned ON");
}
// Turn off the water pump
else if (receivedChar == 'S' || !soilIsDry) {
digitalWrite(waterPumpPin, HIGH);
Serial.println("Water pump turned OFF");
}
}
// Scheduled watering function
else if ((now.hour() == wateringHour1 || now.hour() == wateringHour2) && soilIsDry) {
// Turn on the water pump
digitalWrite(waterPumpPin, LOW);
// Wait until the soil is wet enough
while (digitalRead(soilMoistureSensor)) {
delay(1000);
}
// Turn off the water pump
digitalWrite(waterPumpPin, HIGH);
}
// Wait for a second before the next loop
delay(1000);
}