/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-heating-system
*/
// DHT22 Initial
#include "DHT.h"
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define RELAY_PIN 33 // Arduino pin connected to relay which connected to heating element
int temperature_detection () {
delay(1000);
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
if (t < 23) {
Serial.println("Temperature too low. It is: " + String(t));
return 1;
}
if (t >= 23 && t <= 25) {
Serial.println("Temperature is good. It is: " + String(t));
return 2;
}
if (t > 25) {
Serial.println("Temperature too high. It is: " + String(t));
return 3;
}
}
void setup() {
Serial.begin(9600); // initialize serial
// DHT22 Setup
dht.begin();
pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output
}
void loop() {
if (temperature_detection() == 1) {
Serial.println("The heating element is turned on");
digitalWrite(RELAY_PIN, HIGH); // turn on
}
else if (temperature_detection() == 2 || temperature_detection() == 3)
Serial.println("The heating element is turned off");
digitalWrite(RELAY_PIN, LOW); // turn off
delay(1000);
}