Thursday, November 26, 2015

Interface PIC12F675 with DHT11 digital humidity and temperature sensor

Interfacing PIC12F675 with DHT11 Sensor

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 PIC12F675 and display the results on 1602 LCD display.
Generally the Lcd display uses at least 6 data lines but we have 6 pins can be used as outputs in the PIC12F675, 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 PIC12F675 with LCD display

Interfacing PIC12F675 with DHT11 sensor circuit:
Circuit schematic diagram is shown below.
External oscillator is used (12MHz) and MCLR pin function is disabled.

pic12f675 dht11 interfacing mikroc

Interface PIC12F675 with DHT11 mikroC Code:
2-WIRE SERIAL LCD LIBRARY MUST BE INSTALLED!!

// Interface PIC12F675 with DHT11 sensor
// Serial LCD library must be installed
// Written by: BENCHEROUDA Okba
// electronnote@gmail.com
// http://elecnote.blogspot.com/
// Crystal used: 12MHz
// 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 short  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/O pins are configured as digital
  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);
  }
 }