/******************************** name: 5V relay module and DHT 11/22/21 humidity and temperature sensor module function: This program shows how the temperature and humidity sensor turns on devices or power sockets connected to 5V relay module email: info.acoptex@gmail.com web: http://acoptex.com ********************************/ #include // include library const int relayPin = 8; const int DHTPIN = A0; // Uncomment whatever type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(DHTPIN, DHTTYPE);// Initialize DHT sensor /********************************/ void setup() { Serial.begin(9600); pinMode(relayPin, OUTPUT); dht.begin(); } void loop() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Temperature = "); Serial.print(t); Serial.print(" *C "); Serial.print("Temperature = "); Serial.print(f); Serial.print(" *F "); Serial.print("Humidity = "); Serial.print(h); Serial.print(" %\t "); Serial.println(); if (h >= 66) { // you can change humidity value here - h>=66 to your preffered number digitalWrite(relayPin, LOW); } else { digitalWrite(relayPin, HIGH); } if (t >= 25) {// you can change temperature value here - t>= 30 to your preferred number or change from Celsius to Fahrenheit readings digitalWrite(relayPin, LOW); } else { digitalWrite(relayPin, HIGH); } delay(1000);// 1 second delay between measurements }