Sunday, November 22, 2015

MikroC library for DHT22 (AM2302) digital humidity and temperature sensor

Interface PIC18F4550 with DHT22 (AM2302) sensor

This is a new mikroC PRO for PIC library for DHT22 digital humidity and temperature sensor.
This library works with all PIC12, 16, 18 series and makes the interfacing more easier.
The library has only one function for reading humidity and temperature and store them in one 32-bit number. This 32-bit number can be divided into two 16-bit numbers, the first number for the humidity and the last number for the temperature. The function DHT22_readData() checks if the sensor is responding and checks the results checksum.
If the sensor is not responding, the function returns the value 0x63636363, and if the checksum is not correct the function returns the value 0x58585858. The function DHT22_readData() returns always a number of 32-bit.
For example if the returned number is: 0x021E00EF, this means that the corresponding 16-bit humidity number is 0x021E = 542 ===> RH = 542/10  = 54.2 % 
and temperature data is 0x00EF = 239 ===> T = 239/10 = 23.9 °C
MikroC library for DHT22 (AM2302) example: 
This example shows how to interface PIC18F4550 with DHT22 using MikroC library for DHT22. Circuit schematic diagram shown below.
mikroc library for dht22 am2302

The internal oscillator of the microcontroller is used (8MHz) and the MCLR function is disabled.
MikroC Code:

// MikroC library for DHT22 sensor example
// PIC18F4550 + DHT22 digital humidity
//        and temperature sensor MikroC code
// Written by: BENCHEROUDA Okba
// electronnote@gmail.com
// http://elecnote.blogspot.com/
// Internal oscillator used at 8MHz
// Use at your own risk


 // LCD module connections
sbit LCD_RS at RB5_bit;
sbit LCD_EN at RB4_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB2_bit;
sbit LCD_D6 at RB1_bit;
sbit LCD_D7 at RB0_bit;
sbit LCD_RS_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7_Direction at TRISB0_bit;
// End LCD module connections

  // DHT22 sensor connections
sbit DHT22_Pin at RD0_bit;
sbit DHT22_Pin_Direction at TRISD0_bit;
 // End DHT22 sensor connections

 unsigned Temp, RH ;
 unsigned long value;
 const char degree[] = {14,10,14,0,0,0,0,0};
void degreeChar(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(degree[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}
void main() {
 OSCCON =  0X70;       // Set internal oscillator to 8MHz
 ADCON1 = 0x0F;        // Configure AN pins as digital
 Lcd_Init();
 Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
 Lcd_Cmd(_LCD_CLEAR);            // clear LCD
 while(1){
  value = DHT22_readData();
  if (value == 0x63636363){
   Lcd_Out(1, 1, "  No response   ");
  Lcd_Out(2, 1, "from the sensor ");
  }
  if (value == 0x58585858){
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Out(1, 1, "Check sum error ");
  }
  if ((value != 0x63636363) && (value != 0x58585858)) {
  Temp = value & 0xFFFF;
  value = value >> 16;
  RH = value & 0xFFFF;
  Lcd_Out(1, 1, "    Temp:   .  C");
  Lcd_Out(2, 1, "Humidity:   .  %");
  degreeChar(1, 15);
  if (Temp > 0X8000){
  Lcd_Out(1, 10, "-");
  Temp = Temp & 0X7FFF; }
  LCD_Chr(1, 11, 48 + ((Temp / 100) % 10));
  LCD_Chr(1, 12, 48 + ((Temp / 10) % 10));
  LCD_Chr(1, 14, 48 + (Temp % 10));
  if (RH == 1000)
  LCD_Chr(2, 10, 48 + ((RH / 1000) % 10));
  LCD_Chr(2, 11, 48 + ((RH / 100) % 10));
  LCD_Chr(2, 12, 48 + ((RH / 10) % 10));
  LCD_Chr(2, 14, 48 + (RH % 10));
   }
  delay_ms(1000);
  }
 }

The following video shows how to install the library and has more descriptions: