12V DC pump connected to relay on nodemcu esp8266 running non stop

Hi there, it is 4 am here and I've spent my whole night on this so please help. I've got a 12V DC pump connected to esp8266 via a relay . I'm building an 'automated irrigation system' and the pump needs to be controlled based on soil moisture levels , I've automated the condition for pump on Blynk but the Problem is that the pump is running NON STOP from when i upload the code so blynk can't control it
Pins:

Relay VCC to esp Vin
Relay GND to esp G
Relay IN to esp D3
Relay COM powered by 12V adapter
Relay ON to pump
12v adapter and pump grounded directly

I want the pump to only run when i give command ( press button ) from blynk , before that the pump shouldn't work, this way the automation condition applied on blink can do it's job

#include <Wire.h>
#include <SPI.h>
#include <ACROBOTIC_SSD1306.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_TEMPLATE_ID "TMPLn3apCeAb"
#define BLYNK_DEVICE_NAME "Quickstart Template"



char auth[] = "***";//Enter your Auth token
char ssid[] = "***";//Enter your WIFI name
char pass[] = "***";//Enter your WIFI password

BlynkTimer timer;
bool Relay = 0;


//Define component pins
#define SCL D1
#define SDA D2
#define sensor A0
#define waterPump D3
#define DRY_SOIL     40
#define TIME_PUMP_ON  5
int value = 0;


void setup() {
  Serial.begin(9600);
  pinMode(waterPump, OUTPUT);
  digitalWrite(waterPump, HIGH);
  Wire.begin();
  oled.init();                      // Initialze SSD1306 OLED display
  oled.clearDisplay();

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

  oled.setTextXY(1, 0);
  oled.putString("System Loading");
  for (int a = 0; a <= 15; a++) {
    oled.setTextXY(a, 1);
    oled.putString(".");
    delay(500);
  }
  oled.clearDisplay();

  //Call the function
  timer.setInterval(100L, soilMoistureSensor);
  // timer.setInterval(60L * 1000, autoControlPlantation);
}

//Get the button value
BLYNK_WRITE(1) {
  Relay = param.asInt();

  if (Relay == 1) {

    digitalWrite(waterPump, LOW);
    oled.setTextXY(0, 1);
    oled.putString("Motor is ON ");
  }

  else {
    digitalWrite(waterPump, HIGH);
    oled.setTextXY(0, 1);
    oled.putString("Motor is OFF");
  }
}



//Get the soil moisture values
void soilMoistureSensor() {
  value = 0;
  int value = analogRead(sensor);
  value = map(value, 0, 1024, 0, 100);
  value = (value - 100) * -1;
  Blynk.virtualWrite(0, value);
  oled.setTextXY(0, 0);
  oled.putString("Moisture :");
  oled.putString(String(value));
  oled.putString(" ");

}

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

// void autoControlPlantation(void)
// {
//   if (value < DRY_SOIL)
//   {
//     Blynk.notify("AutoFarm: Warning ==>> Pump ON");
//     digitalWrite(waterPump, LOW);
//     oled.setTextXY(0, 1);
//     oled.putString("Motor is ON ");
//   }

//   else {
//     digitalWrite(waterPump, HIGH);
//     oled.setTextXY(0, 1);
//     oled.putString("Motor is OFF");
//   }
//   delay (TIME_PUMP_ON * 1000);
// }






Do you actually have a relay MODULE, not just a relay? Need to be specific as to what you have. And do you mean the NO connection of the relay or the NC connection of the relay?

IF you disconnect the wire from the Arduino D3 does the relay open and the pump stop?

Edit:
Right after sending, I see this in your program:

 pinMode(waterPump, OUTPUT);
  digitalWrite(waterPump, HIGH);

So, if you don't do this digitalWrite, does the pump stay off?

Hi,
Can you please post some images of your project?

Can you please post specs/data on the relay module?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

How are you powering your project?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Yes its is a relay module. Sorry, it is NO not ON . Yes, disconnecting from D3 as well removing digitalWrite doesn't power the pump . But keeping it at HIGH or LOW doesn't seem to have an effect

Hi,
Have you got the relay module input gnd connected to the 8266 gnd.
The 8266 is a 3V3 device, if you disconnect the wire from the 8266 to the input pin of the module.

  1. What does the relay do with the wire not connected to anything?
  2. Connect the relay input to 3V3, what does the relay do?
  3. Connect the relay input to gnd, what does the relay do?

Can you please post some images of your project?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi tom , If i disconnect the wire the pump turns off. I'm powering the board with a usb cable ( the board powers everything else but the pump )



On using 3v3 instead of vin , it made a strange clicking sound when i plugged in the usb cable and the green light for vcc ( which was turned on before but not as bright as red ) is now off

SRD-05VDC-SL-C-Datasheet.pdf (91.7 KB)
heres the datasheet to the relay module

What about the rest of the test?

Leave the supply to the relay connected to 5V.
Do you have a DMM?

Tom... :smiley: :+1: :coffee: :australia:

So, connecting relay to the 3v3 worked. The greenlight turning on/off was indicator of the pump being on/off . The pump can now be controlled with Blynk . Thanks for the help

If this is the 3V3 on the 8266 and you are supplying the 8266 with 5V, you may be overloading the 8266 internal 3V3 regulator.

The relay is 5V coil, so don't be surprised if it becomes erratic in operation.

Tom. :smiley: :+1: :coffee: :australia:

Oof , so i switched to the 3.3v and that produces the results too. Would that be okay ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.