Blynk connection when pump is connected

I am using arduino mega, 2 soil moisture sensor, 1 DHT11, relay, wifi shield, pump and blynk…
this is my code… i wonder why the blynk connection will not last long is we connected pump… if we only using soil moisture sensor and dht11 (unplug pump wire)… the connection never disconnected.

#define BLYNK_PRINT Serial      //declaration to use Blynk

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h> 
#define DHTPIN 5                // Connect the signal pin of DHT11 sensor to digital pin 5
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);
int sensor_pin = A0;  //soil moisture sensor 1
int sensor1 = A1;     //soil moisture sensor 2
int output_value ;  //output value from soil moisture sensor 1
int sensor_value ;  //output value from soil moisture sensor 2
int Pump=13;

char auth[] = " f79df2aeed1945bab751cf4df80f246b"; // Author Token from email

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "TKP Class Room";
char pass[] = "TkpH0lding";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;
void sendSensor()
{
  output_value= analogRead(sensor_pin);
  sensor_value= analogRead(sensor1);
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, h);
  Blynk.virtualWrite(V8, t);
  Blynk.virtualWrite(V9, output_value);
  Blynk.virtualWrite(V10, sensor_value);
  
}
//Blynk.virtualWrite(V7,  output_value );

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}
void pump(){
  output_value= analogRead(sensor_pin);
  sensor_value= analogRead(sensor1);

  if(output_value>200||sensor_value>200){
      digitalWrite(Pump,HIGH);
      delay(100);
      }
    if(output_value<140||sensor_value<140){
         digitalWrite(Pump,LOW);  
         delay(100);
    }
  
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  dht.begin();  
  pinMode(Pump,OUTPUT);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
    output_value= analogRead(sensor_pin);
  
  pump();
  Blynk.run();
  sendSensor();
  timer.run(); // Initiates BlynkTimer
}

Sounds like power issue causing the ESP to brown out.

Brown out? So what should i do?

Well, I said “sounds like”… but without knowing exactly how you have everything wired, power ratings of your PSU and pump, and so on… it is just a guess.

Basically use seperate 3.3v PSU for the ESP (do not run it off the Arduino 3.3v pin… it is not good enough), and of course whatever you are running for pumps should have its own separate power as well.

Ohh… i see… i’ll try troubleshoot again… thank you for the clue… :grinning: