Arduino setup runs when relay activates

I can't understand why the code in the setup is run more than once. Moreover its a circuit where a moisture sensor should turn on a pump after a given value. There is also a relative humidity sensor. I want to print out in the terminal their values with the title "Datum och tid, Fuktighet %, Tempratur *C, Jordfuktighet %". Here's the code:
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT11
int relayPin = 8;
int sensor_pin = A0; // Soil Sensor input at Analog PIN A0
float fuktighetsprocent;
int sensor_analog;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Datum och tid, Fuktighet %, Tempratur *C, Jordfuktighet %");
digitalWrite(relayPin, HIGH);
pinMode(sensor_pin, INPUT);
pinMode(relayPin, OUTPUT);
dht.begin();
}

void loop() {
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}

sensor_analog = analogRead(sensor_pin);
fuktighetsprocent = ( 100 - ( (sensor_analog/1023.00) * 100 ) );
float hj= fuktighetsprocent;

Serial.print(",");
Serial.print(h);
Serial.print(",");
Serial.print(t);
Serial.print(",");
Serial.println(hj);

if (hj < 50)
{
//pumpOn();
digitalWrite(relayPin, LOW);
delay(2000);

}
else {
//pumpOff();
digitalWrite(relayPin, HIGH);
}
delay(1000);

}

Because the processor is being reset?

Please remember to use code tags when posting code

The initialization of the relay pin should be placed before setting its value:

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

Hi, @zackcess
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

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

We need to know how the relay and pump are wired - this will almost certainly be due to the pump turn-on transient interfering with the Arduino - this can happen in several ways so the circuit (and a photo of it) would be great to see what might be happening.

Hi,
Have you written your code in stages.

Have you code that JUST reads the DHT, AND works?
Have you code that JUST reads the moisture sensor, AND works?
Have you code that JUST activates the relays, AND works?

Until you can establish this, your code could have many problems.
BUT a schematic in this case is essential.

Links to sites where data/specs are of your hardware would help too, especially the relay.

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

No; you can use any sequence. In this case, it prevents the pin from going LOW when switching the pin to output.

1 Like

The code works exactly as I want it to do. I get data from the dht and moisture sensors. The if statement turns off the relay and activates the pump. The only thing is when that data prints in the terminal I get the setup text multiple times which I don't want.

Hi,
Please draw a circuit diagram, not a Fritzy image.
The resolution does not allow us to read pin names and numbers.
A hand drawn circuit will be fine.
What is your power supply.
Can you please post links to the pump please.

Keep your USB cable away from the pump wires.
Does the problem occur if you disconnect the pump?

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

Hi,
If you read the instructions linked, you code would look like this;

#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT11
int relayPin = 8;
int sensor_pin = A0; // Soil Sensor input at Analog PIN A0
float fuktighetsprocent;
int sensor_analog;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
  Serial.begin(9600);
  Serial.println("Datum och tid, Fuktighet %, Tempratur *C, Jordfuktighet %");
  digitalWrite(relayPin, HIGH);
  pinMode(sensor_pin, INPUT);
  pinMode(relayPin, OUTPUT);
  dht.begin();
}

void loop()
{
  delay(1000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  sensor_analog = analogRead(sensor_pin);
  fuktighetsprocent = ( 100 - ( (sensor_analog / 1023.00) * 100 ) );
  float hj = fuktighetsprocent;
  Serial.print(",");
  Serial.print(h);
  Serial.print(",");
  Serial.print(t);
  Serial.print(",");
  Serial.println(hj);
  if (hj < 50)
  {
    //pumpOn();
    digitalWrite(relayPin, LOW);
    delay(2000);
  }
  else
  {
    //pumpOff();
    digitalWrite(relayPin, HIGH);
  }
  delay(1000);
}

Tom.. :grinning: :+1: :coffee: :australia:


Terminal:
This is how I want it to look

Oh thanks, didn't know how to achieve that here

Not always. The code you posted produces a short LOW pulse before going HIGH. If you want an output to begin HIGH (for example as with an active LOW relay input that is pulled HIGH by an external pull up resistor), you have to do it in the reverse order, as the OP did. The output state written by digitalWrite is saved in a hardware latch that is independent of the output buffer stage.

Thank you for the explanation![quote="aarg, post:14, topic:918602, full:true"]

Not always. The code you posted produces a short LOW pulse before going HIGH. If you want an output to begin HIGH (for example as with an active LOW relay input that is pulled HIGH by an external pull up resistor), you have to do it in the reverse order, as the OP did. The output state written by digitalWrite is saved in a hardware latch that is independent of the output buffer stage.
[/quote]

I’m gonna try to change that and reconnect here thanks