I cant turn off my water pump via relay

My water pump insta goes ON when I upload my code, my relay (5v) cant turn the water pump off, but I found that if I hit the relay with a hard stuff in top I can stop my pump and start it. I even tried another relay but still same results. I am using arduino uno just for 5v pin beacuse my vin pin from esp8226 is not giving me 5v even tho I am using via cable. Here is my code and my connections.
IMG_0021

#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_TEMPLATE_NAME "Smart Plant"

/* Connections
Relay. D3
Btn.   D7
Soil.  A0
PIR.   D5
SDA.   D2
SCL.   D1
Temp.  D4
*/

// Include the library files
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);

char auth[] = "xxx";  // Enter your Blynk Auth token
char ssid[] = "Zyrja";  // Enter your WIFI SSID
char pass[] = "22222222";  // Enter your WIFI Password

DHT dht(D4, DHT11); // (DHT sensor pin, sensor type) D4 DHT11 Temperature Sensor
BlynkTimer timer;

// Define component pins
#define soil A0     // A0 Soil Moisture Sensor
#define PIR D5      // D5 PIR Motion Sensor
int PIR_ToggleValue;

void checkPhysicalButton();
int relay1State = HIGH;
int pushButton1State = HIGH;
#define RELAY_PIN_1       D3   // D3 Relay
#define PUSH_BUTTON_1     D7   // D7 Button
#define VPIN_BUTTON_1    V12 

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2, 0x27);
  lcd.backlight();
  pinMode(PIR, INPUT);

  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, HIGH);  // Start with relay off
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);

  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  dht.begin();

  lcd.setCursor(0, 0);
  lcd.print("  Initializing  ");
  for (int a = 5; a <= 10; a++) {
    lcd.setCursor(a, 1);
    lcd.print(".");
    delay(500);
  }
  lcd.clear();
  lcd.setCursor(11, 1);
  lcd.print("W:OFF");

  // Call the function
  timer.setInterval(100L, soilMoistureSensor);
  timer.setInterval(100L, DHT11sensor);
  timer.setInterval(500L, checkPhysicalButton);
}

// Get the DHT11 sensor values
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(V0, t);
  Blynk.virtualWrite(V1, h);

  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(t);

  lcd.setCursor(8, 0);
  lcd.print("H:");
  lcd.print(h);
}

// Get the soil moisture values
void soilMoistureSensor() {
  int value = analogRead(soil);
  value = map(value, 0, 1024, 0, 100);
  value = (value - 100) * -1;

  Blynk.virtualWrite(V3, value);
  lcd.setCursor(0, 1);
  lcd.print("S:");
  lcd.print(value);
  lcd.print(" ");
}

// Get the PIR sensor values
void PIRsensor() {
  bool value = digitalRead(PIR);
  if (value) {
    Blynk.logEvent("pirmotion", "WARNING! Motion Detected!"); // Enter your Event Name
    WidgetLED LED(V5);
    LED.on();
  } else {
    WidgetLED LED(V5);
    LED.off();
  }
}

BLYNK_WRITE(V6) {
  PIR_ToggleValue = param.asInt();  
}

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(VPIN_BUTTON_1);
}

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State ? LOW : HIGH);  // LOW to turn on, HIGH to turn off
  Serial.print("Virtual button pressed. Relay state: ");
  Serial.println(relay1State);
}

void checkPhysicalButton() {
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {
      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State ? LOW : HIGH);  // LOW to turn on, HIGH to turn off

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
      Serial.print("Button toggled. Relay state: ");
      Serial.println(relay1State);
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH;
  }
}

void loop() {
  if (PIR_ToggleValue == 1) {
    lcd.setCursor(5, 1);
    lcd.print("M:ON ");
    PIRsensor();
  } else {
    lcd.setCursor(5, 1);
    lcd.print("M:OFF");
    WidgetLED LED(V5);
    LED.off();
  }

  if (relay1State == HIGH) {
    lcd.setCursor(11, 1);
    lcd.print("W:OFF");
  } else if (relay1State == LOW) {
    lcd.setCursor(11, 1);
    lcd.print("W:ON ");
  }

  Blynk.run(); // Run the Blynk library
  timer.run(); // Run the Blynk timer
}

What is this?

Post a bigger image.

Relays with opto-isolators use HIGH to turn off the relay. Verify your relay works this way.

insta = instantly

I used the LOW too and via serial monitor I can see that the relay is toggling when I press the button

IMG_0030

Your photos are too small. I can't see anything. Draw a wiring diagram.

I am guessing the motor is on the Normally Closed contact.


p.s I am getting the 5v into vin pin from another board as in photo above

I can't see what is going on in the photo.
Are the ESP8266 pins 5v or 3v3? What volt rating are all your devices?

That's three requests for a larger photo. I am tapping out.

as I googled the VIN pin should give me 5v if the cable is connected but im not getting it so i got from another board. and sorry here are 2 photos 1 from close and 1 all connections
IMG_0031
IMG_0032

Appears you are putting 7.4V on the relay's 5V pin and to the LCD.

how?

Referring to post # 5, there is a wire from the battery + (7.4V ?) to a pin on the MCU and also to the relay (5V pin?) and to the LCD.

Did you try yelling at it before that?

1 Like

You mean you see it print that it should be toggling.

Can you write a simple sketch that just turns the relay on and off, and verify that the relay works?

If that's a stretch, just use the blink sketch from the IDE and see if you can blink the relay.

Use your ohmmeter to confirm the relay contacts are doing the right thing.

a7

My first troubleshooting step would be to load the example "blink" sketch. Can be found in the IDE File/Examples/Basics/blink or see below.

Change the LED pin to the output pin going to your pump. The relay should click on and off every second. If it does not then this issue is not in your code.


Not in your post however:
To operate you pump relay from the ESP you should use a simple transistor.

Q2 can be any low power signal transistor:
2n2222, BC337, 2n4401

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);   // <-------change to pump
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

Test if your relay works first by follow the this tutorial: https://newbiely.com/tutorials/esp8266/esp8266-relay

I tested my relay with the arduino uno and it works as expected but when i put it in my ESP8226 it doesnt work when i put the IN wire the green led its glowing in low (in arduino it didnt)

Not all relay modules are happy with 3.3V (neither hitting them with hard objects).
Put a sufficient resolution photo of your relay and datasheet if you have.

Also in your photo I see 4 lithium batteries instead of two...

Those are not lithium ion batteries, but 1.5V alkaline batteries, so power the pump at 6V

Hi!
I'll try to help you, I can't really understand your program since I work with different and perhaps simpler methods, anyway.
Not knowing if your program actually turns off the pump, you could check it using the serial monitor, this way from the computer you can see the status of the relay variable.
If the problem is not software, it could be the relay module that needs a voltage of 5V to switch states. you can do this via a transistor that is activated on the output pin of the micro controller, which in turn will control the relay.
There could also be other problems such as the undersized transistor and therefore when it trips the contacts melt and to open them you have to detach them by giving them blows for example.
I really don't think this is your case, also because a relay like that can handle higher loads than the pump you use.
I hope I have been of help to you!
Have a nice day!

When I use the relay in an arduino uno the moment when i put the IN wire the greed led in relay doesnt go ON and it works as expected, when I put the IN wire into my ESP8226 (i still use the vvc of the arduino uno 5v) the green led in relay starts glowin like its giving voltage or something and my relay doesnt work. But in arduino uno it works perfectly with 5v too.

I have tested the relay with serial monitor it works, plus I tested with 3v and i could see the green led changing but not having power to trigger the relay