Pages: [1]   Go Down
Author Topic: Arduino - Temperature & Humidity DHT11 without delay example  (Read 4870 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there!
I've been using the DHT11 sensor a lot and I spent time making the code its time. So I made a sketch example so anyone can use it.
 I've been hoping that you can post my code to the examples, because its frustrating for new arduino users to use the DHT11 example code directly.
It also is very slow (2seconds delay in each loop).

So I made a sketch to match all your needs.
It reads the sensor every 2 seconds without delays.
The user can also use the temperature and humidity values directly as an integer and compare it to other values without making new arrays.


Please check it out and let me know if you find any errors.

Code:
/*  DHT11 Example Sketch Code for reading the sensor without delay on your program!
 Example Code by: Nick Athanasoulas
 Date: 27/5/2012
 FUNCTION: It reads the sensor every 2 seconds without delays.
 The user can also use the temperature and humidity values directly as an integer
 and compare it to other values without making new arrays.
 */
#define DHT11_PIN 4      // ADC0  Define the ANALOG Pin connected to DHT11 Sensor
int temp1[3];                //Temp1, temp2, hum1 & hum2 are the final integer values that you are going to use in your program.
int temp2[3];                // They update every 2 seconds.
int hum1[3];
int hum2[3];

byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){


    while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
    delayMicroseconds(30);

    if(PINC & _BV(DHT11_PIN))
      result |=(1<<(7-i));
    while((PINC & _BV(DHT11_PIN)));  // wait '1' finish


  }
  return result;
}


long dht11delay_previousMillis = 0;        // will store last time LED was updated
long dht11delay_interval = 1000;           // dht11delay_interval at which to blink (milliseconds)

void setup()
{
  DDRC |= _BV(DHT11_PIN);
  PORTC |= _BV(DHT11_PIN);

  Serial.begin(9600);
Serial.println("DHT11 without delay");
Serial.println("Example code by: Nick Athanasoulas");
  Serial.println("Ready");
  delay(1000);
}

void loop()
{




  unsigned long dht11delay_currentMillis = millis();

  if(dht11delay_currentMillis - dht11delay_previousMillis > dht11delay_interval) {
    // save the last time you blinked the LED
    dht11delay_previousMillis = dht11delay_currentMillis;  
    byte dht11_dat[5];
    byte dht11_in;
    byte i;
    // start condition
    // 1. pull-down i/o pin from 18ms
    PORTC &= ~_BV(DHT11_PIN);
    delay(18);
    PORTC |= _BV(DHT11_PIN);
    delayMicroseconds(40);

    DDRC &= ~_BV(DHT11_PIN);
    delayMicroseconds(40);

    dht11_in = PINC & _BV(DHT11_PIN);

    if(dht11_in){
      Serial.println("dht11 start condition 1 not met");
      return;
    }
    delayMicroseconds(80);

    dht11_in = PINC & _BV(DHT11_PIN);

    if(!dht11_in){
      Serial.println("dht11 start condition 2 not met");
      return;
    }
    delayMicroseconds(80);
    // now ready for data reception
    for (i=0; i<5; i++)
      dht11_dat[i] = read_dht11_dat();

    DDRC |= _BV(DHT11_PIN);
    PORTC |= _BV(DHT11_PIN);

    byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
    // check check_sum
    if(dht11_dat[4]!= dht11_check_sum)
    {
      Serial.println("DHT11 checksum error");
    }



    temp1[0]=dht11_dat[2];
    temp2[0]=dht11_dat[3];
    Serial.print("Temperature: ");
    Serial.print(temp1[0]);
    Serial.print(".");
    Serial.print(temp2[0]);
    Serial.print(" C");
    Serial.print("    ");
    hum1[0]=dht11_dat[0];
    hum2[0]=dht11_dat[1];
    Serial.print("Humidity: ");
    Serial.print(hum1[0]);
    Serial.print(".");
    Serial.print(hum2[0]);
    Serial.println("%");

  }
}
// END OF CODE
« Last Edit: May 27, 2012, 07:47:35 am by nathanas » Logged

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


Seems like working code (did not test it) - recognize the handshake, CRC checking etc - would prefer it in a separate class.

For the DHT11 there is no decimal part so it makes not much sense to print allways .0 for temp and humidity.

Another drawback is that not all pins are possible because you hardcoded PORTC ...

Logged

Rob Tillaart

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems to be a problem sometimes though.
The DHT11 is not communicating because of a program error.
I get "dht11 start condition 1 not met"

Many have the same problem. I found a post of someone that has fixed this error. But his code is not compiling. Please take a look, because sometimes Arduino can't see the readings correctly.

http://linux-utils.blogspot.com/2010/10/dht11-arduino-with-virtualwire.html?showComment=1338136957757#c8321490607167585796
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ground Pin of the DHT11 was broken. That's why I received the "dht11 start condition 1 not met" error.

All are working like a charm now!
Logged

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

Good to hear!

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: