Go Down

Topic: [LIB] Interrupt driven DHTLib (DHT11 & DHT22 = idDHTLib) (Read 24874 times) previous topic - next topic


Jul 08, 2013, 09:49 pm Last Edit: Aug 07, 2013, 06:13 pm by niesteszeck Reason: 1
As turgo kindly ask if someone can add DHT22 functionality to the idDHT11 lib, I make a new lib that can use DHT11 and DHT22 sensors based on the suggestions by pylon.
You can get it from

I have tested it with a DHT11 sensor on a Mega2560 on pin 19 (int4), but I don't have a DHT22 sensor to prove it, so if you have one and can confirm that it is working, please let me know to get out the beta label of the code.

EDIT: Finally is ready. Thanks All for the testing


Jul 28, 2013, 08:06 pm Last Edit: Jul 28, 2013, 08:10 pm by Dessimat0r Reason: 1
DHT22 not working, I'm afraid. I did have trouble getting the non-interrupt version to work, however, which I solved by rearranging the construction order of various objects. For some reason the DHT object construction had to be first even though it did nothing meaningful in its constructor, so I wouldn't rule out a problem there. However, I do still have the idDHT object ctor first. I'll try with the barebones example next.

Edit: Scrub that. Now it's working. I didn't even do anything! But temperature is negative and invalid. It's reading -39c when it's actually 25c or so. Humidity might be correct but not sure.


What I 'changed' was seemingly taking the Arduino with sensor upstairs to my bedroom, where it is warmer. The sensor shows -2.00 temp until 25c (reading on another temperature monitor not connected to Arduino) / -39.50c on Arduino. After it reads the values, those values can be obtained but then appears to be permanently 'acquiring' after another acquire() call.

Tested on Uno R3.


Jul 28, 2013, 09:12 pm Last Edit: Jul 28, 2013, 09:49 pm by Dessimat0r Reason: 1
I added some wait time between the acquisitions rather than starting a new one straight after getting the results (1 sec) from the one one and it's working a little better, but a lot of the time it's showing -1 for temp and hum. The temperature is still negative and incorrect whenever it shows something not -1.


Just to say, the other Adafruit lib worked great without checksum errors once the problems were fixed, so my wiring should not be at fault.


Whatever you do you need a 2 second delay between calls to the DHTXX, 2.5 seconds is better. It sounds like you are polling it too fast.

--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard


Still getting frequent checksum errors even with 2.5secs. The data and checksum are probably corrupt somehow after being retrieved in idDHTLib.


Jul 29, 2013, 03:52 pm Last Edit: Jul 29, 2013, 06:02 pm by niesteszeck Reason: 1
Hi, thanks for testing it, I'll check it why is failing when I have some time. Please be patient.

any info that you can add that can be useful will be great
Best regards


Jul 29, 2013, 08:02 pm Last Edit: Jul 29, 2013, 08:26 pm by niesteszeck Reason: 1
I supose you are using the example code and you are using the dht22Callback instead of the dht11Callback.

I preferred that you use example code to test it, so we can check the same code to find the bug


I am using the dht22callback :) I'll try with the example code today, if you want :)


You are telling that the addafruit lib is working, but it still can be some signal/wiring problem. For example the DHT11 module that I use is equipped whit the pullup resistor, are you using one similar? or just the naked sensor without nothing extra (I think for DHT11 is a ~5K pull up, and a 1K for the DH22, but please confirm on the datasheet). I'm just trying to think about all possibilities, 'cause I don't have a sensor to pass over the oscilloscope here and be sure that the signal is right, remember that some small noise in the line can trigger an interrupt, so maybe, really a remote possibility, is the line that triggers to much interrupts so the data get corrupted. If you have an oscilloscope, please, can you check the signal against the datasheet of the sensor.

I someone have any idea? Please I'll be glad to discuses any aproach


I am using both the pull up resistor and cap :) Like I say, no checksum errors or anything with the Adafruit lib, if the constructor is initialised in front of all the constructors (I removed parts of my code to find out which bit was causing the issues, and the two codes looked the same, except for the constructor order, and I thought 'hmmm, it couldn't be this, could it? that really makes no sense..'. But to rule out any memory issues I'll try it with the simple example.


Please share a sample code that present that problem and a one that "solve" (where the initialization happens) so I can test it with my dht11 sensor, maybe it happens to


Jul 30, 2013, 07:26 pm Last Edit: Jul 30, 2013, 07:34 pm by Dessimat0r Reason: 1

Code: [Select]

#define DHTTYPE DHT22
#define DHTPIN 2     // what pin we're connected to

#include <DHT.h>

int   dht_chk  = -10;
float dht_hum  = 0;
float dht_temp = 0;

unsigned long dht11lastcheck = 0;

void setup() {
Serial.println("DHTxx test!");

void loop() {
if ((millis() > 5000) && (dht11lastcheck + 5000 < millis())) {
dht_hum  = dht.readHumidity();
dht_temp = dht.readTemperature(false);
dht11lastcheck = millis();
dht_chk = 0;

I can't seem to find any working version this code, which is a little strange. Before I did it by re-arranging the constructors when I had some more in a bigger version of the code. The DHT pin is correct.


This DHTTester code works:

Code: [Select]
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor


void setup() {
  Serial.println("DHTxx test!");


void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: ");
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.println(" *C");

Any major differences?

Go Up