1 wire communication problem

hi guys,
I'm trying to read 40bits from DHT11(Humidity and temperature sensor).
It uses 1 wire communication.
I need to pull LOW for 18ms, and then pull HIGH to request data from it.
It then send 40bits immediately, 28us represents"0" and 70us represents"1".
Here is the datasheet- www.robotshop.com/PDF/dht11.pdf

I've been trying to get data for several days with pulseIn() and I couldn't get it.

By using the code below,

#include <SoftwareSerial.h>
int response;
int data[40];

void setup(){
  pinMode(11,OUTPUT);
  digitalWrite(11,HIGH);
  Serial.begin(9600);
  }


void loop(){
  delay(1000);
  pinMode(12,OUTPUT);
  digitalWrite(12,HIGH);
  digitalWrite(12,LOW);
  delay(18);
  digitalWrite(12,HIGH);

  pinMode(12,INPUT);
  response = pulseIn(12,HIGH);
  Serial.println(response);
  Serial.println();

  for (int i=0; i<40;i++){
     data[i] = pulseIn(12,HIGH);
   }
  for (int i=0; i<40;i++){
     Serial.println(data[i]);
   }
  delay(10000);
}

I gets

23
64
23
23
23
24
24
23
24
69
14
23
24
23
23
70
48
69
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

In contrast, by using a more stupid method,

#include <SoftwareSerial.h>
int response;
int data[40];

void setup(){
  pinMode(11,OUTPUT);
  digitalWrite(11,HIGH);
  Serial.begin(9600);
  }


void loop(){
  delay(1000);
  pinMode(12,OUTPUT);
  digitalWrite(12,HIGH);
  digitalWrite(12,LOW);
  delay(18);
  digitalWrite(12,HIGH);

  pinMode(12,INPUT);
  response = pulseIn(12,HIGH);
  Serial.println(response);
  Serial.println();

 response = pulseIn(12,HIGH);
  Serial.println(response);
  response = pulseIn(12,HIGH);
  Serial.println(response);
 *40 times!!!!!
 
  }
  delay(10000);
}

guess what? I got only 8 none zero numbers!

Could anybody help with this problem? This is killing me~
Thanks!

I think the serial output might be taking so much time you miss most of the message. Try removing these two lines:

  Serial.println(response);
  Serial.println();

But in the first code, I tried to read all bits into an array first.
If this is still not fast enough, I could not figure out a faster way.
Any suggestion for a faster solution? would pointer help?

Thanks.

seanfan84:
But in the first code, I tried to read all bits into an array first.
If this is still not fast enough, I could not figure out a faster way.

If the first code you posted above is what you are calling 'the first code' you should read it again:

You signal for the DHT11 to start sending:

  digitalWrite(12,LOW);
  delay(18);
  digitalWrite(12,HIGH);

Switch to input mode:

  pinMode(12,INPUT);

Measure the length of the acknowledgement pulse:

  response = pulseIn(12,HIGH);

AND PRINT OUT THE LENGTH OF THE ACKNOWLEDGEMENT PULSE:

  Serial.println(response);
  Serial.println();

Before you fill the buffer with 40 more pulse lengths:

  for (int i=0; i<40;i++){
     data[i] = pulseIn(12,HIGH);
  }

Are you rolling your own 1-wire library? Why not use the standard library? While you're at it, isn't there a driver library for that sensor too?

I removed those two lines, I got 20bits instead of 19 previously.
Still it does not work.
I'm thinking if that is the problem of pulseIn function.
Thanks.

johnwasser:

seanfan84:
But in the first code, I tried to read all bits into an array first.
If this is still not fast enough, I could not figure out a faster way.

If the first code you posted above is what you are calling 'the first code' you should read it again:

You signal for the DHT11 to start sending:

  digitalWrite(12,LOW);

delay(18);
  digitalWrite(12,HIGH);




Switch to input mode:


pinMode(12,INPUT);




Measure the length of the acknowledgement pulse:


response = pulseIn(12,HIGH);




AND PRINT OUT THE LENGTH OF THE ACKNOWLEDGEMENT PULSE:


Serial.println(response);
  Serial.println();




Before you fill the buffer with 40 more pulse lengths:


for (int i=0; i<40;i++){
     data[i] = pulseIn(12,HIGH);
  }

Thanks, I just found that there is a library of this.
:smiley: :smiley: :smiley:

PeterH:
Are you rolling your own 1-wire library? Why not use the standard library? While you're at it, isn't there a driver library for that sensor too?