Hi everyone,
Is there any hidden bug or hardware conflict in this code for my little degree project? I also want to use this project myself to automate the watering of my Pothos plant at home.
As a side note, I took a 2-year gap year during my final semesters, so I am a bit rusty with my coding skills. Please forgive me if I am a bit slow to understand, but I am eager to learn and fix this project, thank you!
Hardware details:
- MCU: Arduino WEMOS D1 R32
- Temperature/Humidity: DHT11
- Water Level Sensor (Digital input)
- Soil Moisture Sensor (Analog input on Pin 34)
- Pump and LED
- LCD I2C
Here is my current code:
#define SOIL_PIN 34
#define DHTPIN 4
#define LED_PIN 27
#define PUMP_PIN 18
#define WATER_PIN 14
#define BUZZER_PIN 23
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(PUMP_PIN, OUTPUT);
pinMode(WATER_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("SYSTEM START");
delay(2000);
lcd.clear();
}
void loop() {
int soilValue = analogRead(SOIL_PIN);
int waterValue = digitalRead(WATER_PIN);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Serial.println("=====");
Serial.print("Soil: "); Serial.println(soilValue);
Serial.print("Hum: "); Serial.println(humidity);
lcd.setCursor(0,0);
lcd.print("Soil: ");
lcd.print(soilValue);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Hum: ");
lcd.print(humidity, 1);
lcd.print("% ");
if (waterValue == LOW) {
Serial.println("WATER IS LOW!");
digitalWrite(PUMP_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, HIGH);
}
else {
digitalWrite(BUZZER_PIN, LOW); // Turn off alarm if water is fine
if (soilValue > 2500) {
digitalWrite(LED_PIN, HIGH);
digitalWrite(PUMP_PIN, HIGH); // Turn pump on
if (humidity < 50) {
delay(1500);
} else {
delay(700);
}
digitalWrite(PUMP_PIN, LOW);
}
else {
digitalWrite(LED_PIN, LOW);
digitalWrite(PUMP_PIN, LOW);
}
}
delay(2000);
}
.
My specific questions:
- Is using delay() inside the main loop safe enough, or will it cause the water level sensor to respond too slowly if the tank runs dry?
- Are there any ESP32 pin conflicts? (I am using Pin 34 for analog read, and Pins 4, 27, 18, 23, 35 for others).
- Pothos plants prefer drying out between waterings. Is this logic smart enough to prevent overwatering?
Any advice on the code or hardware wiring would be highly appreciated. Thank you!