Pages

Saturday, September 3, 2016

DHT11 VS DHT22

Comparison between DHT11 (RHT01) and DHT22 (AM2302 - RHT03) sensors
DHT11 VS DHT22 Hardware circuit using PIC16F877A and 2004 (20x4) LCD 
DHT11 (RHT01) and DHT22 (AM2302 - RHT03) are relative humidity and temperature sensors. These sensors are small, cheap and easy to use. They use only one wire to communicate with mcu.
The following two topic shows how to interface PIC16F877A with DHT11 and DHT22 sensors respectively:
Interfacing DHT11 relative humidity and temperature sensor with PIC16F877A microcontroller
Interfacing PIC16F877A with DHT22(AM2302-RHT03) sensor using CCS PIC C

The following table shows the DHT11 sensor characteristics (from DHT11 datasheet):
DHT11 Characteristics 
And the following table shows DHT22 (AM2302) technical specification (from AM2302 datasheet):
DHT22 Characteristics
The differences between the two sensors are summarized in the following table:


DHT11 DHT22
Relative Humidity Operating Range 20 ~ 90% 0 ~ 100%
Relative Humidity Accuracy ±5%RH ±2%RH (Max ±5%RH)
Humidity Resolution1%RH 0.1%RH
Humidity Repeatability±1%RH ±1%RH
Humidity Hysteresis ±1%RH ±0.3%RH
Long-term Stability ±1%RH/year ±0.5%RH/year
Temperature Operating Range 0 ~ 50°C -40 ~ 80°C
Temperature Accuracy±2°C±0.5°C
Temperature Resolution1°C0.1°C

DHT11 and DHT22 interfacing with PIC16F877A microcontroller:
The following circuit schematic shows the comparison between DHT11 and DHT22 sensors connection circuit.
DHT11 versus DHT22 (AM2302) using PIC16F877A and 20x4 LCD circuit CCS
The microcontroller used to make the comparison between DHT11 and DHT22 is PIC16F877A which runs with crystal oscillator at 8MHz.
The sensor DHT11 as well as DHT22 has 4 pins which are:
Vcc : Positive power supply (+5V)
Data : Sensor data input and output
NC : Not connected terminal
Gnd : Ground (0V).
A pull-up resistor must be added between the data pin of each sensor and VCC (+5V) pin as shown in the circuit schematic (4.7K ~ 10K).
2004 (20x4) LCD display is used to display temperature and humidity results for both sensors.
CCS C code for DHT11 vs DHT22 project:
The comparison between DHT11 and DHT22 (AM2302) sensors full CCS C code is below.
// DHT11 VS DHT22 CCS C code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP                       
#use delay(clock = 8000000)
#include <lcd.c>
#use fast_io(B)
#define DHT11 PIN_B4                           // Connection pin between DHT11 and mcu
#define DHT22 PIN_B5                           // Connection pin between DHT22 and mcu

