Friday, May 13, 2016

PIC18F4550 microcontroller and DHT22(AM2302) sensor circuit + Proteus simulation

PIC18F4550 and DHT22 relative humidity and temperature sensor Proteus simulation
This topic shows how to interface DHT22 (AM2302, RHT03) digital relative humidity and temperature sensor with PIC18F4550 microcontroller, and how to simulate this interfacing using Proteus.
Note that for the simulation Proteus version should be 8.1 or higher. With these versions there is no need to install Proteus DHT22 library, it is included with the software, so don't waste your time searching for dht22 library, dht22 Proteus library, dhtxx.mdf or dht22 module for Proteus, just use Proteus version 8.1 or higher.
About DHT22 (AM2302, RHT03) relative humidity and temperature sensor:
The DHT22(AM2302, RHT03) sensor comes in a single row 4-pin package and operates from 3.3 to 5.5V power supply. It can measure temperature from -40-80 °C with an accuracy of ±0.5°C and relative humidity ranging from 0-100% with an accuracy of  ±2%. 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.
The following timing diagrams describe the data transfer protocol between a MCU and the DHT22 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.

DHT22 AM2302 RHT03 relative humidity and temperature sensor c code
Data consists of decimal and integral parts. A complete data transmission is 40bit, and the sensor sends higher data bit first.
Data format: 16 bits RH data + 16 bits temperature data + 8bit check sum. If the data transmission is right, the check-sum should be the last 8bit of "MSB 8-bit RH data + LSB 8-bit RH data + MSB 8-bit temperature data + LSB 8-bit temperature data".
The DHT22 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:

DHT22 AM2302 RHT03 digital humidity and temperature sensor c code
Example: MCU has received 40 bits data from AM2302 as
0000 0010 1000 1100 0000 0001 0101 1111 1110 1110
16 bits RH data 16 bits T data check sum
Here we convert 16 bits RH data from binary system to decimal system,
0000 0010 1000 1100 → 652
Binary system Decimal system
Here we convert 16 bits T data from binary system to decimal system,
0000 0001 0101 1111 → 351
Binary system Decimal system
When highest bit of temperature is 1, it means the temperature is below 0 degree Celsius.
Example: 1000 0000 0110 0101, T= minus 10.1℃
16 bits T data
Sum=0000 0010+1000 1100+0000 0001+0101 1111=1110 1110
Check-sum=the last 8 bits of Sum=1110 1110
Interfacing PIC18F4550 with DHT22(AM2302, RHT03) sensor circuit:
The following circuit schematic shows complete project circuit.

PIC18F4550 microcontroller DHT22 AM2302 RHT03 sensor circuit CCS PIC C
The circuit is simple, there is the microcontroller PIC18F4550, DHT22 sensor and 1602 LCD to display humidity and temperature results.
PIC18F4550 uses its internal oscillator @ 8MHz and mclr pin function is disabled.
Interfacing PIC18F4550 with DH22(AM2302-RHT03) sensor CCS C code:
The interfacing code is written with CCS PIC C compiler PCWHD version 5.051.
If you want to understand the code please read the DHT22 datasheet.
Variables Time_out and k are used to test reading time to avoid wrong data reception or microcontroller hanging.

// Interfacing PIC18F4550 with DHT22(AM2302, RHT03) sensor

//LCD module connections
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#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 <18F4550.h>
#use delay(clock = 8000000)
#include <lcd.c>
#use fast_io(B)
// Connection pin between PIC18F4550 and DHT22 sensor
#BIT Data_Pin = 0xF81.0                       // Pin mapped to PORTB.0
#BIT Data_Pin_Direction = 0xF93.0             // Pin direction mapped to TRISB.0

char message1[] = "Temp = 00.0 C";
char message2[] = "RH   = 00.0 %";
short Time_out ;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
unsigned int16 Temp, RH;
void start_signal(){
  Data_Pin_Direction = 0;              // Configure connection pin as output
  Data_Pin = 0;                        // Connection pin output low
  Data_Pin = 1;                        // Connection pin output high
  Data_Pin_Direction = 1;              // Configure connection pin as input
short check_response(){
  if(!Data_Pin){                     // Read and test if connection pin is low
    if(Data_Pin){                    // Read and test if connection pin is high
      return 1;}
unsigned int8 Read_Data(){
  unsigned int8 i, k, _data = 0;     // k is used to count 1 bit reading duration
  for(i = 0; i < 8; i++){
    k = 0;
    while(!Data_Pin){                          // Wait until pin goes high
      if (k > 100) {Time_out = 1; break;}
      bit_clear(_data, (7 - i));               // Clear bit (7 - i)
      bit_set(_data, (7 - i));                 // Set bit (7 - i)
      while(Data_Pin){                         // Wait until pin goes low
      if (k > 100) {Time_out = 1; break;}
  return _data;
void main(){
  setup_oscillator(OSC_8MHZ);                // Set internal oscillator to 8MHz
  setup_adc_ports(NO_ANALOGS);                // Configure AN pins as digital
  lcd_init();                                 // Initialize LCD module
  lcd_putc('\f');                             // LCD clear
    Time_out = 0;
    if(check_response()){                    // If there is response from sensor
      RH_byte1 = Read_Data();                 // read RH byte1
      RH_byte2 = Read_Data();                 // read RH byte2
      T_byte1 = Read_Data();                  // read T byte1
      T_byte2 = Read_Data();                  // read T byte2
      Checksum = Read_Data();                 // read checksum
      if(Time_out){                           // If reading takes long time
        lcd_putc('\f');                       // LCD clear
        lcd_gotoxy(5, 1);                     // Go to column 5 row 1
        lcd_putc("Time out!");
       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){
         message1[6] = '-';
         Temp = Temp & 0X7FFF; }
         message1[6] = ' ';
        message1[7]  = (Temp / 100) % 10  + 48;
        message1[8]  = (Temp / 10) % 10  + 48;
        message1[10] = Temp % 10  + 48;
        message2[7]  = (RH / 100) % 10 + 48;
        message2[8]  = (RH / 10) % 10 + 48;
        message2[10] = RH % 10 + 48;
        message1[11] = 223;                   // Degree symbol
        lcd_putc('\f');                       // LCD clear
        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_putc('\f');                     // LCD clear
          lcd_gotoxy(1, 1);                   // Go to column 1 row 1
          lcd_putc("Checksum Error!");
    else {
      lcd_putc('\f');             // LCD clear
      lcd_gotoxy(3, 1);           // Go to column 3 row 1
      lcd_putc("No response");
      lcd_gotoxy(1, 2);           // Go to column 1 row 2
      lcd_putc("from the sensor");

Interfacing PIC18F4550 with DHT22(AM2302, RHT03) sensor Proteus simulation video:
The following video shows Proteus simulation of this interfacing.

DHT22(AM2302, RHT03) Proteus simulation file download:

DHT22 datasheet