jolle-pe
Posts: 10
Joined: Wed Jan 02, 2013 1:56 pm

Problem with DHT11 Sensor

Mon Jan 12, 2015 8:53 pm

Hi everybody,

i got a problem with a DHT11 Sensor.

I used a c++ program to read all data and save them into a sqlite database.

Here is my code (wich is a little optimized for failure)
You see if the value for temperature is 10.128, 11.128, 12.128
the program read the value again.

Code: Select all

double Service::ReadDHT11()
{
    bool check;                         
    int dht11_val[5]= {0,0,0,0,0};    
    double DouTempIn;                   
    double DouHumidity;                it
    do
    {


        uint8_t lststate=HIGH;          
        uint8_t counter=0;
        uint8_t j=0,i;

        for(i=0; i<5; i++)
            dht11_val[i]=0;
        pinMode(DHT11PIN,OUTPUT);
        digitalWrite(DHT11PIN,LOW);
        delay(18);
        digitalWrite(DHT11PIN,HIGH);
        delayMicroseconds(40);
        pinMode(DHT11PIN,INPUT);
        for(i=0; i<MAX_TIME; i++)
        {
            counter=0;
            while(digitalRead(DHT11PIN)==lststate)
            {
                counter++;
                delayMicroseconds(1);
                if(counter==255)
                    break;
            }
            lststate=digitalRead(DHT11PIN);
            if(counter==255)
                break;

            if((i>=4)&&(i%2==0))
            {
                dht11_val[j/8]<<=1;
                if(counter>16)
                    dht11_val[j/8]|=1;
                j++;
            }
        }                          



 if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))  /
        {
            stringstream SstrHumidity;
            stringstream SstrTempIn;
            SstrHumidity << dht11_val[0] << "." << dht11_val[1];    // Value for Humidity
            SstrTempIn << dht11_val[2] << "." << dht11_val[3];      // Value for Temperature

            SstrHumidity >> DouHumidity;
            SstrTempIn >> DouTempIn;


		if(DouHumidity==147.128)		
			check = false;
		if(DouTempIn==10.128||DouTempIn==11.128||DouTempIn==12.128)			
			check = false;
		else
            		check = true;                                           
        }
        else
            check = false;                                        
    }
    while(check==false);                                           

}

Sometimes i get 11°C as a value. But in this room is round about 22°C. So this must be a fail. Is there a possibillity to ignore more fails?

Best regards

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Problem with DHT11 Sensor

Mon Jan 12, 2015 11:55 pm

DHT11/22 sensor are problematic with user space. The timing of The RPi are far to be exact and could cause problem.

There is post about it

Joan is using Pigpio on python but you should be able to convert it C
http://www.raspberrypi.org/forums/viewt ... 75#p515575


This is an explanation using serial SPI. Code is available in C or python
http://www.raspberrypi.org/forums/viewt ... 13#p638713

Return to “Interfacing (DSI, CSI, I2C, etc.)”