Pages: [1]   Go Down
Author Topic: problem with DHT11 returning 21 bits instead of 41 ...  (Read 1711 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all !

I'm writing here because i'm facing a problem that i can't bypass ...

Let me explain :
For those who managed to get the DHT11 (Temperature & Humidity digital sensor) working with the Arduino MEGA2560, let me tell you : GREAT, YOU'RE A BOSS !

My problem :
I make a capture with the "Pulsein()" command to get the duration of the HIGH states.
Based on this, i should be able to gather 41 bits corresponding to :
[1 bit of DHT11 telling communication is about to start] + [8 bits : Integral RH data] + [8 bits : Decimal RH data] + [8 bits : Integral TEMPERATURE data] + [8 bits : Decimal TEMPERATURE data] + [8 bits : Checksum]
The problem is that what i gather looks like this :
81,24,24,24,71,24,23,24,25,24,71,24,23,23,24,24,25,24,70,24,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
86,23,23,24,70,23,23,24,25,24,71,24,24,24,24,24,25,23,70,24,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,24,24,24,70,24,24,24,25,24,70,24,24,23,23,24,25,24,70,23,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
86,23,23,24,70,18,23,24,25,24,71,24,24,24,24,24,25,24,71,24,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,23,23,70,24,24,24,25,24,71,23,23,24,24,24,25,24,71,23,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,24,24,71,23,23,18,24,24,71,23,23,20,24,24,25,23,70,24,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,24,23,23,70,24,24,24,25,24,71,24,24,24,24,24,25,24,71,24,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,23,23,70,23,23,24,25,18,70,24,24,24,24,23,24,23,71,24,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,24,24,71,24,24,24,25,24,71,23,23,24,24,24,25,24,70,23,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,24,24,24,71,23,23,24,25,24,71,24,24,23,24,24,25,22,71,24,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,24,24,23,71,23,23,24,25,24,71,24,24,24,24,24,25,24,70,23,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
86,18,24,24,71,23,23,24,25,24,71,24,24,23,23,24,25,24,70,23,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
86,23,23,24,70,24,24,24,25,24,71,24,24,24,24,24,25,23,71,23,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
86,23,23,24,70,24,24,24,25,24,71,24,24,24,23,24,25,24,70,23,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
86,24,23,23,70,24,24,24,25,24,71,24,24,24,24,24,25,24,71,24,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,24,24,69,24,24,24,25,24,70,24,24,24,24,24,25,24,71,24,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,23,24,70,24,23,24,25,24,71,24,24,24,24,23,24,23,71,23,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,23,24,70,24,24,24,25,24,71,24,18,24,24,24,25,24,64,24,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,23,24,65,24,24,24,25,24,70,24,24,24,24,24,25,24,71,24,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516
87,23,23,24,71,23,23,24,25,24,71,23,23,24,20,24,25,24,71,24,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,516

You can have access to the datasheet here : http://www.micro4you.com/files/sensor/DHT11.pdf

And my code is here based on the code of a greek man :

void setup(){
  Serial.begin(9600);
}

void loop()
{
    int pin = 2;
    int highs[42];
    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW);
    delay(10);
    digitalWrite(pin, HIGH);
    delayMicroseconds(30);
    pinMode(pin, INPUT);
    for (int i=0; i<42; i++) {
        highs = pulseIn(pin, HIGH);
    }


Thank you for your precious help !
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Please use the code button (#) to tag code

Code:
void loop()
{
    int pin = 2;
    int highs[42];
    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW);

    delay(10);  <<<<< should be at least 18 ms !!!! See figure 3 page 6

    digitalWrite(pin, HIGH);

    delayMicroseconds(30);  <<< should be 20-40 msec, OK I guess

    pinMode(pin, INPUT);
    for (int i=0; i<41; i++)   [color=red] // there are 41 pulses 0..40   not 42 ! [/color]
    {
        highs[i] = pulseIn(pin, HIGH);
    }
}

That said, you correctly detect the 80 mu pulse. and the other pulses

you have filled the array highs[];  now you need code to convert this to  bytes, would look something like this partial code
Code:
byte RHI;
byte RHD;
byte TI;
byte TD;
byte CRC;
for (int i=1; i< 9; i++)
{
  RHI = RHI * 2;
  if (highs[i] > 50)  RHI = RHI + 1;  // add a one bit;  in short RHI++;
}
for (int i=9; i< 17; i++)
{
  RHD = RHD * 2;
  if (highs[i] > 50)  RHD = RHD + 1; 
}
for (int i=17; i< 25; i++)
{
  TI= TI* 2;
  if (highs[i] > 50)  TI = TI + 1; 
}
for (int i=25; i< 33; i++)
{
  TD= TD * 2;
  if (highs[i] > 50)  TD= TD+ 1; 
}
for (int i=33; i< 41; i++)
{
  CRC= CRC * 2;
  if (highs[i] > 50)  CRC= CRC + 1; 
}
Serial.println(RHI, DEC);
Serial.println(RHD, DEC);
Serial.println(TI, DEC);
Serial.println(TD, DEC);
Serial.println(CRC, DEC);



Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your answer !

In fact, i tweaked the values because i was thining that the DHT11 was sending the values too early to the arduino.
But in fact this is not the case because the arduino receives the first pulsation of ~80µS and then, follows only half of the message : 20 bits instead of 40 ...

I really don't know why this is happening, perhaps the "PulseIn" has a bug ?



Thank you by the way for your fast answer and your interest in my problem !
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The fact it gets only half the pulses implies that every other one is skipped by PulseIn();  Looking at the code it seems just to do this.

See - C:\Program Files (x86)\arduino-0022\hardware\arduino\cores\arduino\wiring_pulse.c

Lets replace pulseIn() with some simple code .. It will change the numbers but could you test it?
Rob

Code:
void loop()
{
    int pin = 2;
    int highs[42];
    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW);
    delay(10);  
    digitalWrite(pin, HIGH);
    delayMicroseconds(30);  
    pinMode(pin, INPUT);
    for (int i=0; i<41; i++)
    {
        while(digitalRead(pin) == LOW);  // wait until HIGH
        unsigned long t = micros();
        while(digitalRead(pin) == HIGH); // count HIGH
        highs[i] = (int) micros() - t;
    }
}
« Last Edit: March 25, 2011, 03:22:42 am by robtillaart » Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Something strange happened when i used your code for replacing the PulseIn() command ...
I got these results :
Code:
112,116,112,120,112,120,112,120,112,120,112,120,112,120,116,112,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,120,516
112,112,120,112,120,112,120,112,120,112,120,112,120,112,120,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,516
120,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,116,120,112,120,112,120,112,120,112,120,112,120,112,124,112,120,112,120,516
112,120,116,112,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,120,112,112,120,112,120,112,120,112,120,112,120,112,516
112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,116,120,112,120,516
112,112,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
120,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
112,112,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
120,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,516
112,112,120,112,120,112,120,112,120,112,120,112,124,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,516
112,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,112,120,120,112,516


