Thursday, November 19, 2015

Interface PIC12F615 with DHT11 digital humidity and temperature sensor

Interface PIC12F615 with DHT11 and LCD display Proteus simulation

There are many topics on this blog talking about how to interface the digital humidity and temperature sensor DHT11 with different types of PIC microcontrolles and Arduino.
Simply the DHT11 is a digital humidity and temperature sensor which uses one wire to communicate with the microcontroller.
In this topic we will see how to interface this sensor with the microcontroller PIC12F615 and display the results on 1602 Lcd display.
Generally the Lcd display uses at least 6 data lines but we have 5 pins can be used as outputs in the PIC12F615, that why we have developed a new 2-wire serial Lcd display. This 2-wire Lcd uses another library which is nearly the same as the original MikroC Lcd library, this library is simple and easy to use. To see how to use 2-wire Lcd display visit the following topic:
Interface PIC12F615 with LCD display

Interface PIC12F615 with DHT11 circuit:
Circuit schematic diagram is shown below.
Internal oscillator of the microcontroller is used at 8MHz and MCLR pin function is disabled.
pic12f615 dht11 mikroc

Interface PIC12F615 with DHT11 mikroC code:

// Interface PIC12F615 with LCD display and DHT11 sensor
// Serial LCD library must be installed
// Written by: BENCHEROUDA Okba
// electronnote@gmail.com
// http://elecnote.blogspot.com/
// Internal oscillator used @ 8MHz
// Use at your own risk

  // LCD module connections
 sbit Serial_LCD_DATA at GP0_bit;
 sbit Serial_LCD_CLK at GP1_bit;
 sbit Serial_LCD_DATA_Direction at TRISIO0_bit;
 sbit Serial_LCD_CLK_Direction at TRISIO1_bit;
  // End LCD module connections
  unsigned char  Check, Temp, RH, Sum, Ch ;
void StartSignal(){
 TRISIO.F2 = 0;    //Configure TRISIO.F2 as output
 GPIO.F2 = 0;    //GP2 sends 0 to the sensor
 delay_ms(18);
 GPIO.F2 = 1;    //GP2 sends 1 to the sensor
 delay_us(30);
 TRISIO.F2 = 1;  //Configure TRISIO.F2 as input
  }
 //////////////////////////////
 void CheckResponse(){
 Check = 0;
 delay_us(40);
 if (GPIO.F2 == 0){
 delay_us(80);
 if (GPIO.F2 == 1)   Check = 1;   delay_us(40);}
 }
 //////////////////////////////
 char ReadData(){
 char i, j;
 for(j = 0; j < 8; j++){
 while(!GPIO.F2); //Wait until GP2 goes HIGH
 delay_us(30);
 if(GPIO.F2 == 0)
       i&= ~(1 << (7 - j));  //Clear bit (7-b)
 else {i|= (1 << (7 - j));  //Set bit (7-b)
 while(GPIO.F2);}  //Wait until GP2 goes LOW
 }
 return i;
 }
 //////////////////////////////
 void main() {
  ANSEL = 0;        // All I/0 pins are configured as digital
  GPIO = 0;         // Initial state of GPIO port
  TRISIO = 0;       //  Configure all available I/O pins as outputs
  Serial_Lcd_Init();                 // LCD display initialization
  Serial_Lcd_Cmd(_LCD_CURSOR_OFF);        // cursor off
  Serial_Lcd_Cmd(_LCD_CLEAR);             // clear LCD
  while(1){
  StartSignal();
  CheckResponse();
  if(Check == 1){
  RH = ReadData();
  ReadData();
  Temp = ReadData();
  ReadData();
  Sum = ReadData();
  if(Sum == (RH + Temp)){
  Serial_Lcd_Out(1, 6, "T:  C");
  Serial_Lcd_Out(2, 6, "H:  %");
  Serial_LCD_Chr(1, 8, 48 + ((Temp / 10) % 10));
  Serial_LCD_Chr(1, 9, 48 + (Temp % 10));
  Serial_LCD_Chr(2, 8, 48 + ((RH / 10) % 10));
  Serial_LCD_Chr(2, 9, 48 + (RH % 10));
  }
  else{
  Serial_Lcd_Cmd(_LCD_CLEAR);
  Serial_Lcd_Out(1, 6, "Error");}
    }
  else {
  Serial_Lcd_Cmd(_LCD_CLEAR);
  Serial_Lcd_Out(1, 6, "Error");
  }
  delay_ms(1000);
  }
 }

Interface PIC12F615 with DHT11 Proteus simulation video:


DHT11 Proteus simulation file download:
DOWNLOAD