Humidity and temperature control with DHT11

I’m making an incubator. I use a DHT11. It will show temp and humidity informations on lcd, it shows successfully but it doesn’t open/close the heater lamp. Codes are here:

NOTE: Trifle the lcd codes please

#include <dht11.h>
#define DHT11PIN 9

dht11 DHT11;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int chk = DHT11.read(DHT11PIN);

  if (((float)DHT11.humidity, 2) <= 55) {
    Serial.println("3-HIGH");
    Serial.println((float)DHT11.humidity, 2);
    digitalWrite(3, HIGH);
    return;
  }
  if (((float)DHT11.humidity, 2) > 50) {
    Serial.println("3-LOW");
    digitalWrite(3, LOW);
    Serial.println((float)DHT11.humidity, 2);
    return;
  }


  if (((float)DHT11.temperature, 2) <= 37) {
    Serial.println("4-LOW");
    digitalWrite(4, LOW);
  }
  if (((float)DHT11.temperature, 2) > 36) {
    digitalWrite(4, HIGH);
    Serial.println("4-HIGH");
  }


}

You need to set the pinModes of the output pins to OUTPUT before using digitalWrite. Best done in setup().

if (((float)DHT11.humidity, 2) <= 55) {

The , 2 in that if will not do what you think. Remove it there and in the other if statements. It will work in the print, though.

@smg_exe didn't specify which DHT11 library is in-use (there are sever). But, there's likely no need for the cast to float.

Hi,
Can I suggest you read the temp and humidity into two variables at the start of the loop, then use those variables in your if statements.
It will look cleaner and easier to read.
Also you will be taking a snapshot of your project at the start of the loop and acting apon it, then reading again and acting.

Tom.. :slight_smile:

New codes.

serial:
“3-HIGH
temp:
2
humidity:
2”

It still doesn’t works :frowning:

#include <dht11.h>
#define DHT11PIN 9

dht11 DHT11;

void setup()
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int chk = DHT11.read(DHT11PIN);
  int h = ((float)DHT11.humidity, 2);
  int t = ((float)DHT11.temperature, 2);
  if (h <= 55) {
    Serial.println("3-HIGH");
    digitalWrite(3, HIGH);
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    return;
  }
  if (h > 50) {
    Serial.println("3-LOW");
    digitalWrite(3, LOW);
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    return;
  }


  if (t <= 37) {
    Serial.println("4-LOW");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    digitalWrite(4, LOW);
    delay(1000);
    return;
  }
  if (t > 36) {
    digitalWrite(4, HIGH);
    Serial.println("4-HIGH");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    return;
  }


}

It doesn’t work because it is still wrong.
Lose the ", 2"s.

Also, if you’re assigning to an “int”, what is the point of casting to float?

Also, a simple “else” instead of two "if"s.

And lose the "return"s

And.print the temperature and humidity just ONCE as soon as they are read.

OOOOHH THANK YOU!!

Final codes:

#include <dht11.h>
#define DHT11PIN 9

dht11 DHT11;

void setup()
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int chk = DHT11.read(DHT11PIN);
  int h = ((float)DHT11.humidity);
  int t = ((float)DHT11.temperature);
  if (h <= 55) {
    Serial.println("3-HIGH");
    digitalWrite(3, HIGH);
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    if (t <= 37) {
    Serial.println("4-LOW");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    digitalWrite(4, LOW);
    delay(1000);
    return;
  }
  if (t > 36) {
    digitalWrite(4, HIGH);
    Serial.println("4-HIGH");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    return;
  }

    return;
  }
  if (h > 50) {
    Serial.println("3-LOW");
    digitalWrite(3, LOW);
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    if (t <= 37) {
    Serial.println("4-LOW");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    digitalWrite(4, LOW);
    delay(1000);
    return;
  }
  if (t > 36) {
    digitalWrite(4, HIGH);
    Serial.println("4-HIGH");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    return;
  }

    return;
  }


  if (t <= 37) {
    Serial.println("4-LOW");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    digitalWrite(4, LOW);
    delay(1000);
    return;
  }
  if (t > 36) {
    digitalWrite(4, HIGH);
    Serial.println("4-HIGH");
    Serial.println("temp:");
    Serial.println(t);
    Serial.println("humidity:");
    Serial.println(h);
    delay(1000);
    return;
  }


}

Final codes:

You’re new here, aren’t you?

TheMemberFormerlyKnownAsAWOL:
You're new here, aren't you?

Once upon a time, we all were new here -- someone was twice new here? :slight_smile:

GolamMostafa:
Once upon a time, we all were new here – someone was twice new here? :slight_smile:

The point was, it almost certainly isn’t the final code.

I hate it when I have to explain gags.

@OP

The Library (dht11.h) has no examples. Try to operate your sensor using this Library: SimpleDHT.h which comes with many examples.

#include <SimpleDHT.h>
int pinDHT11 = 4;  //Humidity DATA line at A0-pin of UNO
SimpleDHT11 dht11;  //object is created from class

byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  dht11.read(pinDHT11, &temperature, &humidity, NULL);
  Serial.println((int)humidity);
  Serial.println((int)temperature);
  delay(2000);
}

SimpleDHT-master.zip (17.3 KB)