hello i have been trying to use a DHT11 but i cant get the if else statements to work and have been trying for a week or 2 to get it to work with some tutorials and have followed them perfectly but cant get the if else statements work can anyone please help me?
Hi. Read some helpful forum tips and then format and copy your code and paste here.
okay here is the current code that i use :
#include <Relay.h>
#include <dht.h>
const int RELAY_FAN_PIN = 3;
#define RELAY_FAN_PIN A5 ;
#define DHTPIN 12 ;
#define DHTTYPE DHT11 ;
const int TEMP_THRESHOLD_UPPER = 25;
const int TEMP_THRESHOLD_LOWER = 20;
// dht.begin();
dht DHT;
float temperature;
void setup()
{
Serial.begin(9600);
}
void loop()
{
delay(2000);
int readData = DHT.read11(12); // DHT11
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
if(temperature > TEMP_THRESHOLD_UPPER){
Serial.println("The fan is turned on");
digitalWrite(3,HIGH); // turn on
} else if(temperature < TEMP_THRESHOLD_LOWER){
Serial.println("The fan is turned off");
digitalWrite(3,LOW); // turn on
}
}
}`
@ltb Why do users choose to ignore the advice on posting code ?
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
Where does your code read the temperature into the temperature variable ?
Hi,
What model Arduino are you using?
Are you getting readings from the DHT11?
What do you get in the IDE monitor?
Thanks.. Tom...
![]()
i have added this to the code :
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
at one point for a bit and it showed the temp but the if else statements still didnt work
im using a arduino uno and the DHT11 shows that its 30 degrees
Hello
Well I guess you have an item to look for is the korrect usage of data types.
p.s.
Did you check the output of the compiler ?
Hi,
Can you post in a new post your updated code please.
Thanks.. Tom..
![]()
here is the my updated code :
#include <Relay.h>
#include <dht.h>
const int RELAY_FAN_PIN = 3;
#define RELAY_FAN_PIN A5 ;
#define DHTPIN 12 ;
#define DHTTYPE DHT11 ;
const int TEMP_THRESHOLD_UPPER = 25;
const int TEMP_THRESHOLD_LOWER = 20;
// dht.begin();
dht DHT;
float temperature;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
delay(2000);
int readData = DHT.read11(12); // DHT11
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
if(temperature > TEMP_THRESHOLD_UPPER){
Serial.println("The fan is turned on");
digitalWrite(3,HIGH); // turn on
} else if(temperature < TEMP_THRESHOLD_LOWER){
Serial.println("The fan is turned off");
digitalWrite(3,LOW);
}
}
}
You have already been pointed out the error.

You are reading one variable and checking another.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.