Simple example to read humidity and temperature from DHT11 sensor

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DHT11.h Source File

DHT11.h

00001 #include "mbed.h"
00002 
00003 //Class to manage DHT11 sensor
00004 class DHT11{
00005   
00006     public:
00007         /* Takes the data pin of the sensor in parameter */
00008         DHT11(PinName pin):dataPin(pin)
00009         {
00010             iBit = 0;
00011             data = 0;
00012         }  
00013         
00014         /*  Request and receive new data from sensor
00015             Returns : 
00016                 1 : OK
00017                 -1 : Checksum Error
00018         */
00019         int readData(void){
00020             this->data = 0;
00021             this->dataPin.output(); 
00022             // Request a measurement (low during t > 18ms)
00023             this->dataPin = 0;
00024             wait_ms(20);
00025             this->dataPin = 1;
00026             
00027             //Wait for the sensor to take control and set low level /!\ Important
00028             wait_us(20);
00029             
00030             this->dataPin.input();
00031             
00032         //TODO Check if timing is correct (low : 80µs ; high 80µs)
00033             // Wait until end of 80µs low
00034             while(!this->dataPin.read());
00035             // Wait until end of 80 µs high
00036             while(this->dataPin.read());
00037             
00038             // Sensor reply 40bits 
00039             for(iBit=0; iBit<40; iBit++) {
00040                 this->data = this->data << 1; // Shift for new number
00041                 this->timer.stop(); 
00042                 this->timer.reset(); 
00043                 
00044                 // Wait for low level to end
00045                 while(!this->dataPin.read());       
00046                 this->timer.start();     
00047                 // Count time while high level     
00048                 while(this->dataPin.read());
00049                 
00050                 if(this->timer.read_us() > 50) 
00051                 {
00052                     //This bit is '1'
00053                     this->data++;
00054                 }
00055             }
00056             
00057             wait_ms(250);
00058             
00059             //Checking checksum
00060             if((this->data & 0x00000000000000ff) != ((this->data & 0x000000ff00000000) >> 32) + 
00061                                                     ((this->data & 0x00000000ff000000) >> 24) + 
00062                                                     ((this->data & 0x0000000000ff0000) >> 16) + 
00063                                                     ((this->data & 0x000000000000ff00) >> 8))
00064             {
00065                 return -1;        
00066             }
00067             
00068             return 1;
00069         }
00070         
00071         int getTemperature(void){
00072             return (int)((this->data & 0x0000000000ff0000) >> 16);
00073         }
00074         
00075         int getHumidity(void){
00076             return (int)((this->data & 0x000000ff00000000) >> 32);
00077         }
00078         
00079     
00080     private:
00081         DigitalInOut dataPin;//Communication with the sensor
00082         Timer timer; //initialize timer
00083         uint64_t data; // 64 bit variable for temporary data
00084         int iBit;
00085 };