Basics with DHT11 - DHTLIB_ERROR_TIMEOUT

I just bought the sensor DHT11 and trying to test it with the library code provided by the IDE, but I always get the same error on .read() . Code -2

Environment:
-Windows 8.1 64bits.
-Arduino IDE: 1.5.8 BETA
-Tested in arduino uno and arduino yún

/*####################################################################
 FILE: dht11_functions.pde - DHT11 Usage Demo.
 VERSION: 2S0A

 PURPOSE: Measure and return temperature & Humidity. Additionally provides conversions.

 LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
 GET UPDATES: https://www.virtuabotix.com/

      --##--##--##--##--##--##--##--##--##--##--
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      | ##  ##  ##  ##  ##  ##  ##  ##  ##  ## |
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      | ##  ##  ##  ##  ##  ##  ##  ##  ##  ## |
      ##  ##  ##  ## DHT11 SENSOR ##  ##  ##  ##
      ##  ##  ##  ##  ##FRONT ##  ##  ##  ##  ##
      | ##  ##  ##  ##  ##  ##  ##  ##  ##  ## |
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      | ##  ##  ##  ##  ##  ##  ##  ##  ##  ## |
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      ##  ##  ##  ##  ##  ##  ##  ##  ##  ##  ##
      --##--##--##--##--##--##--##--##--##--##--
          ||       ||          || (Not    ||
          ||       ||          || Used)   ||
        VDD(5V)   Readout(I/O)          Ground

  HISTORY:
  Joseph Dattilo (Virtuabotix LLC) - Version 2S0A (27 May 12)
  -Rewritten to with more powerful Versalino functionality
  Joseph Dattilo (Virtuabotix LLC) - Version 0.4.5 (11/11/11)
  -Made Library Arduino 1.0 Compatible
  Joseph Dattilo (Virtuabotix LLC) - Version 0.4.0 (06/11/11)
  -Fixed bugs (squish)
  Mod by Rob Tillaart - Version 0.3 (28/03/2011)
  Mod by SimKard - Version 0.2 (24/11/2010)
 George Hadjikyriacou - Original version (??)
#######################################################################*/


#include <dht11.h>

dht11 DHT11;

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

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);

  Serial.print("Temperature (°K): ");
  Serial.println(DHT11.kelvin(), DEC);

  Serial.print("Dew Point (°C): ");
  Serial.println(DHT11.dewPoint(), DEC);

  Serial.print("Dew PointFast (°C): ");
  Serial.println(DHT11.dewPointFast(), DEC);

  delay(2000);
}

My output:

DHT11 TEST PROGRAM
LIBRARY VERSION: 2S0A

Read sensor: Time out error
Humidity (%): 0.0000000000
Temperature (°C): 0.0000000000
Temperature (°F): 32.0000000000
Temperature (°K): 273.1499938964
Dew Point (°C): nan
Dew PointFast (°C): nan

Read sensor: Time out error
Humidity (%): 0.0000000000
Temperature (°C): 0.0000000000
Temperature (°F): 32.0000000000
Temperature (°K): 273.1499938964
Dew Point (°C): nan
Dew PointFast (°C): nan

My wiring:

-left pin: 5v
-right pin: gnd
-middle pin: 5v - digital2 - through 10kO resistor


Any idea why I cannot read?

No I cannot see where it fails. However you can try my latests DHT library

robtillaart:
No I cannot see where it fails. However you can try my latests DHT library

Thank you for the answer.

I downloaded the latest version 0.1.17 and tried to run the dht11_test. I connected the middle pin of the dht11 sensor to the digital pin 5

#define DHT11_PIN 5

This is my output:

DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.17

Type,    status,    Humidity (%),    Temperature (C)
DHT11,     Unknown error,    0.0,    0.0
DHT11,     Unknown error,    0.0,    0.0
DHT11,     Unknown error,    0.0,    0.0

So it seems that still not working.
I printed the chk value and it is -3.

I tried also without resistor because I saw the dht11 I bought comes with a 103 built onboard. Maybe my sensor is broken :confused:

I have an multimeter, but I am not very confident with it. How could I check if the sensor is ok?

Update:
I disassembled the dht11 from the board.(the model from sainsmart is this( [Discontinued] SainSmart DHT11 Temperature And Relative Humidity Senso – SainSmart.com )
Now the ouput is this:

DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.17

Type,	status,	Humidity (%),	Temperature (C)
DHT11, 	OK,	0.0,	0.0
DHT11, 	OK,	0.0,	0.0
DHT11, 	OK,	0.0,	0.0

Progress but still not working...

Connection problem. From the .h file
#define DHTLIB_ERROR_CONNECT -3

That means the sensor does not react on the wake up call of the DHT library.
Probably the Adafruit library had the same problem.

(I should update the example sketch to print the right error message - thanks for that reminder :wink:

I have also this (obsolete) DHT11 specific lib you can try - Arduino Playground - DHT11Lib - but I expect it will fail in a similar way.

robtillaart:
Connection problem. From the .h file
#define DHTLIB_ERROR_CONNECT -3

That means the sensor does not react on the wake up call of the DHT library.
Probably the Adafruit library had the same problem.

(I should update the example sketch to print the right error message - thanks for that reminder :wink:

I have also this (obsolete) DHT11 specific lib you can try - Arduino Playground - HomePage - but I expect it will fail in a similar way.

Thank you for the answer.
I installed the dht11 specific one and seems it works:

Read sensor: OK
Humidity (%): 59.00
Temperature (°C): 24.00
Temperature (°F): 75.20
Temperature (°K): 297.15
Dew Point (°C): 15.51
Dew PointFast (°C): 15.48

I would like to know the reason why the dht generic is wrong ... What could I test?

In my 0.1.17 DHT lib I have tried to minimize the time outs to get best performance out of the lib.
If you change this line in .h file the handshake gets more time => less critical

#define DHTLIB_TIMEOUT  10000    // now it is 400

That might get it working. I'll make a note to do additional testing with a DHT11
(I concentrated mostly on DHT22/33)

Wiring is not ok
Replace orange wire (Data) by a twisted pair (one wire for Data, one wire for the Ground)

68tjs:
Wiring is not ok
Replace orange wire (Data) by a twisted pair (one wire for Data, one wire for the Ground)

The wiring is working as it is. Could you please post a diagram with the one you say?

@robtillaart:

I tried what you said replacing

#define DHTLIB_TIMEOUT 10000

but still doesn't work.
The dht11 old version works. But still would like to use the generic updated version.

Another related question. I am testing two different dht11 sensor.
The first works without problem with the dht11 'old version':
Dht11

The second is a dht11 I bought in Sainsmart. This one doesn't work with any version I try.
It has a small piece of cirtuic wich says 103. It is a capacitor? resistor...? How should the wiring be with this sensor?
dht11 on board

Thanks again.

Type,	status,	Humidity (%),	Temperature (C)
DHT11, 	OK,	0.0,	0.0
DHT11, 	OK,	0.0,	0.0
DHT11, 	OK,	0.0,	0.0

The reason it says OK is that all bytes got from the sensor are 0 and the CRC is a simple addition making the CRC also 0 ==> the library is fooled by limitation of the sensor.

As said I need to do more testing with DHT11, - but I'm out of stock, need to order new ...

robtillaart:

Type,	status,	Humidity (%),	Temperature (C)

DHT11, OK, 0.0, 0.0
DHT11, OK, 0.0, 0.0
DHT11, OK, 0.0, 0.0




The reason it says OK is that all bytes got from the sensor are 0 and the CRC is a simple addition making the CRC also 0 ==> the library is fooled by limitation of the sensor.

As said I need to do more testing with DHT11, - but I'm out of stock, need to order new ...

No hurry. I am checking also about the the difference between the dht11 sensors.

I found a schema for the dht22 sainsmart schema and works.

Hi Rob,

I recently downloaded your latest DHT library and tried to run using a DHT11. I get a connect error (code -3). I am able to run with another library, I believe, your old obsolete one, and it works just fine with the same wiring, etc..

It looks like this issues still exists in your latest library.

Thanks,
Raj

The latest version (0.1.14 and up) is experimental AVR only code.

Please use DHTstable - Arduino/libraries/DHTstable at master · RobTillaart/Arduino · GitHub

I've posted my experiences with DHT11 at...

That gives you some code that doesn't rely on a library, if you want to take that approach. It also reports some Gotchas that people who haven't used the DHT11 much might be tripped up by.