char message1[] = "DHT11 Temp = 00.0 C ";
char message2[] = "DHT11 RH   = 00.0 % ";
char message3[] = "DHT22 Temp = 00.0 C ";
char message4[] = "DHT22 RH   = 00.0 % ";
short Time_out;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
unsigned int16 Temp, RH;
void dht11_start_signal(){
  output_drive(DHT11);                        // Configure connection pin as output
  output_low(DHT11);                          // Connection pin output low
  delay_ms(25);
  output_high(DHT11);                         // Connection pin output high
  delay_us(30);
  output_float(DHT11);                        // Configure connection pin as input
}
short dht11_check_response(){
  delay_us(40);
  if(!input(DHT11)){                          // Read and test if connection pin is low
    delay_us(80);
    if(input(DHT11)){                         // Read and test if connection pin is high
      delay_us(50);
      return 1;
    }
  }
}
unsigned int8 dht11_Read_Data(){
  unsigned int8 i, k, _data = 0;        // k is used to count 1 bit reading duration
  if(Time_out)
    break;
  for(i = 0; i < 8; i++){
    k = 0;
    while(!input(dht11)){                            // Wait for DHT22 pin to go high
      k++;
      if(k > 100){
        Time_out = 1;
        break;
      }
      delay_us(1);
    }
    delay_us(30);
    if(!input(dht11))
      bit_clear(_data, (7 - i));                     // Clear bit (7 - i)
    else{
      bit_set(_data, (7 - i));                       // Set bit (7 - i)
      while(input(dht11)){                           // Wait for DHT22 pin to go low
        k++;
        if(k > 100){
        Time_out = 1;
        break;
      }
      delay_us(1);}
    }
  }
  return _data;
}
void dht22_start_signal(){
  output_drive(DHT22);                        // Configure connection pin as output
  output_low(DHT22);                          // Connection pin output low
  delay_ms(25);
  output_high(DHT22);                         // Connection pin output high
  delay_us(30);
  output_float(DHT22);                        // Configure connection pin as input
}
short dht22_check_response(){
  delay_us(40);
  if(!input(DHT22)){                          // Read and test if connection pin is low
    delay_us(80);
    if(input(DHT22)){                         // Read and test if connection pin is high
      delay_us(50);
      return 1;
    }
  }
}
unsigned int8 dht22_Read_Data(){
  unsigned int8 i, k, _data = 0;                // k is used to count 1 bit reading duration
  if(Time_out)
    break;
  for(i = 0; i < 8; i++){
    k = 0;
    while(!input_state(dht22)){                      // Wait for DHT22 pin to go high
      k++;
      if(k > 100){
        Time_out = 1;
        break;
      }
      delay_us(1);
    }
    delay_us(30);
    if(!input_state(dht22))
      bit_clear(_data, (7 - i));                     // Clear bit (7 - i)
    else{
      bit_set(_data, (7 - i));                       // Set bit (7 - i)
      while(input_state(dht22)){                     // Wait for DHT22 pin to go low
        k++;
        if(k > 100){
        Time_out = 1;
        break;
      }
      delay_us(1);}
    }
  }
  return _data;
}
void main(){
  lcd_init();                                        // Initialize LCD module
  lcd_putc('\f');                                    // LCD clear
  delay_ms(1000);
  while(TRUE){
    Time_out = 0;
    dht11_Start_signal();
    if(dht11_check_response()){                // If there is a response from dht11 sensor
      RH_byte1 = dht11_Read_Data();                  // read RH byte1
      RH_byte2 = dht11_Read_Data();                  // read RH byte2
      T_byte1 = dht11_Read_Data();                   // read T byte1
      T_byte2 = dht11_Read_Data();                   // read T byte2
      Checksum = dht11_Read_Data();                  // read checksum
      if(Time_out){                                  // If reading takes long time
        lcd_gotoxy(1, 1);                            // Go to column 1 row 1
        lcd_putc("  DHT11 Time out!   ");
        lcd_gotoxy(1, 2);                            // Go to column 1 row 1
        lcd_putc("                    ");            // Clear second row
      }
      else{
       if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){
         message1[13]  = T_Byte1/10  + 48;
         message1[14]  = T_Byte1%10  + 48;
         message1[16] = T_Byte2/10  + 48;
         message2[13]  = RH_Byte1/10 + 48;
         message2[14]  = RH_Byte1%10 + 48;
         message2[16] = RH_Byte2/10 + 48;
         message1[17] = 223;                         // Degree symbol
         lcd_gotoxy(1, 1);                           // Go to column 1 row 1
         printf(lcd_putc, message1);                 // Display message1
         lcd_gotoxy(1, 2);                           // Go to column 1 row 2
         printf(lcd_putc, message2);                 // Display message2
       }
       else{
         lcd_gotoxy(1, 1);                           // Go to column 1 row 1
         lcd_putc("DHT11 Checksum Error");
         lcd_gotoxy(1, 2);                           // Go to column 1 row 2
         lcd_putc("                    ");           // Clear second row
       }
      }
    }
    else {                                     // If dht11 sensor don't respond
      lcd_gotoxy(1, 1);                              // Go to column 1 row 1
      lcd_putc("    No response     ");
      lcd_gotoxy(1, 2);                              // Go to column 1 row 2
      lcd_putc(" from DHT11 sensor  ");
    }
    Time_out = 0;
    dht22_Start_signal();
    if(dht22_check_response()){                       // If there is a response from sensor
      RH_byte1 = dht22_Read_Data();                  // read RH byte1
      RH_byte2 = dht22_Read_Data();                  // read RH byte2
      T_byte1 = dht22_Read_Data();                   // read T byte1
      T_byte2 = dht22_Read_Data();                   // read T byte2
      Checksum = dht22_Read_Data();                  // read checksum
      if(Time_out){                                  // If reading takes long time
        lcd_gotoxy(21, 1);                           // Go to column 1 row 1
        lcd_putc("  DHT22 Time out!   ");
        lcd_gotoxy(21, 2);                           // Go to column 1 row 4
        lcd_putc("                    ");            // Clear fourth row
      }
      else{
       if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){
         RH = RH_byte1;
         RH = (RH << 8) | RH_byte2;
         Temp = T_byte1;
         Temp = (Temp << 8) | T_byte2;
         if(Temp > 0X8000){
           message3[12] = '-';
           Temp = Temp & 0X7FFF; }
         else
          message3[12] = ' ';
         message3[13]  = (Temp / 100) % 10  + 48;
         message3[14]  = (Temp / 10) % 10  + 48;
         message3[16] = Temp % 10  + 48;
         message4[13]  = (RH / 100) % 10 + 48;
         message4[14]  = (RH / 10) % 10 + 48;
         message4[16] = RH % 10 + 48;
         message3[17] = 223;                         // Degree symbol
         lcd_gotoxy(21, 1);                          // Go to column 1 row 3
         printf(lcd_putc, message3);                 // Display message3
         lcd_gotoxy(21, 2);                          // Go to column 1 row 4
         printf(lcd_putc, message4);                 // Display message4
       }
       else{
         lcd_gotoxy(21, 1);                          // Go to column 1 row 3
         lcd_putc("DHT22 Checksum Error");
         lcd_gotoxy(21, 2);                          // Go to column 1 row 4
         lcd_putc("                    ");           // Clear fourth row
       }
      }
    }
    else {                                       // If dht22 sensor don't respond
      lcd_gotoxy(21, 1);                             // Go to column 1 row 3
      lcd_putc("    No response     ");
      lcd_gotoxy(21, 2);                             // Go to column 1 row 4
      lcd_putc(" from DHT22 sensor  ");
    }
  delay_ms(1000);
  }
}   // End of program

DHT11 VS DHT22 Video:
The following video shows the comparison between the two sensors results.