T3: FYI Adafruit DHT22/AM2302

el_supremo

Well-known member
The library code supplied for the DHT22 (https://github.com/adafruit/DHT-sensor-library) works when run on a 328 based chip (Arduino Nano etc.) but it doesn't work on the Teensy 3.
When instantiating the DHT22 object, the example code sets the digital pin being used and the type of device. But there is a third argument which if unspecified defaults to 6. This number is used to differentiate between the length of a zero and a one when reading data from the DHT22 and it is much too short for a Teensy3. I've played around with this value and determined that, at all three cpu speeds (24, 48, 96MHz), any value in the range from 13 to 40 works.
So I'd recommend that you use something in the middle, such as 27, and change the example code from this:
Code:
DHT dht(DHTPIN, DHTTYPE);
to this:
Code:
DHT dht(DHTPIN, DHTTYPE, 27);

Pete
 
I just looked at that code. I believe the problem may be related to their use of "delayMicroseconds(1)" on line 120.

Arduino's documentation says:

Caveats and Known Issues

This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times.

Teensy 3.0 has a very accurate delayMicroseconds() from 1 us to an hour, but using 1 us on Arduino probably delays about 3 us in reality.

Any chance you could try editing Adafruit's code to use 3 or 4 us and see if it performs the same on the 2 different boards? If so, maybe we should ask them to update this code?

I could also buy a DHT22 and investigate. Right now, I simply don't have this hardware for testing.
 
3us doesn't work on T3.
Setting it to 4us works on Arduino Nano and also on the T3 at all 3 speeds.
[edit] but I haven't got any test equipment to really nail this down.[/edit]

Pete
 
3us doesn't work on T3.
Setting it to 4us works on Arduino Nano and also on the T3 at all 3 speeds.
Pete

Dear Pete and Paul,

Many thanks for looking into this. My DHT22 works fine because of your work! As a FYI, I am using a 1k pullup and the standard instantiation (DHT dht(DHTPIN, DHTTYPE, 6);) though the microsecond delay has to be adjusted as you noted in the CPP file. Many thanks!

Cheers! Constantin

PS: Any downside to using a stronger pullup than the 10K used by Ladyada's example? The reason I ask is that I've used my DHT22's with 1k pullups ever since I saw them in a diagram somewhere else. I'd be just as happy to switch to 10K seeing that all my other pullups are 10K.
 
I remarked in an erlier post that the Adafruit library failed when using a DHT11 and a Teensy 2.
Like Paul, I was suspicious of the 1us delay in the library. My 'solution' was to run the Teensy 2 at 8MHz instead of 16MHz and it worked fine.
It would be nice to see this issue nailed properly.
BTW I've used the DHT11 with & without a pullup & don't see any obvious difference either way.
 
A bit new to this but followed your instructions on changing the code in CCP file to 4 and the pin use is 3 but output is spurious at best

Read failFailed to read from DHT
Humidity: 81.50 % Temperature: 39.20 *C
Humidity: 81.50 % Temperature: 39.20 *C
Humidity: 81.50 % Temperature: 39.20 *C
Read failFailed to read from DHT
Humidity: 904.30 % Temperature: 39.40 *C
Humidity: 904.30 % Temperature: 39.40 *C
Humidity: 904.30 % Temperature: 39.40 *C
Read failFailed to read from DHT
Humidity: 91.50 % Temperature: 438.20 *C
Humidity: 91.50 % Temperature: 438.20 *C
Humidity: 91.50 % Temperature: 438.20 *C
Read failFailed to read from DHT
Humidity: 915.00 % Temperature: -246.00 *C
Humidity: 915.00 % Temperature: -246.00 *C
Humidity: 915.00 % Temperature: -246.00 *C
Read failFailed to read from DHT
Humidity: 95.30 % Temperature: 27.10 *C
Humidity: 95.30 % Temperature: 27.10 *C
Humidity: 95.30 % Temperature: 27.10 *C
Read failFailed to read from DHT
Humidity: 195.40 % Temperature: 26.90 *C
Humidity: 195.40 % Temperature: 26.90 *C
Humidity: 195.40 % Temperature: 26.90 *C
Read failFailed to read from DHT
Humidity: 87.60 % Temperature: 438.10 *C
Humidity: 87.60 % Temperature: 438.10 *C
Humidity: 87.60 % Temperature: 438.10 *C
Read failFailed to read from DHT
Humidity: 904.10 % Temperature: 438.00 *C
Humidity: 904.10 % Temperature: 438.00 *C
Humidity: 904.10 % Temperature: 438.00 *C
Read failFailed to read from DHT
Humidity: 82.20 % Temperature: 438.00 *C
Humidity: 82.20 % Temperature: 438.00 *C
Humidity: 82.20 % Temperature: 438.00 *C
Read failFailed to read from DHT
Humidity: 894.60 % Temperature: 26.80 *C
Humidity: 894.60 % Temperature: 26.80 *C
Humidity: 894.60 % Temperature: 26.80 *C
Read failFailed to read from DHT photo.jpg
Humidity: 891.40 % Temperature: 438.00 *C
 
I have had issues too, depending on which incarnation of the sensor I was dealing with (dHT22, AM2302, etc.) In the end, I gave up since the issues seem to be related to running the sensor at 3.3V vs. 5V. I'm using the HTU21 instead, which works perfectly at 3.3V and which can use the sensiron library to be queried.
 
Hi There,
I solved problems mentioned above "tweaking" third parameter ...

See https://github.com/korczis/iCave/blob/master/app/dht.cpp ..

// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:

DHT dht(DHTPIN, DHTTYPE, 25);

float dhtHumidity = 0;
float dhtTemperature = 0;
float dhtHeatIndex = 0;

void setupDht() {
dht.begin();
}

void loopDht() {
// Reading temperature or humidity takes about 250 milliseconds!

// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
dhtHumidity = dht.readHumidity();

// Read temperature as Celsius
dhtTemperature = dht.readTemperature();

// Read temperature as Fahrenheit
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(dhtHumidity)) {
dhtHumidity = 0;
}

if (isnan(dhtTemperature)) {
dhtTemperature = 0;
}

dhtHeatIndex = (dht.computeHeatIndex(f, dhtHumidity) - 32) * (5.0f / 9.0f);
}
 
I'm having problems with the DHT22 for temperatures below 0C, it returns temperatures around -3272C, for any setting of the third parameter, from 6 to 80,

For temperatures above 0C it works fine with any setting in that range.
 
I'm having problems with the DHT22 for temperatures below 0C, it returns temperatures around -3272C, for any setting of the third parameter, from 6 to 80,

For temperatures above 0C it works fine with any setting in that range.

Maybe related to some current issues within this library:
https://github.com/adafruit/DHT-sensor-library/issues/177
https://github.com/adafruit/DHT-sensor-library/issues/182
https://github.com/adafruit/DHT-sensor-library/issues/181

And maybe a few others

You might look at the Pull request to that library: https://github.com/adafruit/DHT-sensor-library/pull/178
And see if the changes in it fixes it for you and let them (Adfafruit and the person who issued the PR) know if it does
 
Back
Top