/******************************** 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 libraries #include #include #include 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 LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);//Initialise the LCD 1602 I2C module /********************************/ void setup() { Serial.begin(9600); pinMode(relayPin, OUTPUT); dht.begin(); } void loop() { //Define the LCD as 16 column by 2 rows lcd.begin (16, 2); //Switch on the backlight lcd.setBacklight(HIGH); // 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; } //goto first column (column 0) and first line (Line 0) lcd.setCursor(0, 0); lcd.print("Temp = "); lcd.print(t); lcd.print(" *C "); lcd.setCursor(0, 1); lcd.print(" Hum = "); lcd.print(h); lcd.print(" % "); 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 >= 26) {// 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 }