Dropping USB connection when relay off

Hi folks I have made following code:

void loop() {
  Serial.println("Turn off per default relay");
  digitalWrite(RELAY_0, LOW);
  Plant_0();
}


//MAP
//Planta [0] Hylla höger hörn.
void Plant_0() {
  digitalWrite(RELAY_0, LOW);
  int SOIL_MOISTURE_0 = analogRead(sensor);  //Sets valA1 as whatever the sensor reads
  delay(5000);
  Serial.println("read sensor wait..");
  Serial.println(SOIL_MOISTURE_0);
  delay(10000);

if (SOIL_MOISTURE_0 > 500) {
    Serial.println("turn on pump");
    digitalWrite(RELAY_0, HIGH);  //sets the relay to cut the power to the track
    Serial.println("delay 3 sec");
    delay(3000);
  }else
    digitalWrite(RELAY_0, LOW);
}

Everytime the relay is turned off The USB connection is dropped why?

Help me out

Thank you

Is there a flyback diode on the relay coil?
Maybe an inductive high voltage spike when the field of the relay coil collapses is causing problems?

The USB connection is dropping because the Arduino is resetting.
You should not power a relay from the Arduino.

The LED in your circuit lacks a current-limiting resistor.

oh I see so I really need to have an external power source for the relay?

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