Hi, for a few months the project of switching the fan based on indoor and outdoor humidity measurements worked well. All of a sudden, after restarting ARDUINO UNO, the fan starts only once, and reports a sensor reading error when the loop is finished. I replaced the sensors, I replaced the relay. And nothing. Does anyone have any ideas? Thank you
Kit: Arduino Uno, 2x DHT22 sensors, 5V relay module - 1 channel with level switch. The relay is connected to a 220V tubular fan.
Source code:
#include <DHT.h>
#define DHTPIN_OUT 2
#define DHTPIN_IN 3
#define DHTTYPE DHT22
DHT dht_outdoor(DHTPIN_OUT, DHTTYPE);
DHT dht_indoor(DHTPIN_IN, DHTTYPE);
const int RELAY_PIN = 7;
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.begin(9600);
dht_outdoor.begin();
dht_indoor.begin();
}
void loop() {
delay(60000); // 1 minute delay
float outdoor_humidity = dht_outdoor.readHumidity();
float indoor_humidity = dht_indoor.readHumidity();
float indoor_temp = dht_indoor.readTemperature();
if (isnan(outdoor_humidity) || isnan(indoor_humidity) || isnan(indoor_temp)) {
Serial.println("Error reading data from sensors!");
return;
}
Serial.print("Outdoor sensor - Humidity: ");
Serial.print(outdoor_humidity);
Serial.print("% Temperature: ");
Serial.print(dht_outdoor.readTemperature());
Serial.print("°C\n");
Serial.print("Indoor sensor - Humidity: ");
Serial.print(indoor_humidity);
Serial.print("% Temperature: ");
Serial.print(indoor_temp);
Serial.print("°C\n");
if (outdoor_humidity < 60.0 && indoor_humidity > 60.0) {
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Relay switched ON");
delay(1800000); // 30 minute delay
digitalWrite(RELAY_PIN, LOW);
Serial.println("Relay switched OFF");
} else {
digitalWrite(RELAY_PIN, LOW);
Serial.println("Relay switched OFF");
}
}
Serial monitor result after start:
Outdoor sensor - Humidity: 43.50% Temperature: 19.90°C
Indoor sensor - Humidity: 65.90% Temperature: 19.70°C
Relay switched ON
Relay switched OFF
Error reading data from sensors!
Error reading data from sensors!
Error reading data from sensors!
Error reading data from sensors!
Error reading data from sensors!
moderator edit: fixed tags