Issue while controlling relay using ESP32

I’m trying to make an automated plant watering system using Esp32 and Blynk, So I’m using components such as relay, soil moisture sensor, water pump, DHT11 for showing temperature and humidity value of surroundings. Everything is working both in serial monitor as well as LCD display and I can able to see them in Blynk app too. But the problem is Relay is turned on whenever the code uploaded. what may be the issue and how can i able to resolve this issue?

#define BLYNK_TEMPLATE_ID "*******"
#define BLYNK_TEMPLATE_NAME "******"
#define BLYNK_AUTH_TOKEN "********"

#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//SDA --> 21
//SCL --> 22

char ssid[] = "*****";
char pass[] = "*****";
 
BlynkTimer timer;
 
DHT dht(4, DHT11);
#define sensor 33
#define relay 27

void setup() {
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  lcd.init();
  lcd.backlight();
  dht.begin();
 
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
 
  lcd.setCursor(1, 0);
  lcd.print("System Loading");
  for (int a = 0; a <= 15; a++) {
    lcd.setCursor(a, 1);
    lcd.print(".");
    delay(200);
  }
  lcd.clear();
 
  timer.setInterval(5000L, DHT11sensor);
  timer.setInterval(1500L, soilMoistureSensor);
}
 

 
//Get the soil moisture values
void soilMoistureSensor() {
  int value = analogRead(sensor);
  value = map(value, 0, 1024, 0, 100);
  value = (value - 399)* -1 ;
  if (value > 100){
    value = 100;
  }
  Blynk.virtualWrite(V0, value);

  Serial.print("Moisture value: ");
  Serial.println(value);
  lcd.setCursor(0, 0);
  lcd.print("Moisture:");
  lcd.print(value);
  lcd.print(" ");
  
}

//Get the button value
BLYNK_WRITE(V1) {
  bool Relay = param.asInt();
  if (Relay == 1) {
    digitalWrite(relay, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Motor is ON ");
  } else {
    digitalWrite(relay, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Motor is OFF");
  }
}
 
void DHT11sensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, h);
  Serial.print("Temperature: ");
  Serial.println(t);
  Serial.print("Humidity:");
  Serial.println(h);


  lcd.setCursor(0, 1);
  lcd.print("T:");
  lcd.print(t);
 
  lcd.setCursor(8, 1);
  lcd.print("H:");
  lcd.print(h);
}
 
void loop() {
  Blynk.run();  //Run the Blynk library
  timer.run();  //Run the Blynk timer

}

Is your relay active HIGH or active LOW ?

Pete.

Beneath the relay module it’s written as HW-307, so I’m anticipating that it’s an active HIGH relay type.

I’d start by testing the relay by providing a LOW (GND) then HIGH(VCC) signal to the control pin to see which energises the relay.

If it is indeed active HIGH then you need to change your code, as this is written tor an active LOW relay…

From void setup, to turn the relay off initially…

Pete.

Roughly can you please explain what’s the difference between Active HIGH and LOW relays?

An active LOW relay will energise the relay coil when the control signal is LOW.

Active HIGH is the opposite.

Pete.

Try this sample to see if the relay works well

void setup() {
  pinMode(27, OUTPUT);
  digitalWrite(27, LOW);
  }

void loop() {
  delay(2000);
  digitalWrite(27, HIGH);
  Serial.print("HIGH");
  delay(2000);
  digitalWrite(27, LOW);
  Serial.print("LOW");
}