Go Down

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

robtillaart

#165
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)

silvaphoenix

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

robtillaart

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)
{
    // READ VALUES
    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);
      Serial.println("\t");
    }
    Serial.println();


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

    // CONVERT AND STORE
    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;
    }

    // TEST CHECKSUM
    uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
    if (bits[4] != sum)
    {
        return DHTLIB_ERROR_CHECKSUM;
    }
    return result;
}
Rob Tillaart

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

silvaphoenix

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
Stephen

robtillaart

Code: [Select]

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


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)

silvaphoenix

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

fvdnabee

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

     // REQUEST SAMPLE
     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

robtillaart

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)

roop

Hi,

I got a strange issue with humidity readings and wonder if my DHT-22 is faulty or not.
I use an UNO and v1.1.20 of the lib.

The readings jump at ~39.9 humidity to ~61.2.
I don't know the exact values (the changes happend really fast).

In bytes (what i've seen):

bits[0] is 1 and bits[1] is 143

then jumps to

bits[0] is 2 and bits[1] is 99


I really can't tell if the sensor is the problem here
or some calculations are off.

Temperature seems normal so far.

Thanks a lot and great work so far!

robtillaart

most probably the sensor is somehow faulty, otherwise the library would give a CRC error (OK chance one in 255 the Checksum is the same.

Is it repeatable?
Does the temperature show meaningful numbers?


Have you tried another version of the library?
The first post of this thread has 0.1.13 attached which is sort of reference version (without all AVR specific optimizations) Can you please give that a try?

Trying with the another lib e.g. Adafruit lib is also a way to test if the sensor is broken or the lib.

Rob Tillaart

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

roop

Yeah its totally repeatable.
I'll make another test and post the values.

Temperature is arount 20 - 21 °C (room temp).
Should be fine.

I've just tried the Adafruit lib. Same values there.

Will try the 0.1.13 next.

Thanks so far.


robtillaart

Quote
I've just tried the Adafruit lib. Same values there.
Strong indication its the sensor...
Rob Tillaart

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

roop

Yes, looks like the sensor is faulty =(.

Here are some values (v.0.1.13):


DHT TEST PROGRAM
LIBRARY VERSION: 0.1.13

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

DHT22, OK, 62.1, 21.5, 5224
DHT22, OK, 62.3, 21.5, 5220
DHT22, OK, 62.3, 21.5, 5224
DHT22, OK, 62.0, 21.4, 5080
DHT22, OK, 62.1, 21.5, 5224
DHT22, OK, 62.1, 21.4, 5184
DHT22, OK, 62.0, 21.4, 5076
DHT22, OK, 62.1, 21.5, 5220
DHT22, OK, 61.9, 21.4, 5176
DHT22, OK, 61.8, 21.4, 5076
DHT22, OK, 61.8, 21.4, 5076
DHT22, OK, 61.8, 21.4, 5076
DHT22, OK, 61.5, 21.4, 5324
DHT22, OK, 61.2, 21.4, 5128
DHT22, OK, 61.4, 21.4, 5224
DHT22, OK, 61.4, 21.4, 5220
DHT22, OK, 61.5, 21.4, 5344
DHT22, OK, 61.6, 21.4, 4984
DHT22, OK, 61.7, 21.4, 5080
DHT22, OK, 61.8, 21.4, 5096
DHT22, OK, 61.4, 21.3, 5224
DHT22, OK, 61.3, 21.3, 5176
DHT22, OK, 61.5, 21.4, 5320
DHT22, OK, 61.6, 21.3, 5224
DHT22, OK, 39.8, 21.3, 5128
DHT22, OK, 38.9, 21.3, 5176
DHT22, OK, 38.2, 21.3, 5224
DHT22, OK, 37.8, 21.3, 5128
DHT22, OK, 37.7, 21.2, 5176
DHT22, OK, 37.9, 21.2, 5128
DHT22, OK, 38.2, 21.2, 5220
DHT22, OK, 38.7, 21.1, 5196
DHT22, OK, 38.7, 21.1, 5176
DHT22, OK, 38.9, 21.1, 5124
DHT22, OK, 39.2, 21.1, 5076
DHT22, OK, 39.6, 21.1, 5032
DHT22, OK, 61.2, 21.1, 5124
DHT22, OK, 61.5, 21.1, 5224
DHT22, OK, 61.7, 21.1, 5248
DHT22, OK, 62.0, 21.0, 4984
DHT22, OK, 62.0, 21.0, 4980
DHT22, OK, 61.5, 21.0, 5220
DHT22, OK, 61.3, 21.0, 5128
DHT22, OK, 39.9, 21.0, 5124
DHT22, OK, 39.6, 21.0, 5196
DHT22, OK, 38.8, 20.9, 5028
DHT22, OK, 38.9, 20.9, 5132
DHT22, OK, 39.4, 20.9, 5076
DHT22, OK, 39.7, 20.9, 5220
DHT22, OK, 39.7, 20.9, 5224
DHT22, OK, 39.3, 20.9, 5124
DHT22, OK, 39.2, 20.9, 5060
DHT22, OK, 39.0, 20.8, 5084
DHT22, OK, 38.9, 20.8, 5028
DHT22, OK, 39.4, 20.9, 5076
DHT22, OK, 61.1, 20.9, 5124
DHT22, OK, 61.4, 20.9, 5128
DHT22, OK, 61.6, 20.9, 5128
DHT22, OK, 61.9, 20.9, 5224
DHT22, OK, 62.1, 20.9, 5032
DHT22, OK, 62.3, 20.9, 5152
DHT22, OK, 62.4, 20.9, 5028


I hope a new one will work.

Thanks again.

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!

Arduino
via Egeo 16
Torino, 10131
Italy