PDA

View Full Version : T3: FYI Adafruit DHT22/AM2302



el_supremo
01-22-2013, 08:13 PM
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:

DHT dht(DHTPIN, DHTTYPE);
to this:

DHT dht(DHTPIN, DHTTYPE, 27);

Pete

PaulStoffregen
01-25-2013, 12:28 AM
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 (http://arduino.cc/en/Reference/DelayMicroseconds) 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.

el_supremo
01-25-2013, 01:34 AM
3us doesn't work on T3.
Setting it to 4us works on Arduino Nano and also on the T3 at all 3 speeds.
but I haven't got any test equipment to really nail this down.

Pete

Constantin
07-29-2013, 01:50 AM
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.

ian
07-29-2013, 12:34 PM
I remarked in an erlier post (http://http://forum.pjrc.com/threads/23515-DHT11-amp-Teensy-2-0?highlight=DHT11) 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.

beijingcam
01-26-2014, 07:45 PM
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 DHT1333
Humidity: 891.40 % Temperature: 438.00 *C

Constantin
01-28-2014, 08:11 PM
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 (http://www.digikey.com/product-detail/en/HPP828E031/223-1144-1-ND/3945773), which works perfectly at 3.3V and which can use the sensiron library to be queried.

korczis
02-17-2015, 10:55 PM
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);
}