Class for DHT11, DHT21 and DHT22 (temperature & humidity)

you will pick up speed as long as you keep asking questions! (no there ain't no bad questions, only bad answers)

Updated the playground to version 0.1.09

  • reduced the foot print with 30 bytes.
  • predecrement in timeoutloops
  • use of mask to build up bytes.
  • added time measurement in the DHT22 example sketch.
    On average a reading takes 24- 25 milliseconds, most of this time is the wake up call.
    Should be investigated as this is now 80% of the blocking time.

Recently confirmed to work on: Digistump Digix 84 MHz Clock Speed too.
- DHT22 Timeout error - Digistump Digix with level-shifter - Sensors - Arduino Forum -

I've copy+paste the code for the dht.h and dht.cpp from github. But if i use it for my scetch i have this strange "\stray" errors.

.../dht.h:1: error: stray '\357' in program
.../dht.h:1: error: stray '\273' in program
.../dht.h:1: error: stray '\277' in program

I think it means there is an error in the dht.h file. Isn't it?
With the older revision of the library everything was fine.

cannot replicate your error.

Can you zip the dht.h that fails and attach it in a post here ?
Are you on windows mac unix?
What editor (settings) are you using? ==> is the file changed to UTF-8 format instead of ASCII?

link to the previous version 1.0.8 - Arduino/dht.h at 4126dfc63a3dc645bfc518e0cefe3cda653903cf · RobTillaart/Arduino · GitHub -

Hi Rob,
thanks for reply.
You had the right instinct. :slight_smile:
There was the same 3 characters in both files too much. The files was changed to UTF-8 and everything seems ok.

Many thanks .
Joerg

You're welcome.

please let me know if any other issues pop up.

Hello,
at first many thanks for this great lib.

I tested it on a pro mini with 3,3v and 16MHz and it works fine.

For safety work i bought some 3,3V 8 MHz boards but run in trouble with the 8 MHz.

Could You please help me to make it work with please :slight_smile: I choose the right board, serial and one wire sensors works fine.

So could I make changes to make it run with 8MHz 328p?

Thanks in advance and regards

Joerg

[code] but run in trouble with the 8 MHz.[/code]
Can you explain the kind of trouble you have?
What output did your sketch generate?
(Please post it here for others reference - I know you mailed it to me)

Have you tried to run the examples?

You mailed me you renamed the lib because of a name conflict, please can you move the other lib from the libraries folder and use the original name.

ok for better verification I build a simple sketch based on yours:

//
//    FILE: dht_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.07
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//     URL: http://arduino.cc/playground/Main/DHTLib
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT22_PIN 2

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT22, \t");
  int chk = DHT.read22(DHT22_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(1000);


 
}
//
// END OF FILE
//

Output is:

Read sensor: Time out error<\r>
<\n>Humidity (%): 0.00<\r>
<\n>Temperature (??C): 0.00<\r>
<\n>Temperature (??F): 32.00<\r>
<\n>Temperature (??K): 273.15<\r>
<\n>Dew Point (??C): nan<\r>
<\n>Dew PointFast (??C): nan<\r>

do you have a pull up resistor (4K7 or 10K) on the dataline?

I have 4k7 on data line and it works fine with a pro mini with 16 MHz

Thanks, worked for me, used a 10k

with 8 MHz ?

Sorry no, using nano @16mhz, was getting odd / erroneous readings from dht11, 10k pullup and it works

@bevangg
how long/short are your wires?

Hi see attached images (right click and view in new tab, they are too big for the forum viewer), the DHT11 is on a prototyping board and is wired using very short connections. I have not got around to trimming the resistor pins and probably won't bother as it is a prototype at this stage. Since putting the 10k pull-up resistor on it readings have been consistently accurate (95% as a guess) and not wildly variant as it was before ie temperature showing 120 centigrade. The nano can't adequately supply so many relays and sensors when connected over usb hence the power supply module as the voltage drops to 3.7 which is not enough for the 433mhz receiver to function. The DHT11 does not seem to be affected by the voltage at all. All parts were bought at banggood.com, cheap, quality and fast delivery (relatively by chinese standards). The whole lot in the shots including the nano cost £12.50. The relays can be controlled by the sensors, the rtc or over the internet using lightwaverf.

With 4 relay's you definitely need an additional power supply! in fact even with one you need to have it.

Hello where can I download this library?

Thanks.

some explanations - Arduino Playground - DHTLib -
latest code - Arduino/libraries/DHTlib at master · RobTillaart/Arduino · GitHub -

I was reading that but I dont seem to find the "download link" for the library, normally on the other libraries I have, I downloaded them.
can you please help me?
What to I need to do in order to have this library?