Tuesday, November 25, 2014

ARDUINO Humidity & Temperature Measurement Using DHT11 Sensor

In this project we are going to interface the arduino uno with DHT11 digital humidity and temperature sensor.
About DHT11 humidity and temperature sensor:
The DHT11 sensor comes in a single row 4-pin package and operates from 3.5 to 5.5V power supply. It can measure temperature from 0-50 °C with an accuracy of ±2°C and relative humidity ranging from 20-95% with an accuracy of  ±5%. The sensor provides fully calibrated digital outputs for the two measurements. It has got its own proprietary 1-wire protocol, and therefore, the communication between the sensor and a microcontroller is not possible through a direct interface with any of its peripherals. The protocol must be implemented in the firmware of the MCU with precise timing required by the sensor.
dht11 sensor
The following timing diagrams describe the data transfer protocol between a MCU and the DHT11 sensor. The MCU initiates data transmission by issuing a “Start” signal. The MCU pin must be configured as output for this purpose. The MCU first pulls the data line low for at least 18 ms and then pulls it high for next 20-40 us before it releases it. Next, the sensor responds to the MCU “Start“  signal by pulling the line low for 80 us followed by a logic high signal that also lasts for 80 us. Remember that the MCU pin must be configured to input after finishing the “Start“ signal. Once detecting the response signal from the sensor, the MCU should be ready to receive data from the sensor. The sensor then sends 40 bits (5 bytes) of data continuously in the data line. Note that while transmitting bytes, the sensor sends the most significant bit first.
dht11 sensor
Data consists of decimal and integral parts. A complete data transmission is 40bit, and the sensor sends higher data bit first.
Data format: 8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit check sum. If the data transmission is right, the check-sum should be the last 8bit of "8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data".
The DHT11 is a digital sensor so it sends 1's and 0's, but it is very important to know how it sends the digital data. The figure below shows how the sensor sends its information:
dht11 sensor
Interfacing Arduino with DHT11 sensor circuit:
The figure below shows the wiring of our circuit using arduino uno board, DHT11 sensor and 16X2 LCD display:
interface dht11 sensor with arduino

dht11 sensor

Interfacing Arduino with DHT11 sensor code:
 #include <LiquidCrystal.h>
 LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
 byte deg[7] = {           //to display ° character
  B01110,
  B01010,
  B01110,
  B00000,
  B00000,
  B00000,
  B00000,
  };
 unsigned char a, b,i,rh1,rh2,t1,t2,sum;
 void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.createChar(0, deg);
 }
 int startsignal(){
  pinMode(8, OUTPUT);
  digitalWrite(8,LOW);
  delay(18);
  digitalWrite(8,HIGH);
  delayMicroseconds(30);digitalWrite(8,LOW);
  pinMode(8, INPUT);}
  
 int checkresponse(){
  a = 0;delayMicroseconds(40);
  if(digitalRead(8)==LOW){
    delayMicroseconds(80);
    if(digitalRead(8)==HIGH)
     a = 1;}
    delayMicroseconds(40);
  }
 
 int readdata(){
 for(b=0;b<8;b++){ 
   while(!digitalRead(8));  //Wait pin8 goes high
   delayMicroseconds(30);
   if(digitalRead(8)==LOW){ 
    bitClear(i,7-b);}
   else { bitSet(i,7-b); 
       while(digitalRead(8));//Wait pin8 goes low
      }
      }
  }
 void loop() {
  startsignal();    //send the start signal to the sensor
  checkresponse();  //check if the sensor sends the response signal
  if (a==1){
   readdata();
   rh1 = i;
   readdata();
   rh2 = i;
   readdata();
   t1=i;
   readdata();
   t2 = i;
   readdata();
   sum = i;
   if (sum == rh1+rh2+t1+t2){      //Checksum
     //Display Temperature  
     lcd.clear();
     lcd.setCursor(0, 0); lcd.print("Temp    =   .0 C");
     lcd.setCursor(10, 0); lcd.print(t1);
     lcd.setCursor(14, 0);lcd.write(byte(0));
     //Display Humidity:
     lcd.setCursor(0, 1); lcd.print("Humidity=   .0%");
     lcd.setCursor(10, 1); lcd.print(rh1);
   }
   else{        //If the checksum is incorrect
   lcd.clear();
     lcd.setCursor(1, 0); lcd.print("Checksum error");}  
 }
   else {       //If the sensor does not respond
    lcd.clear(); 
    lcd.setCursor(2, 0); lcd.print("no response");
    lcd.setCursor(0, 1); lcd.print("from the sensor");
    } 
 delay(1000); 
 }    


Interfacing Arduino with DHT11 sensor video: