I'm new to hardware field and currently working with my project.
I'm using esp32 as dev board with temp sensor dht11 and a moisture sensor as input to the board and relay module with will signal to 5v dc motor for on or off. I'm facing issue that once the relay module receives signal it triggers and sounds tick; but when the data signal is cut off it not stop working instead it continuously on's the motor;
can anyone help me with this issue ;
here is the code that i'm using:
#include <DHT.h> // DHT sensor library
// Define pins
#define RELAY_PIN 26 // Relay connected to GPIO26
#define DHTPIN 4 // DHT11 sensor data pin
#define DHTTYPE DHT11 // Change to DHT22 if using that sensor
#define MOISTURE_SENSOR 34 // Soil moisture sensor analog pin
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
delay(2000); // Allow system to stabilize
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure pump is OFF initially
dht.begin(); // Start DHT sensor
Serial.println("Smart Irrigation System Initialized...");
}
void loop() {
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read soil moisture sensor value
int moistureValue = analogRead(MOISTURE_SENSOR);
int moisturePercent = map(moistureValue, 4095, 0, 0, 100); // Convert to percentage
// Display sensor readings88
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C | Humidity: ");
Serial.print(humidity);
Serial.print("% | Soil Moisture: ");
Serial.print(moisturePercent);
Serial.println("%");
// Watering Logic
if (moisturePercent < 30) { // If soil is dry
Serial.println("Soil dry! Turning ON the water pump for 5 minutes.");
digitalWrite(RELAY_PIN, HIGH); // Turn ON pump
delay(5000); // Keep the pump ON for 5 minutes (300,000 ms)
} else {
Serial.println("Soil moisture sufficient. Pump remains OFF.");
digitalWrite(RELAY_PIN, LOW); // Keep pump OFF
}
delay(5000); // Wait 5 seconds before checking again
}
The majority of relay modules are active LOW, ie they turn on when a LOW signal is applied to their input. Check that the relay is actually turning on when you set RELAY_PIN HIGH