Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

// Interfacing LM335 sensor with PIC16F887 mikroC code

// Internal oscillator used @ 8MHz


// Configuration words: CONFIG1 = 0x2CD4
// CONFIG2 = 0x0700
// http://simple-circuit.com/

// LCD module connections


sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections

char Temp_C[] = "Temp = 00.0 C";


char Temp_K[] = "= 00.0 K";
int Kelvin, Celsius;

void main() {
OSCCON = 0X70; // Set internal oscillator to 8MHz
ANSEL = 1; // Configure RA0 pin as analog (AN0)
Lcd_Init(); // Initialize LCD module
Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off
Lcd_Cmd(_LCD_CLEAR); // clear LCD

while(1) {

Kelvin = ADC_Read(0) * 0.489; // Read analog voltage and convert it to Kelvin


(0.489 = 500/1023)
Celsius = Kelvin - 273; // Convert Kelvin to degree Celsius

if(Celsius < 0){


Celsius = abs(Celsius); // Absolute value
Temp_C[7] = '-'; // Put minus '-' sign
}

else
Temp_C[7] = ' '; // Put space ' '

if (Celsius > 99)


Temp_C[7] = 1 + 48; // Put 1 (of hundred)

Temp_C[8] = (Celsius / 10) % 10 + 48;


Temp_C[9] = Celsius % 10 + 48;
Temp_C[12] = 223; // Put degree symbol
Temp_K[2] = (Kelvin / 100) % 10 + 48;
Temp_K[3] = (Kelvin / 10) % 10 + 48;
Temp_K[4] = Kelvin % 10 + 48;
lcd_out(1, 1, Temp_C);
lcd_out(2, 6, Temp_K);

delay_ms(1000); // Wait 1 second


}
}
// End of code
TOGGLE NAVIGATION

Measure Temperature and Humidity using DHT11 sensor


and PIC micro-controller
Published by MKDas on 15/05/2020
When you need to measure humidity, one sensor that comes to mind first is DHT11. This sensor
also provides temperature data. Using DHT11, we can measure humidity and temperature but as
it is a digital sensor, interfacing is not linear like an analog sensor. Here in this article, we are
going to learn how to interface a DHT11 sensor and PIC micro-controller and measure humidity
and temperature.

Disclaimer:

Electricity poses inherent risks. Adequate skills are crucial for handling it safely. Undertake tasks
at your own risk; the author disclaims responsibility for misuse, harm, or errors. Website content
is unique and copyrighted; refrain from unauthorized copying. Most articles are open-source for
your benefit. Feel free to use the knowledge freely, and consider purchasing available resources.
For assistance or guidance, comment below; the author aims to help. Some articles may contain
affiliate links, having no impact on you but supporting the author with a commission. Thank you
for your understanding.

Advertisements

Table of Contents
 About DHT11 sensor:
o Technical Specification of DHT11:
o Interfacing DHT11 with MCU:
 Circuit diagram:
 Coding:
o Code Explanation:
 Test result:
 Conclusion:
About DHT11 sensor:
The DHT11 measures relative humidity. The relative humidity is the amount of water vapor in
air vs the saturation point of water vapor in the air. At the saturation point, water vapor starts to
condense and accumulate on surfaces forming dew.

The saturation point changes with air temperature. Cold air can hold less water vapor before it
becomes saturated, and hot air can hold more water vapor before it becomes saturated.

The formula to calculate relative humidity is:

The relative humidity is expressed as a percentage. At 100% RH, condensation occurs, and at 0%
RH, the air is completely dry.
Pin Configuration of DHT11

Technical Specification of DHT11:


 Low cost
 3 to 5V power and I/O
 2.5mA max current use during conversion (while requesting data)
 Good for 20-80% humidity readings with 5% accuracy
 Good for 0-50°C temperature readings ±2°C accuracy
 No more than 1 Hz sampling rate (once every second)
 Body size 15.5mm x 12mm x 5.5mm
 4 pins with 0.1″ spacing

Interfacing DHT11 with MCU:


According to the datasheet of DHT11, the recommended connection diagram is:
This pin is bi-directional. It sends and receives data. The communication between MCU and
DHT11 is:
Communication signal pattern

Signal pattern
So we have to draw our circuit according to the datasheet of DHT11.

You can read this article too: How to interface DHT22 with PIC microcontroller
Circuit diagram:
Here is the circuit diagram for interfacing DHT11 sensor and PIC micro-controller:

