Go Down

Topic: Class for DHT11, DHT21 and DHT22 (temperature & humidity) (Read 77979 times) previous topic - next topic


Mar 06, 2015, 08:57 am Last Edit: Mar 06, 2015, 09:05 am by robtillaart
I see no code path that would generate 99.9% in a consistent way.

Can you post the exact output you get from the sketch?

please also try the 0.1.13 version attached to - http://forum.arduino.cc/index.php?topic=58531.0
DHT21 is interface compatible to DHT22.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Hi Rob,
Output attached. I'll report back when I've tried the other version you suggest


Output looks normal and states the communication is OK.

you might try this debug version of read() in the lib. It dumps the bytes that come from the sensor.
Code: [Select]

int8_t dht::read(uint8_t pin)
    int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS);

    Serial.println("dump: ");
    for (int i=0; i< 5; i++)
      Serial.print(bits[i], HEX);

    // these bits are always zero, masking them reduces errors.
    bits[0] &= 0x03;
    bits[2] &= 0x83;

    humidity = word(bits[0], bits[1]) * 0.1;
    temperature = word(bits[2] & 0x7F, bits[3]) * 0.1;
    if (bits[2] & 0x80)  // negative temperature
        temperature = -temperature;

    uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
    if (bits[4] != sum)
    return result;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Hi Rob,
Bits[0] and bits[1] are 3 and E7. they do not change when humidity is changed by breathing on the sensor. Temperature does change. I haven't tried version 13 yet


Code: [Select]

  float f = word(3, 0xE7) * 0.1;

output 99.9

As there is no checksum or other error the library works as intended.

You might need to recalibrate the sensors, or send them back?

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Thank you Rob I've already sent two back so might try a different sensor.


I'm trying to get the DHT22 sensor working on the DigiX with DHTlib. I've read that the recommended way for doing so was plugging the DHT22 on 3.3V as per http://forum.arduino.cc/index.php?topic=215336.0.

When I use the version of DHTlib from February last year, I can get the DHT22 to work. This is the git commit with ref 2f6075437687932dc5f191e8931e35d1fd78a88f (February 10 2014)

However, with the DHTlib from the master tree I'm seeing two problems:
* In the dht22_test example sketch, the name stat is taken by a function. So I changed the name to statDHT. Compiler error:
c:\program files (x86)\digistump_arduino\hardware\tools\g++_arm_none_eabi\arm-none-eabi\include\sys\stat.h:150:5: error: previous declaration of 'int stat(const char*, stat*)'
int _EXFUN(stat,( const char *__restrict __path, struct stat *__restrict __sbuf ));
* More importantly in the dht.cc file, the direct port access does not compile for the DigiX:
In file included from C:\Users\Floris\Documents\Arduino\libraries\DHTlib\dht.h:19:0,
                 from C:\Users\Floris\Documents\Arduino\libraries\DHTlib\dht.cpp:42:
C:\Users\Floris\Documents\Arduino\libraries\DHTlib\dht.cpp: In member function 'int8_t dht::_readSensor(uint8_t, uint8_t, uint8_t)':
C:\Users\Floris\Documents\Arduino\hardware\digistump\sam\cores\digix/Arduino.h:57:65: error: invalid conversion from 'Pio*' to 'uint8_t {aka unsigned char}' [-fpermissive]
 #define digitalPinToPort(P)        ( g_APinDescription[P].pPort )
C:\Users\Floris\Documents\Arduino\libraries\DHTlib\dht.cpp:121:20: note: in expansion of macro 'digitalPinToPort'
     uint8_t port = digitalPinToPort(pin);
C:\Users\Floris\Documents\Arduino\hardware\digistump\sam\cores\digix/Arduino.h:62:44: error: base operand of '->' is not a pointer
 #define portInputRegister(port)    ( &(port->PIO_PDSR) )
C:\Users\Floris\Documents\Arduino\libraries\DHTlib\dht.cpp:122:29: note: in expansion of macro 'portInputRegister'
     volatile uint8_t *PIR = portInputRegister(port);
Error compiling.
I've managed to get DHTlib to compile by changing the data type to the return types from DigiX:
diff --git a/libraries/DHTlib/dht.cpp b/libraries/DHTlib/dht.cpp
index a40e47c..9cfd101 100644
--- a/libraries/DHTlib/dht.cpp
+++ b/libraries/DHTlib/dht.cpp
@@ -118,8 +118,10 @@ int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBit
     // reduces footprint ~100 bytes => portability issue?
     // direct port read is about 3x faster
     uint8_t bit = digitalPinToBitMask(pin);
-    uint8_t port = digitalPinToPort(pin);
-    volatile uint8_t *PIR = portInputRegister(port);
+    //uint8_t port = digitalPinToPort(pin);
+       Pio* port = digitalPinToPort(pin);
+    //volatile uint8_t *PIR = portInputRegister(port);
+       volatile RoReg* PIR = portInputRegister(port);

     pinMode(pin, OUTPUT);
However, when I try to get a reading from my DHT22 sensor (modified dht22_test.ino), DHTlib is returning an 'Ack Low' error:
DHT22,    Ack Low error,   0.0,   0.0,   1061

Any suggestions? Could the code changes since February 2014 have introduced timing issues or is the direct port access not compatible with the DigiX (I'm not knowledgeable enough to access whether the above patch makes sense)?

I've put the changes to DHTlib (git diff) on pastebin @ http://pastebin.com/cEckj8es


Sorry for late reaction.

The latest portable version which should work as is on Digix is the 0.1.13.

The versions thereafter are experimental to support lower clock speeds on AVR processors, typically UNO and MEGA, and to some extend decrease footprint.

I have no Digix experience so I don't know if your modifications are correct or not.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131