Monday, January 12, 2015

Interface DHT11 digital humidity and temperature sensor with PIC16F84A

Interface DHT11 digital humidity and temperature sensor with PIC16F84A
This topic shows how to interface digital humidity and temperature sensor DHT11 with the old microchip pic16f84a. To see about this sensor please visit this topic:
and this topic to understand the code:
The complete schematic diagram of the circuit is shown below:
If there is no device connected to the circuit or there is a check sum error the lcd will display "ERROR".
Interface DHT11 sensor with PIC16F84A circuit:
pic16f84a dht11 mikroc

dht11 mikroc

Interface DHT11 sensor with PIC16F84A mikroC code:

//Interface of DHT11 with PIC16F84A MikroC code
//Please read the datasheet of DHT11 sensor to understand the code
//Written by: BENCHEROUDA Okba
//electronnote@gmail.com
//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
 unsigned char *text,mytext[4];
 unsigned short  a, b, i, t, rh;

 void StartSignal(){
 TRISB.F6 = 0;    //Configure RB6 as output
 PORTB.F6 = 0;    //RB6 sends 0 to the sensor
 delay_ms(18);
 PORTB.F6 = 1;    //RB6 sends 1 to the sensor
 delay_us(30);
 TRISB.F6 = 1;    //Configure RB6 as input
  }
 void CheckResponse(){
 a = 0;
 delay_us(40);
 if (PORTB.F6 == 0){
 delay_us(80);
 if (PORTB.F6 == 1)   a = 1;   delay_us(40);}
 }
 void ReadData(){
 for(b=0; b<8; b++){
 while(!PORTB.F6);                   //Wait until PORTB.F6 goes HIGH
 delay_us(30);
 if(PORTB.F6 == 0)  i&=~(1<<(7-b));   //Clear bit (7-b)
 else{i|= (1 <<( 7 - b));             //Set bit (7-b)
 while(PORTB.F6);}                    //Wait until PORTB.F6 goes LOW
 }
 }
 void error(){
 text="ERROR";
  Lcd_Out(1,6,text);
  }
 void main() {
 TRISB = 0;        //Configure PORTB as output
 PORTB = 0;        //Initial value of PORTB
 Lcd_Init();
 while(1){
 Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
 Lcd_Cmd(_LCD_CLEAR);             // clear LCD
  StartSignal();
  CheckResponse();
  if(a == 1){
  ReadData();
  rh = i;
  ReadData();
  ReadData();
  t = i;
  ReadData();
  ReadData();
  if(i = rh + t){
  text = "Temp:  .0C";
  Lcd_Out(1, 4, text);
  text = "Humi:  .0%";
  Lcd_Out(2, 4, text);
  ByteToStr(t,mytext);
  Lcd_Out(1, 9, Ltrim(mytext));
  ByteToStr(rh,mytext);
  Lcd_Out(2, 9, Ltrim(mytext));}
  else error();
  }
  else error(); 
  delay_ms(2000);
  }
  }


Interface DHT11 sensor with PIC16F84A video: