Basically, this project aims to control a relay with a temperature. What I would like to do is when the temperature is below 38°C, the relay turns on and when the temperature exceeds 38°C, it will turn off. But, what happens is that the relay doesn't turn off even if the temperature exceeds 38°C.
For reference, I am using DHT11 for the temperature sensor. The setup is working just fine, it's just the condition where I'm having problems
Here's the code:
#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 7
#define DHTTYPE DHT11
int relay1 = 3;
int relay2 = 4;
int temp = 7;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
dht.begin();
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
one();
//relay and motion connection
delay(2000);
}
void one() {
float dhtHumidity = dht.readHumidity();
float dhtTemperature = dht.readTemperature();
delay(1000);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(dhtTemperature);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Humid: ");
lcd.print(dhtHumidity);
lcd.print(" H");
}
void two() {
temp = dht.readTemperature();
if (temp < 38) {
digitalWrite(relay1, LOW);
}
else {
digitalWrite(relay1, HIGH);
}
}
Note:
- The LCD Display is working just fine as well.
- I accidentally bought a 2 channel relay, but I will not use the 2nd channel.