DHT11 temperature and humidity sensor due library

I was wondering if anybody knows of a Due friendly library for the DHT11 temperature and humidity sensor or if someone might be able to help me figure out how to modify a uno library to work on the due.

where I found a uno tutorial:

I can import the
dht.cpp: https://docs.google.com/file/d/0B5Dr9BmFCKPES1d2bkd0OE1hOWs/edit?usp=sharing
dht.h: https://docs.google.com/file/d/0B5Dr9BmFCKPEY2tPYzUzRlJpR1k/edit?usp=sharing

but when I try to compile the program it complains about dht.cpp:

DHT\dht.cpp.o: In function `dht::read(unsigned char)':
C:\Users\Organix\Code\Arduino\Due\Test\libraries\DHT/dht.cpp:82: multiple definition of `dht::read(unsigned char)'
dht.cpp.o:C:\Users\Organix\AppData\Local\Temp\build9168811753292924831.tmp/dht.cpp:82: first defined here
DHT\dht.cpp.o: In function `dht::read11(unsigned char)':
C:\Users\Organix\Code\Arduino\Due\Test\libraries\DHT/dht.cpp:28: multiple definition of `dht::read11(unsigned char)'
dht.cpp.o:C:\Users\Organix\AppData\Local\Temp\build9168811753292924831.tmp/dht.cpp:28: first defined here
DHT\dht.cpp.o: In function `dht::read22(unsigned char)':
C:\Users\Organix\Code\Arduino\Due\Test\libraries\DHT/dht.cpp:49: multiple definition of `dht::read22(unsigned char)'
dht.cpp.o:C:\Users\Organix\AppData\Local\Temp\build9168811753292924831.tmp/dht.cpp:49: first defined here
collect2: ld returned 1 exit status

I loaded an run the sketch OK
Thit you place the DHT library on the place it should be.
Restarting the IDE could also help.

Thanks gharryh - my problem was I had the dht.cpp open in the sketch I was compiling :blush:
I opened a new sketch and imported the dht library and compiled fine...Sorry for the noobness XD

That version is not up to date and has some bugs in it. The latest version (with no known bugs) can be found here - Arduino Playground - DHTLib -

It would be great if you can confirm it working on the DUE.

Thanks,
Rob

Hi Rob,

Sorry to say but i do have problems with that one:
DHT11_TEST.ino: In function 'void loop()':
DHT11_TEST:78: error: 'DHTLIB_OK' was not declared in this scope
DHT11_TEST:81: error: 'DHTLIB_ERROR_CHECKSUM' was not declared in this scope
DHT11_TEST:84: error: 'DHTLIB_ERROR_TIMEOUT' was not declared in this scope

Using your test sketch.

Regards

Harry

HI Gharryh,

I have written 3 DHT libs over time,

The libs are not (100%) compatible.

From the messages I think you use the DHT11 library, and that the library is not installed correctly or you use an older version. NB the three #defines that cannot be resolved were added later. SO I think you need to upgrade the lib, the playground has the latest version.

Reinstalling the Lib solved the problem.
Maybe something went wrong whils copying from the website.
Is it correct that there aren no numbers after the decimal?

Is it correct that there aren no numbers after the decimal?

Technically speaking it does have a decimal part but it is always zero ..

Is this due a lack of programming or the Lib?

it is due to the capabilities of the sensor itself. It returns 5 bytes just like the DHT22 (same handshake) and 2 of the bytes (decimal part) are always zero for the DHT11

And because the sensor itself behaves that way, the lib does not support a decimal part for the DHT11, it would make no sense.

Hello,

I am trying to get the DHT22 sensor working on the Arduino Due.

When I try to compile it gives me these errors:

In file included from F:\program files (x86)\Arduino\hardware\arduino\sam\cores\arduino/Arduino.h:189:0,
                 from F:\program files (x86)\Arduino\libraries\DHT\dht.h:19,
                 from F:\program files (x86)\Arduino\libraries\DHT\dht.cpp:36:
F:\program files (x86)\Arduino\libraries\DHT\dht.cpp: In member function 'int dht::_readSensor(uint8_t, uint8_t)':
F:\program files (x86)\Arduino\hardware\arduino\sam\variants\arduino_due_x/variant.h:62:65: error: invalid conversion from 'Pio*' to 'uint8_t {aka unsigned char}' [-fpermissive]
 #define digitalPinToPort(P)        ( g_APinDescription[P].pPort )
                                                                 ^
F:\program files (x86)\Arduino\libraries\DHT\dht.cpp:107:20: note: in expansion of macro 'digitalPinToPort'
     uint8_t port = digitalPinToPort(pin);
                    ^
F:\program files (x86)\Arduino\hardware\arduino\sam\variants\arduino_due_x/variant.h:66:44: error: base operand of '->' is not a pointer
 #define portInputRegister(port)    ( &(port->PIO_PDSR) )
                                            ^
F:\program files (x86)\Arduino\libraries\DHT\dht.cpp:108:29: note: in expansion of macro 'portInputRegister'
     volatile uint8_t *PIR = portInputRegister(port);
                             ^
Error compiling.

I have the latest library from Github by Rob Tillaart:

// FILE: dht.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.17

// FILE: dht.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.17
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: Arduino Playground - HomePage

Can anyone please help? Thanks!

For the DUE I advice to use the library version 0.1.13.
The versions after the 0.1.13 explore optimizations...

attached on this post - http://forum.arduino.cc/index.php/topic,58531.0.html

Rob thank you for the quick reply,

I've been testing and trying to (unsuccessfully) get it to work.

I did manage to get some output, the first time it is polled it comes back with the correct temp and humidity but then gives checksum errors therafter:

DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.13

Type,	status,	Humidity (%),	Temperature (C)	Time (us)
DHT read(), 	OK,	31.7,	25.1,	4641
DHT read(), 	Checksum error,	3292.7,	-12.5,	4452
DHT read(), 	Checksum error,	3292.6,	-12.5,	4358
DHT read(), 	Checksum error,	3292.6,	-12.5,	4358
DHT read(), 	Checksum error,	3292.6,	-12.5,	4358
DHT read(), 	Checksum error,	3292.6,	-12.5,	4359
DHT read(), 	Checksum error,	3292.6,	-12.5,	4359
DHT read(), 	Checksum error,	3292.6,	-12.5,	4358
DHT read(), 	Checksum error,	3292.6,	-12.5,	4311
DHT read(), 	Checksum error,	3292.6,	-12.5,	4358
DHT read(), 	Checksum error,	3292.6,	-12.5,	4358
DHT read(), 	Checksum error,	3292.6,	-12.5,	4359

This is strange, your Tuning code is almost the same but produces more than 1 correct read:

DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.13

Type,	status,	Humidity (%),	Temperature (C)	Time (us)
DHT22, 	OK,	31.9,	25.0,	5068,	490
DHT22, 	OK,	32.4,	25.1,	4448,	480
DHT22, 	OK,	31.9,	25.0,	4726,	470
DHT22, 	OK,	31.9,	25.1,	4819,	460
DHT22, 	OK,	31.9,	25.1,	4823,	450
DHT22, 	OK,	31.9,	25.0,	4727,	440
DHT22, 	OK,	31.9,	25.1,	4821,	430
DHT22, 	OK,	31.9,	25.1,	4823,	420
DHT22, 	OK,	31.9,	25.1,	4821,	410
DHT22, 	OK,	31.9,	25.0,	4727,	400
DHT22, 	OK,	31.9,	25.1,	4823,	390
DHT22, 	OK,	31.9,	25.1,	4820,	380
DHT22, 	OK,	31.9,	25.1,	4820,	370
DHT22, 	OK,	31.9,	25.1,	4822,	360
DHT22, 	Time out error,	-999.0,	-999.0,	2840,	370
DHT22, 	OK,	31.9,	25.1,	4825,	360
DHT22, 	Time out error,	-999.0,	-999.0,	2843,	370
DHT22, 	OK,	31.9,	25.1,	4820,	360
DHT22, 	Time out error,	-999.0,	-999.0,	2845,	370
DHT22, 	OK,	31.9,	25.1,	4824,	360
DHT22, 	Time out error,	-999.0,	-999.0,	2840,	370
DHT22, 	OK,	31.9,	25.1,	4825,	360
DHT22, 	Time out error,	-999.0,	-999.0,	2843,	370
DHT22, 	OK,	31.9,	25.1,	4820,	360

Well I figured it out.

it's this line right here:

    Serial.print("DHT read(), \t");

read(), although a string literal that should not be executed, is being executed!

take read() out, and it works!

That is by far the most bizarre thing I've ever seen!

Something must be wrong with the Arduino IDE! It should not compile that as an executable statement should it?

I mean it should not run the read() function if it's inside of quotes in a string literal should it?

Or maybe I'm just noob and it should?

.... read(), although a string literal that should not be executed, is being executed!

what could happen is that the Serial.print("DHT read(), \t") uses an interrupt that corrupts the reading of the sensor.

Other cause might be a missing pull up resistor on the data line (4K7 or lower when lines are longer)

Other possible cause can be that the power is not stable, DUE is 3.3V right? did you check?

Finally, can you post the code you use?

I didn't take out the whole line though, I only took out the read() part.

For example: Serial.print("DHT, \t") instead of Serial.print("DHT read(), \t")
and it works with no errors. That is why I couldn't believe my eyes.

The 4.7kΩ pull up resistor is there between the data line and VCC.

Power is stable, it's 3.28 volts measured with a DMM. It's not a hardware problem because if I only take just read() out of the string literal it has no errors at all.

So then I started running a few experiments to narrow down the problem.

I changed all the read statements in the library to the word obtain() instead. Much to my surprise, nothing changed. Everything still works perfectly unless read() is in the string literal.

Some weird results:
If I change it to lead() it has checksum errors.
If I change it to re() no errors. 0 checksum errors, 0 timeouts. Run for about a minute.
But rea() and the errors begin. Same as before, first reading is good then all checksum errors thereafter.

That is strange and it seems to me like a compiler issue. This is beta after all, to program the Due I am using Arduino IDE 1.5.8 beta.

The code I am using is your example code from github along with your library. The only mod I made is I changed all the read() to obtain() for example read22() to obtain22() throughout all of the library, .cpp and .h but that had no effect anyway. It behaves exactly the same as with read22(). So I changed back to read22() throughout the library. So the library is back to the way it was with no modifications. The only other change in the code is I use pin 12 and not 5 or 6, but that doesn't matter. The code works if it's re() but not rea(). Strange.

Maybe it's an interrupt issue like you said, but then why would rea() cause interrupt problems but re() not cause any problems at all? And inside a string literal? To me it seems to be a bug in the compiler, but I could be wrong.

DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.13

Type,	status,	Humidity (%),	Temperature (C)	Time (us)
DHT re(), 	OK,	37.2,	22.5,	4623
DHT re(), 	OK,	37.5,	22.4,	4295
DHT re(), 	OK,	37.1,	22.4,	4247
DHT re(), 	OK,	37.1,	22.4,	4248
DHT re(), 	OK,	37.0,	22.4,	4248
DHT re(), 	OK,	37.0,	22.4,	4247
DHT re(), 	OK,	36.9,	22.4,	4201
DHT re(), 	OK,	36.9,	22.4,	4201
DHT re(), 	OK,	36.9,	22.4,	4200
DHT re(), 	OK,	37.0,	22.5,	4248
DHT re(), 	OK,	37.0,	22.5,	4248
DHT re(), 	OK,	37.0,	22.5,	4247
DHT re(), 	OK,	37.0,	22.5,	4248
DHT re(), 	OK,	36.9,	22.4,	4200
DHT re(), 	OK,	37.0,	22.5,	4247
DHT re(), 	OK,	37.0,	22.5,	4248
DHT re(), 	OK,	37.0,	22.5,	4247
DHT re(), 	OK,	37.1,	22.5,	4341
DHT re(), 	OK,	37.1,	22.5,	4342
DHT re(), 	OK,	37.0,	22.5,	4248
DHT re(), 	OK,	37.1,	22.5,	4341
DHT re(), 	OK,	37.1,	22.5,	4342
DHT re(), 	OK,	37.2,	22.5,	4295
DHT re(), 	OK,	37.2,	22.4,	4247
DHT re(), 	OK,	37.3,	22.5,	4389
DHT re(), 	OK,	37.4,	22.5,	4295
DHT re(), 	OK,	37.4,	22.5,	4294
DHT re(), 	OK,	37.4,	22.5,	4295
DHT re(), 	OK,	37.4,	22.5,	4295
DHT re(), 	OK,	37.5,	22.5,	4388
DHT re(), 	OK,	37.5,	22.5,	4389
DHT re(), 	OK,	37.6,	22.5,	4295
DHT re(), 	OK,	37.6,	22.5,	4294
DHT re(), 	OK,	37.6,	22.5,	4295
DHT re(), 	OK,	37.5,	22.5,	4389
DHT re(), 	OK,	37.5,	22.5,	4388

The problem occurs with rea() as a string literal:

DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.13

Type,	status,	Humidity (%),	Temperature (C)	Time (us)
DHT rea(), 	OK,	36.9,	22.5,	4539
DHT rea(), 	Checksum error,	3295.4,	11.2,	4215
DHT rea(), 	Checksum error,	3295.2,	11.2,	4122
DHT rea(), 	Checksum error,	3295.2,	11.2,	4123
DHT rea(), 	Checksum error,	3295.2,	11.2,	4122
DHT rea(), 	Checksum error,	3295.2,	11.3,	4122
DHT rea(), 	Checksum error,	3295.2,	11.2,	4122
DHT rea(), 	Checksum error,	3295.2,	11.3,	4123
DHT rea(), 	Checksum error,	3295.2,	11.2,	4123
DHT rea(), 	Checksum error,	3295.2,	11.2,	4122
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4168
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4168
DHT rea(), 	Checksum error,	3295.2,	-11.3,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.3,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4168
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.3,	4169
DHT rea(), 	Checksum error,	3295.2,	-11.2,	4168
DHT rea(), 	Checksum error,	3295.2,	11.2,	4122
DHT rea(), 	Checksum error,	3295.2,	11.3,	4123
DHT rea(), 	Checksum error,	3295.2,	11.3,	4123
DHT rea(), 	Checksum error,	3295.2,	11.3,	4123
DHT rea(), 	Checksum error,	3295.2,	11.2,	4123
DHT rea(), 	Checksum error,	3295.2,	11.2,	4122
DHT rea(), 	Checksum error,	3295.2,	11.3,	4122
DHT rea(), 	Checksum error,	3295.1,	-11.2,	4215
//
//    FILE: dht_read_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: DHT library test sketch for DHTlib read function && Arduino
//     URL:
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT_PIN 12

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

void loop()
{
    // READ DATA
    Serial.print("DHT rea(), \t");     // change rea() to re() and no errors!!! Strange.

    uint32_t start = micros();
    int chk = DHT.read22(DHT_PIN);
    uint32_t stop = micros();

    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.print(DHT.temperature, 1);
    Serial.print(",\t");
    Serial.print(stop - start);
    Serial.println();

    delay(2000);
}
//
// END OF FILE
//

Thank you for your work on this library, it is very much appreciated the hard work you put into this for the community and it has turned out to be very interesting and bizarre behavior. I still think it might be a compiler bug, but I am not sure. I can do any experiments you want I even have an oscilloscope if need be.

Some weird results:
If I change it to lead() it has checksum errors.
If I change it to re() no errors. 0 checksum errors, 0 timeouts. Run for about a minute.
But rea() and the errors begin. Same as before, first reading is good then all checksum errors thereafter.

really weird, never seen.

please replace "read()" with "xxxxxx" same amount of chars?
then it is definitely not a function call.

does it fail?

Yes, it fails with same amount of characters. You are right it's not a function call.

DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.13

Type,	status,	Humidity (%),	Temperature (C)	Time (us)
DHT xxxx, 	OK,	33.0,	25.1,	4671
DHT xxxx, 	OK,	33.4,	25.0,	4344
DHT xxxx, 	OK,	33.0,	25.1,	4344
DHT xxxx, 	OK,	33.0,	25.1,	4344
DHT xxxx, 	OK,	33.0,	25.1,	4344
DHT xxxx, 	OK,	33.0,	25.1,	4344
DHT xxxx, 	OK,	33.0,	25.1,	4343
DHT xxxx, 	OK,	33.1,	25.1,	4438
DHT xxxx, 	OK,	33.1,	25.0,	4344
DHT xxxx, 	OK,	33.1,	25.1,	4438
DHT xxxx, 	OK,	33.0,	25.0,	4297
DHT xxxx, 	OK,	33.0,	25.1,	4344
DHT xxxx, 	OK,	33.0,	25.1,	4343
DHT xxxx, 	OK,	33.0,	25.1,	4344
DHT TEST PROGRAM 
LIBRARY VERSION: 0.1.13

Type,	status,	Humidity (%),	Temperature (C)	Time (us)
DHT xxxxx, 	OK,	33.3,	25.0,	4537
DHT xxxxx, 	Checksum error,	3293.6,	12.5,	4170
DHT xxxxx, 	Checksum error,	3293.4,	-12.5,	4218
DHT xxxxx, 	Checksum error,	3293.4,	-12.5,	4217
DHT xxxxx, 	Checksum error,	3293.4,	12.5,	4218
DHT xxxxx, 	Checksum error,	3293.4,	12.5,	4217
DHT xxxxx, 	Checksum error,	3293.4,	12.5,	4218
DHT xxxxx, 	Checksum error,	3293.4,	12.5,	4218
DHT xxxxx, 	Checksum error,	3293.4,	12.5,	4217
DHT xxxxx, 	Checksum error,	3293.4,	12.5,	4218
DHT xxxxx, 	Checksum error,	3293.4,	12.5,	4218

So 4 characters and everything is ok, but 5 or 6 characters, and checksum errors after the 1st ok read.

OK that rules out one part, I am still waiting for a new dht11 so I can't reproduce right now, sorry...

That is strange and it seems to me like a compiler issue. This is beta after all, to program the Due I am using Arduino IDE 1.5.8 beta.

Still it can be a compiler issue, have you tried another compiler e.g 1.0.6 ?