Pages: [1]   Go Down
Author Topic: Using the DHT11 temp/hum sensor  (Read 484 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI everyone,

I just get this example for the DHT11 sensor and add some problem reading is output.

This is my code
Code:
double humidity;
double temperature;


void setup()
{
  Serial.begin(115200);
  Serial.println("DHT11 TEST PROGRAM ");

}


void loop()
{
  read_dht11();
 
}

void read_dht11(void){
 
  // BUFFER TO RECEIVE
uint8_t bits[5];
uint8_t cnt = 7;
uint8_t idx = 0;

        // EMPTY BUFFER
for (int i=0; i< 5; i++) bits[i] = 0;

// REQUEST SAMPLE
pinMode(22, OUTPUT);
digitalWrite(22, LOW);
delay(18);
digitalWrite(22, HIGH);
delayMicroseconds(40);
pinMode(22, INPUT);

// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = 10000;
while(digitalRead(22) == LOW)
if (loopCnt-- == 0)
                {
                  Serial.println("Error time out 1");
                }
        Serial.println("Passe 1");
loopCnt = 10000;
while(digitalRead(22) == HIGH)
if (loopCnt-- == 0)
                {
                    Serial.println("Error time out 2");
                }
        Serial.println("Passe 2");
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++)
{
loopCnt = 10000;
while(digitalRead(22) == LOW)
if (loopCnt-- == 0)
                        {
                            Serial.println("Error time out 3");
                        }
                Serial.println("Passe 3");
unsigned long t = micros();

loopCnt = 10000;
while(digitalRead(22) == HIGH)
if (loopCnt-- == 0)
                        {
                            Serial.println("Error time out 4");
                        }
                Serial.println("Passe 4");
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0)   // next byte?
{
cnt = 7;    // restart at MSB
idx++;      // next byte!
                        Serial.println("Count = 0");
}
else cnt--;
                Serial.println("Count --");
}

// WRITE TO RIGHT VARS
        // as bits[1] and bits[3] are allways zero they are omitted in formulas.
humidity    = bits[0];
temperature = bits[2];

uint8_t sum = bits[0] + bits[2]; 

if (bits[4] != sum)
        {
            Serial.println("Checksum error");
        }
Serial.println("OK");

}


And this is why I get for troubleshooting on the serial monitor

Code:
DHT11 TEST PROGRAM
Passe 1
Passe 2
Passe 3
Passe 4
Count --
Passe 3
Passe 4
Count --
Passe 3
Passe 4
Count --
Passe 3
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4
Error time out 4




Is some of you have already worked with this sensor??

At first it seems to work, but after couple of iteration....error 4, error 4, error 4....



Thanks in advance!
Logged

Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 504
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are reading the Sensor too quickly.
The sensor has maximum reading rate of once every 2 seconds.
Put some delay in your reading loop.
Logged

Pages: [1]   Go Up
Jump to: