DHT 11 with Relay issues

hello, im quite certain its in the code some where as a I am a beginner to programming . I modified the DHT11 code with some IF statements to trigger relays when the values reach a certain point however they are not working. The rest of the code displays all the information fine.
here is the code . Any help is greatly appreciated! thank you :slight_smile:

#include <dht11.h>

dht11 DHT11;

int humid = 3;
int temp = 4 ;
int fan = 5 ;

void setup()
{
DHT11.attach(2);
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);

pinMode (humid, OUTPUT);
pinMode (temp,OUTPUT);
pinMode (fan,OUTPUT);
}

void loop()
{
Serial.println("\n");

int chk = DHT11.read();

Serial.print("Read sensor: ");
switch (chk)
{
case 0:
Serial.println(“OK”);
break;
case -1:
Serial.println(“Checksum error”);
break;
case -2:
Serial.println(“Time out error”);
break;
default:
Serial.println(“Unknown error”);
break;
}

Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, DEC);

Serial.print("Temperature (°C): ");
Serial.println((float)DHT11.temperature, DEC);

Serial.print("Temperature (°F): ");
Serial.println(DHT11.fahrenheit(), DEC);

/* Humidity */
if (DHT11.humidity < 70){
digitalWrite (humid, HIGH);
}

else
{
digitalWrite (humid, LOW);
}

/* Temp */
if (DHT11.fahrenheit() < 72) {
digitalWrite (temp, HIGH);
}
else {
digitalWrite (temp,LOW);
}

if (DHT11.fahrenheit() > 76) {
digitalWrite (fan,HIGH);
}
else {
digitalWrite (fan,LOW);
}

delay(2000);
}

See #7 http://forum.arduino.cc/index.php?topic=207045.0

How are your relays connected to the Arduino?

from the output pin to the INT on the relay board. Its a 8 relay board with male header pins for connections. Like a "sainsmart" It works with the standard blink example and the same connections. I also cannot get a LED To light up if I try replacing the Relays with a LED. it seems the IF statements are not functioning correctly with the argument.

Thank you

What is print on the serial monitor for these: Serial.println((float)DHT11.humidity, DEC);

Serial.println(DHT11.fahrenheit(), DEC);

Humidity (%): 37.0000000000 Temperature (°C): 15.0000000000 Temperature (°F): 59.0000000000

copied directly from the serial monitor

Try this:
/* Temp */
float x = DHT11.fahrenheit() ; // or maybe float x = (float)DHT11.fahrenheit
if (x < 72.0) {
digitalWrite (temp, HIGH);
}

etc.

EDIT: I have not use the DHT11 yet, what do you get when you try these:
Serial.println(DHT11.temperature, DEC);
Serial.println((float)DHT11.temperature, DEC);

that was the ticket. Seems to be working correctly now. Thank you very much!

could you provide a brief explanation of why this works apposed to my method? or even link me to an article about these types of situations in order to strengthen my understanding..

Since the library (on the playground) returns floats for temperature and humidity. it seems pointless to cast the values as floats and print them with 10 digits after the decimal place. So, why are you doing either one?

The library (on the playground) does not have a fahrenheit() method. So, it appears that it isn't the playground version on the library that you are using. No clue as to which one you are using, so, you're on your own figuring out why it didn't work.

Sorry I didn't specify earlier. I am using the DHT11 Library from Virtuabotix.

https://www.virtuabotix.com/product/virtuabotix-dht11-temperature-humidity-sensor/

Thanks for all the help!