Circuit Diagram

Coding:
/*******************************************************************************

Program for, "DHT11 interfacing with PIC16F877A"

Program written by_ Engr. Mithun K. Das

MCU: PIC16F877A; X-tal: 8MHz; mikroC pro for PIC v7.6.0


Date: 15-05-2020

*******************************************************************************/

// LCD module connections

sbit LCD_RS at RB2_bit;

sbit LCD_EN at RB3_bit;

sbit LCD_D4 at RB4_bit;

sbit LCD_D5 at RB5_bit;

sbit LCD_D6 at RB6_bit;

sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;

sbit LCD_EN_Direction at TRISB3_bit;

sbit LCD_D4_Direction at TRISB4_bit;

sbit LCD_D5_Direction at TRISB5_bit;

sbit LCD_D6_Direction at TRISB6_bit;

sbit LCD_D7_Direction at TRISB7_bit;

// END of LCD initialization

sbit Data at RB1_bit;

sbit DataDir at TRISB1_bit;

char message[] = "00.0";

unsigned short TOUT = 0, CheckSum, i;

unsigned int humidity,value;

unsigned int T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;

void StartSignal()

DataDir = 0; // Data port is output

Data = 0;

Delay_ms(25);
Data = 1;

Delay_us(30);

DataDir = 1; // Data port is input

unsigned short CheckResponse()

TOUT = 0;

TMR2 = 0;

T2CON.TMR2ON = 1; // start timer

while(!Data && !TOUT);

if (TOUT) return 0;

else

TMR2 = 0;

while(Data && !TOUT);

if (TOUT) return 0;

else

T2CON.TMR2ON = 0;

return 1;

unsigned short ReadByte()

unsigned short num = 0, t;

DataDir = 1;
for (i=0; i<8; i++)

while(!Data);

Delay_us(40);

if(Data) num |= 1<<(7-i);

while(Data);

return num;

void Interrupt() iv 0x0004 ics ICS_AUTO

if(PIR1.TMR2IF)

TOUT = 1;

T2CON.TMR2ON = 0; // stop timer

PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag

void main()

// port initialization

TRISB = 0b00000010;

PORTB = 0;

CMCON = 7;// comparator off

ADCON1 = 0x07;// ADC off

INTCON.GIE = 1; //Enable global interrupt

INTCON.PEIE = 1; //Enable peripheral interrupt


// Configure Timer2 module

PIE1.TMR2IE = 1; // Enable Timer2 interrupt

T2CON = 0; // Prescaler 1:1, and Timer2 is off initially

PIR1.TMR2IF =0; // Clear TMR INT Flag bit

TMR2 = 0;

Lcd_Init();

Lcd_Cmd(_LCD_CLEAR);

Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_Out(1,1,"DHT11 WITH");

Lcd_Out(2,1,"PIC16F877A");

Delay_ms(2000);

Lcd_Cmd(_LCD_CLEAR);

while(1)

unsigned short check;

PIE1.TMR2IE = 1; // Enable Timer2 interrupt

ADCON1 = 0x07; // all digital selected

StartSignal();

check = CheckResponse();

if (!check)

Lcd_Cmd(_LCD_CLEAR);

Lcd_Out(1, 1, "No response");

Lcd_Out(2, 1, "from the sensor");

else

{
RH_Byte1 = ReadByte();

RH_Byte2 = ReadByte();

T_Byte1 = ReadByte();

T_Byte2 = ReadByte();

CheckSum = ReadByte();

// Check for error in Data reception

if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))

Lcd_Out(1,1,"HUMIDITY:");

Lcd_Out(2,1,"TEMP:");

message[0] = RH_Byte1/10 + 48;

message[1] = RH_Byte1%10 + 48;

message[3] = RH_Byte2%10 + 48;

Lcd_Out(1,11, message);

Lcd_Out(1,16,"%");

message[0] = T_Byte1/10 + 48;

message[1] = T_Byte1%10 + 48;

message[3] = T_Byte2%10 + 48;

Lcd_Out(2,11, message);

Lcd_Chr_CP(223);

Lcd_Out(2,16,"C");

Delay_ms(2000);

else

Lcd_Cmd(_LCD_CLEAR);

Lcd_Out(1, 1, "Checksum Error!");


Lcd_Out(2, 1, "Trying Again...");

PIE1.TMR2IE = 0; // disable Timer2 interrupt

}// while(1)

}// void

You might also like