Surely, i couldn't use them because they was very inacurate (max value : 124 ; min value : 112) ...
I then realised that in your code the command "analogRead" was used, and i changed it to "digitalRead" which was more appropriate, and then TAAADAAA !!!! miracle !!!!

I could then obtain the total message of 41 bits which looked like this :
Code:
88,24,28,72,20,20,68,24,28,24,28,28,24,28,24,24,28,24,20,20,72,28,72,72,72,28,20,24,20,24,24,24,24,28,28,72,72,72,24,76,72
92,24,20,72,28,28,72,20,28,24,24,28,24,20,24,28,24,28,24,20,72,28,72,72,72,24,28,24,24,28,24,28,28,20,28,72,72,68,20,72,72
92,24,24,72,24,24,68,24,24,28,24,24,28,24,20,20,24,28,24,24,76,24,72,72,72,20,24,24,28,28,24,28,28,24,24,72,72,72,20,72,72
92,24,24,76,24,28,72,28,24,28,24,24,28,24,28,24,28,20,20,20,72,24,72,68,68,24,24,28,24,24,28,24,24,28,24,72,72,68,28,72,72
92,28,24,72,24,28,72,24,28,28,24,24,28,24,24,24,24,28,28,24,72,24,72,68,68,20,24,20,24,28,24,28,24,28,24,72,72,68,20,72,68
92,24,24,72,24,16,72,28,24,28,24,24,28,24,28,20,24,32,24,20,72,24,72,72,72,20,24,20,20,24,20,20,28,24,28,72,72,72,24,68,68
88,24,24,68,24,28,72,24,24,28,24,28,24,28,24,28,28,20,24,20,68,24,72,72,72,24,28,28,20,24,20,24,28,24,28,68,68,72,24,72,68
92,24,28,72,24,20,72,24,28,28,24,28,24,24,28,24,24,28,24,24,72,28,72,72,72,24,20,20,24,24,20,24,28,24,28,72,72,72,24,72,68
88,28,28,72,28,24,68,24,28,24,28,28,24,28,20,28,24,24,20,24,72,28,72,72,72,20,28,24,24,20,20,24,24,28,28,72,72,72,28,72,68
92,24,28,72,20,20,72,28,24,24,24,28,24,28,28,24,24,28,24,28,72,24,72,72,72,28,20,20,20,20,28,24,32,24,28,72,72,72,28,72,68
92,24,24,72,24,28,68,28,24,28,24,24,28,24,28,20,16,20,24,24,68,24,72,72,72,20,20,28,20,24,20,20,28,24,28,72,72,72,24,72,68
88,28,24,72,24,24,72,24,24,28,24,24,24,28,24,28,28,20,24,24,68,24,72,64,72,24,28,24,20,24,20,24,28,24,28,68,72,72,24,72,64
88,24,28,72,20,24,76,24,28,24,24,28,24,28,28,20,28,24,24,20,72,28,72,72,72,24,20,24,28,20,20,24,24,28,28,72,72,72,20,72,68
92,24,20,68,24,28,72,20,24,28,24,28,28,24,24,28,28,24,24,20,68,24,72,72,72,24,24,28,24,28,28,24,28,28,24,72,72,72,24,68,68
88,28,28,72,20,28,72,28,28,24,28,24,24,28,24,20,28,24,28,28,72,28,72,72,72,20,20,24,24,24,28,24,24,24,28,72,72,72,20,72,72
92,24,24,72,24,28,68,24,24,28,24,28,28,24,28,20,28,20,24,20,68,24,72,72,72,20,16,24,24,28,20,20,28,24,28,72,72,76,24,72,68
84,24,28,72,24,28,72,28,24,28,24,20,24,28,24,24,28,20,24,20,72,24,72,72,72,24,24,28,24,20,24,20,28,24,28,72,72,72,24,72,64



