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;
}
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.
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.
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?
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]