DHT11 switching between INVALID VALUE -999 and appropriate data

Hi!
My project is pretty simple. Its about visualizating measured data by DHT11 of temperature and humidity on LCD display (16x2), however the part with measuring doesnt work.

The problem is that DHT11 is returning value -999, which, according to library, should be a DHTLIB_INVALID_VALUE, and then sends the appropriate data of temperature and humidity. This cyclus is repeating (see picures).

This appears on the LCD display, however variable "chk" that I print on the serial monitor returns 2 errors: DHTLIB_ERROR_TIMEOUT and DHTLIB_ERROR_CHECKSUM (see picture).
error

Rarelly the DHT11 even return: DHTLIB_OK...

Here is my code:

#include <dht.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
dht dht;

int dhtPin = 2;

void setup() {

  Serial.begin(115200);

  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("Temp: ");

  lcd.setCursor(0, 1);
  lcd.print("Hum: ");
}

void loop() {

  int chk = dht.read11(dhtPin);

  switch (chk) {
    case DHTLIB_OK:
      Serial.println("DHT11: OK");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.println("Checksum error");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.println("Timeout error");
      break;
    case DHTLIB_INVALID_VALUE:
      Serial.println("Invalid value");
    default: 
      Serial.println("Unknown error");
      break;
  }

  lcd.setCursor(6, 0);
  lcd.print(dht.temperature);

  lcd.setCursor(5, 1);
  lcd.print(dht.humidity);

  delay(500);
}

I am using DHTlib library, see the refference.

Wiring should be all right. I have a DHT11 as a modul that already has a pull-up resistor.

This is my first post on forum, therefore I will be glad for any advice about what I should improve for the next time.

Thanks!

correction:

So you're getting Timeouts, and corrupt data - that could well be due to a dodgy connection.

Might be a faulty sensor - do you have any others to try?

Please post your schematic, and some good clear photos of your setup - someone might be able to spot something...

OK, I will try to wire up everything again with a different wires.

Unfortunatelly I dont have a any other DHTs to try.

Didnt work. Here is the photo:


Probably the only important thing is wire up of the sensor (you sholud be able to see the signs "+", "-", and "out" on the sensor module) and pin 2 of MEGA.

Please try - GitHub - RobTillaart/DHTNew: Arduino library for DHT11 and DHT22 with automatic sensor recognition
The DHTLIB library is rather old (although should be pretty stable)

Pull up resistor 4K7 on the data line might improve communication.

1 Like

@keri27
You need to wait 2 seconds between reads.

3 Likes

Thank you, apparently this works! Now I can see only the appropriate values on the display. Serial monitor still sometimes shows the DHTLIB_ERROR_CHECKSUM, but it looks much better.

Thanks for advice, I will try this library as well.

2 seconds is the minimum. Try delay(2200) see if it gets better.

1 Like

It didnt. I have tried even longer delay as 3 s, but the serial monitor looks like this:
image
Its strange because the sensor seems working e.g. when I put my finger on it, I can see the change of the humidity immediately (like 3-4s).

Maybe the onboard pull-up is bad or wrong.
If you have a 4.7K or 5.1K or 10K resistor, try connecting it between the DHT11 out pin and 5V

2 Likes

Better un-mark that as the solution, then.

This kind of thing can happen when something is marginal; ie, when it's working, it's only barely working - so it only a tiny disturbance to push it over the edge to not working.

1 Like

I used a wrong pin, therefore I remove the previous response. The DHTNew library test seems good:

I connect the 10K resistor between out pin and 5V, but apparently nothing changed (see wiring)

I also started printing temperature and humidity via serial monitor and thi s is how it looks like.:

Most of the time there is a checksum error, although the values of temperature and humidity are still going.

So everything OK now?

Is that with The DHTNew library?

No, this is the previous library DHTlib, but I will try the DHTNew as soon as possible.

I have already tried the latest version of DHTNew library and it seemed to me there was less invalid values, though there were still some...

Here is my code:

#include <dhtnew.h>
#include <LiquidCrystal_I2C.h>

DHTNEW dht(2);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() 
{
  Serial.begin(115200);
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHTNEW_LIB_VERSION);

  //dht.setSuppressError(1);

  lcd.init();
  lcd.backlight();
}

void loop() 
{
  if (millis() - dht.lastRead() > 2000) {
  
    int chk = dht.read();
    switch (chk) {
      case DHTLIB_OK:
        Serial.println("DHT11: OK");
        break;
      case DHTLIB_ERROR_CHECKSUM:
        Serial.println("Checksum error");
        break;      
      case DHTLIB_ERROR_TIMEOUT_A:
        Serial.println("Timeout error");
        break;      
      case DHTLIB_INVALID_VALUE:
        Serial.println("Invalid value");
        break;            
      case DHTLIB_ERROR_BIT_SHIFT:
        Serial.println("Bit shift error");
        break;            
      case DHTLIB_ERROR_SENSOR_NOT_READY:
        Serial.println("DHT11: Not ready");
        break;                  
      default:
        Serial.print("Unknown: ");
        Serial.println(chk);
    }
    Serial.print("Temperature: ");
    Serial.print(dht.getTemperature(), 1);
    Serial.println(" C");

    Serial.print("Humidity: ");
    Serial.print(dht.getHumidity(), 1);
    Serial.println(" %");

    lcd.clear();  
    lcd.setCursor(0, 0);
    lcd.print("Teplota: "); //print temperature
    lcd.print(dht.getTemperature(), 1);
    lcd.print(" C");

    lcd.setCursor(0, 1);
    lcd.print("Vlhkost: "); //print humidity
    lcd.print(dht.getHumidity(), 1);
    lcd.print(" %");
    Serial.println("--------------------");
  }
}

Most of the time I receive DHT11: OK, but sometimes I also receive:
image
... sometimes its pretty often and sometimes its superb rare. This can be particullary solved by DHTNew function setSuppressError():

the library will not output -999 but the last known valid value for temperature and humidity. This flag is useful to suppress 'negative spikes' in graphs or logs.

I would say its fault of the sensor itself (hardware), therefore I will try to find out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.