Tuesday 19 October 2010

DHT11 Arduino with VirtualWire

By some reason when using VirtualWire library and using vw_setup(2000) caused the readings from humidity sensor to fail (dht11 start condition 1 not met). I looked at datasheet and changed a the times how the MCU starts to talk with the sensor.

The code is based from this post

Here is the the fixed code:

void InitDHT() {
 pinMode(TEMP_RH_PIN, OUTPUT);
 delay(1000);
 digitalWrite(TEMP_RH_PIN,HIGH);
}

void ReadDHT() {

 bGlobalErr=0;
 byte dht_in;
 byte i;
        
        // see datasheet to understand this
        pinMode(TEMP_RH_PIN, OUTPUT);
        digitalWrite(TEMP_RH_PIN,HIGH);
        
 digitalWrite(TEMP_RH_PIN,LOW);
 delay(18);

 digitalWrite(TEMP_RH_PIN,HIGH);
 delayMicroseconds(22);

 pinMode(TEMP_RH_PIN,INPUT);
 delayMicroseconds(5);

 dht_in=digitalRead(TEMP_RH_PIN);

 if(dht_in) {
  bGlobalErr=1;
  Serial.println("<dht11 start condition 1 not met");
  return;
 }
 
 delayMicroseconds(80);
 dht_in=digitalRead(TEMP_RH_PIN);

 if(!dht_in) {
  bGlobalErr=2;
  Serial.println("<dht11 start condition 2 not met");
  return;
 }
 delayMicroseconds(80);
 
        //now ready for data reception... pick up the 5 bytes coming from the sensor
 for (i=0; i<5; i++)
    dht_dat[i] = read_dht_dat();

 //Next: restore pin to output duties
 pinMode(TEMP_RH_PIN,OUTPUT);
 digitalWrite(TEMP_RH_PIN,HIGH);

 byte dht_check_sum = dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];

 /*Condition in following "if" says "if fifth byte from sensor
       not the same as the sum of the first four..."*/
 if(dht_dat[4]!= dht_check_sum) {
   bGlobalErr=3;
          Serial.println("DHT11 checksum error");   
 }

}


byte read_dht_dat() {
 //Collect 8 bits from datastream, return them interpreted
 //as a byte. I.e. if 0000.0101 is sent, return decimal 5.
 //Code expects the system to have recently entered the
 //dataline low condition at the start of every data bit's
 //transmission BEFORE this function is called.
 byte i = 0;
 byte result=0;
 for (i=0; i< 8; i++) {
  //We enter this during the first start bit (low for 50uS) of the byte
  //Next: wait until pin goes high
  while(digitalRead(TEMP_RH_PIN)==LOW);
  delayMicroseconds(30);
  if (digitalRead(TEMP_RH_PIN)==HIGH)//Was: if(PINC & _BV(dht_PIN))
  result |=(1<<(7-i));
  while (digitalRead(TEMP_RH_PIN)==HIGH);
  //Was: while((PINC & _BV(dht_PIN)));
 }
 //end of "for.."
 return result;
}

4 comments:

  1. hi you :-) the code work on my arduino with dht11. thank you. - but i have one problem: i have temperature under 1°C. have you a idea for this problem? i can not display under 1°C an i´m newbie... i hope you can help...

    ReplyDelete
  2. sorry - i have read the data sheet :-))))

    ReplyDelete
  3. I am trying to use your code but most of the variables are not declared. I guess I am doing something wrong..

    I am using Arduino 1.0.1 and don't know if there needs to be a library or something included.
    Can you just give me a sketch that I copy /paste it to arduino and compiles successfully?
    Can you please help me as soon as possible?

    Thanks in advance!

    ReplyDelete
  4. Hi NaThAN, you need to add the VirtualWire lib to your arduino

    ReplyDelete