Project

General

Profile

Strange numbers from a DHT22

Added by mikeacar almost 6 years ago

Hi!

I have a couple of DHT22 temperature/humidity sensors. With the dht_demo example, I get some pretty strange numbers out:

> temperature = 7
> humidity = 25

But using the DHT22 from Virtuabotix I get plausible results. It’s consistent with two different sensors.

Any ideas?


Replies (10)

RE: Strange numbers from a DHT22 - Added by JohnO almost 6 years ago

Can you post the actual sketch that you compiled as an attachment?

Are you also known as Pierre Baillet - as in a recent Github report?

RE: Strange numbers from a DHT22 - Added by JohnO almost 6 years ago

*I tried to reply to http://jeelabs.net/boards/7/topics/1469 but the forum won’t let me write anything (username: octplane).
*

You were able to post 18 hours ago, can you go through the signin process again?
http://jeelabs.net/login

I don’t have any admin access on the forum.

RE: Strange numbers from a DHT22 - Added by mikeacar almost 6 years ago

Sure, though it’s just dht_demo:

/// @dir dht_demo
/// Demo readout of the DHT11 or DHT22 temperature/humidity sensors.
/// @see http://jeelabs.org/2012/04/14/reading-out-dhtxx-sensors/
// 2012-04-02  http://opensource.org/licenses/mit-license.php

#include 

DHTxx dht (7); // connected to DIO4

void setup () {
  Serial.begin(57600);
  Serial.println("\n[dht_demo]");
}

void loop () {
  int t, h;
  if (dht.reading(t, h)) {
    Serial.print("temperature = ");
    Serial.println(t);
    Serial.print("humidity = ");
    Serial.println(h);
    Serial.println();
  }
  delay(3000);
}

And no, I’m not Pierre Baillet.

RE: Strange numbers from a DHT22 - Added by mjl almost 6 years ago

mikeacar wrote:
> Hi!
>
> I have a couple of DHT22 temperature/humidity sensors. With the dht_demo example, I get some pretty strange numbers out:
>
> > temperature = 7
> > humidity = 25
>
> But using the DHT22 from Virtuabotix I get plausible results. It’s consistent with two different sensors.
>
> Any ideas?

You’re not alone, I see the same funky values using Jeelib’s DHT22, I switched to Adafruit’s DHT22 lib for the time being as that gives results consistent with the roomboards and TMP36s I have.

(I’m not Pierre either :-))

RE: Strange numbers from a DHT22 - Added by octplane almost 6 years ago

Hi,

I am Pierre (!), and github and danward79 (and the old forum) have a fix and part of the solution.

Reported issue: https://github.com/jcw/jeelib/issues/47

Fix from danward79: http://forum.jeelabs.net/node/1487.html

Cheers,
Pierre.

RE: Strange numbers from a DHT22 - Added by mjl almost 6 years ago

Thanks for the pointer! Dan’s fix works fine for me. The following patch makes the DHT type selectable. I don’t have a DHT11 to test with, but the results when selecting a DHT11 while a DHT22 is present match the old wrong output, and looks fine when selecting DHT22.

$ diff -u ~/Downloads/jeelib-master/Ports.h Ports.h--- /Users/ml/Downloads/jeelib-master/Ports.h    2013-02-21 11:49:46.000000000 +0100
+++ Ports.h 2013-03-17 23:38:10.000000000 +0100
@@ -629,11 +629,14 @@
   long reading ();
 };

+enum dhttype { DHT11, DHT22 };
+
 /// Interface for the DHT11 and DHT22 sensors, does not use floating point
 class DHTxx {
   byte pin;
+  dhttype type;
 public:
-  DHTxx (byte pinNum);
+  DHTxx (byte pinNum, dhttype dhtType);
   /// Results are returned in tenths of a degree and percent, respectively
   bool reading (int& temp, int &humi);
 };
$ diff -u ~/Downloads/jeelib-master/Ports.cpp Ports.cpp--- /Users/ml/Downloads/jeelib-master/Ports.cpp  2013-02-21 11:49:46.000000000 +0100
+++ Ports.cpp   2013-03-17 23:37:34.000000000 +0100
@@ -945,8 +945,9 @@
   return raw;
 }

-DHTxx::DHTxx (byte pinNum) : pin (pinNum) {
+DHTxx::DHTxx (byte pinNum, dhttype dhtType) : pin (pinNum) {
   digitalWrite(pin, HIGH);
+  type = dhtType;
 }

 bool DHTxx::reading (int& temp, int &humi) {
@@ -1014,11 +1015,11 @@
     return false;

   word h = (data[1] << 8) | data[2];
-  humi = ((h >> 3) * 5) >> 4;     // careful with overflow
+  humi = type == DHT11 ? ((h >> 3) * 5) >> 4 : h;

   int tmul = data[3] & 0x80 ? -5 : 5;
   word t = ((data[3] & 0x7F) << 8) | data[4];
-  temp = ((t >> 3) * tmul) >> 4;  // careful with overflow
+  temp = type == DHT11 ? ((t >> 3) * tmul) >> 4 : t;

   return true;
 }
$ diff -u ~/Downloads/jeelib-master/examples/Ports/dht_demo/dht_demo.ino examples/Ports/dht_demo/dht_demo.ino 
--- /Users/ml/Downloads/jeelib-master/examples/Ports/dht_demo/dht_demo.ino  2013-02-21 11:49:46.000000000 +0100
+++ examples/Ports/dht_demo/dht_demo.ino    2013-03-17 23:27:13.000000000 +0100
@@ -5,7 +5,7 @@

 #include 

-DHTxx dht (5); // connected to DIO2
+DHTxx dht (5, DHT22); // connected to DIO2

 void setup () {
   Serial.begin(57600);

RE: Strange numbers from a DHT22 - Added by mikeacar almost 6 years ago

Thanks, I’ll give this a try and report back.

RE: Strange numbers from a DHT22 - Added by jcw almost 6 years ago

Thanks - when using th DHT22 in this way, are the results still reported in tenths of °C and tenths of %RH ?

RE: Strange numbers from a DHT22 - Added by mjl almost 6 years ago

Yes, they are still reported in tenths:

[dht_demo]
temperature = 220
humidity = 415

temperature = 220
humidity = 420

temperature = 220
humidity = 415

RE: Strange numbers from a DHT22 - Added by jcw almost 6 years ago

I’ve checked in a fix - it’s equivalent, but slightly different from what was proposed above: there’s a new third arg when calling reading(), set it to true for DHT21 and DHT22 sensors.

    (1-10/10)