I modded the DHT11 library v0.4 of George Hadjikyriacou to reflect the changes.

Thank you for your help, now the lib is working greatly =]


PS : When the lib will be finalised, i'll post it right here on this post.

By the way, as it seems thath the "PulseIn()" command has a bug, is there a way to report the bug to the DEV TEAM of the arduino ?
« Last Edit: March 25, 2011, 02:47:36 am by simkard » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
then realised that in your code the command "analogRead" was used, and i changed it to "digitalRead" which was more appropriate, and then TAAADAAA !!!! miracle !!!!
Oops - changed it in code above, it was allready getting late smiley-wink

- http://www.arduino.cc/en/Reference/PulseIn -
For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing.
In your case the cvalue is LOW and want to wait for the pin go HIGH, ....
so it is no bug, specification/ expectation issue.

There is a way to send bugreports: - http://code.google.com/p/arduino/issues/list -
Take care that the bug is confirmed (eg at the forum)  before posting to prevent "overloading" the dev team.
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

As promised, here is the DHT11 library from "George Hadjikyriakos" that i had to mod because i wasn't getting good values (checksum was always false).
Hope it will be used by lot of people !!!

LINK : http://www.megaupload.com/?d=UHNCH5EM
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


There is a serious bug in the lib in the code below:  What if the value of pulsedata is 48 or 49 ?



Code:
int dht11::create_binary(int a,int b,int c,int d,int e,int f,int g,int h)
{
    int place[]={128,64,32,16,8,4,2,1};
    int pulsedata[8];
    pulsedata[0] = a;
    pulsedata[1] = b;
    pulsedata[2] = c;
    pulsedata[3] = d;
    pulsedata[4] = e;
    pulsedata[5] = f;
    pulsedata[6] = g;
    pulsedata[7] = h;

    //Convert timings to binary
    for (int i=0; i<8; i++) {
        if (pulsedata[i] > 49) {
            pulsedata[i] = 1;
        }
        else {
            if (pulsedata[i] < 48) {
                pulsedata[i] = 0;
            }
        }
    }

    //Convert binary to decimal
    int decimal = (place[0]*pulsedata[0])+ (place[1]*pulsedata[1])+ (place[2]*pulsedata[2])+ (place[3]*pulsedata[3])+ (place[4]*pulsedata[4])+ (place[5]*pulsedata[5])+ (place[6]*pulsedata[6])+ (place[7]*pulsedata[7]);
    return decimal;
}

Furthermore this code uses way to much memory.

Code:
//Convert timings to byte
uint8_t dht11::create_binary(int * h, int start)  // only use the start index and the addres of the highs array
{
  uint8_t val = 0;
  for (int i=0; i<8; i++)
  {
    val * = 2;
    if (h[start + i] > 49)  val += 1;
  }
  return val;
}

The calls become

Code:
    rh_1 = create_binary(highs, 1);
    rh_2 = create_binary(highs, 9);
    t_1 = create_binary(highs, 17);
    t_2 = create_binary(highs, 25);
    checksum = create_binary(highs, 33);
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 125
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Thank you for your support in this library.
It is possibly me dealy with the values ... i had problems with the readings of the timings as said upper in this thread.


If i understood correctly, there is no use to convert timings to binary, then binary to decimal as it could be achieved directly to bytes ?

As i'm not a king in prog, could you please help me with dealing with the bugs & optimisations that are possibles in this library ?


Hope you can help me !  smiley-sad-blue
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As I have no such sensor at hand it is difficult to test, however I might give it a try .

Lets state that the current 0.2 version (without my remarks) is the reference version.
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: