Tuesday, November 25, 2014

Interfacing DHT11 humidity and temperature sensor with PIC16F877A

After interfacing the DHT11 with Arduino uno board at the following post:
ARDUINO Humidity & Temperature Measurement Using DHT11 Sensor
Now we are going to see how to interface this sensor with microchip pic16f877a.
There are some descriptions of how this sensor work  in the above link.
Interfacing DHT11 sensor with PIC16F877A circuit:
The below picture shows the schematic diagram of our circuit, the LCD is 16x2 and crystal is 12MHz.
Interfacing DHT11 sensor with PIC16F877A circuit

A brief description of the code:
The code is written using MikroC compiler.
 First we must send a start signal to the sensor, we do that by configuring the pic pin connected to the sensor as output, the mcu sends 0 for 18ms then sends 1 for 30us.
After sending start signal to the sensor, the sensor will send a response signal to the mcu. To detect this signal mcu pin must be configured as input.
When the sensor finishes the response signal, it begins sending humidity and temperature data serially.
If there is a problem with the sensor or there is no sensor connected to the circuit, the LCD displays "there is no response from the sensor". And if there is a problem in the sensor which means the values are incorrect the LCD displays "Check sum error".
Interfacing DHT11 sensor with PIC16F877A mikroc

Interfacing DHT11 sensor with PIC16F877A video:
This video shows temperature and humidity in my home at: 18:33 11/13/2014 :

Interfacing DHT11 sensor with PIC16F877A mikroC code:
 //PIC16F877A DHT11 digital humidity and 
 //         temperature sensor MikroC code
//Written by: BENCHEROUDA Okba
//Used crystal: 12MHz
//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
 char *text,mytext[4];
 unsigned char  a = 0, b = 0,i = 0,t1 = 0,t2 = 0,
               rh1 = 0,rh2 = 0,sum = 0;
 void StartSignal(){
 TRISD.F2 = 0;    //Configure RD2 as output
 PORTD.F2 = 0;    //RD2 sends 0 to the sensor
 PORTD.F2 = 1;    //RD2 sends 1 to the sensor
 TRISD.F2 = 1;    //Configure RD2 as input
 void CheckResponse(){
 a = 0;
 if (PORTD.F2 == 0){
 if (PORTD.F2 == 1)   a = 1;   delay_us(40);}
 void ReadData(){
 while(!PORTD.F2); //Wait until PORTD.F2 goes HIGH
 if(PORTD.F2 == 0)    i&=~(1<<(7-b));  //Clear bit (7-b)
 else{i|= (1<<(7-b));               //Set bit (7-b)
 while(PORTD.F2);}  //Wait until PORTD.F2 goes LOW
 void main() {
 TRISB = 0;        //Configure PORTB as output
 PORTB = 0;        //Initial value of PORTB
 Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
 Lcd_Cmd(_LCD_CLEAR);             // clear LCD
  if(a == 1){
  rh1 =i;
  rh2 =i;
  t1 =i;
  t2 =i;
  sum = i;
  if(sum == rh1+rh2+t1+t2){
  text = "Temp:  .0C";
  text = "Humidity:  .0%";
  Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
  Lcd_Cmd(_LCD_CLEAR);             // clear LCD
  text = "Check sum error";
  else {
  text="No response";
  text = "from the sensor";