#include <built_in.
h>
// Define pins for LCD
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// Relay control pins
sbit Heater_Relay at RC0_bit;
sbit Fan_Relay at RC1_bit;
// Function prototypes
float readTemperature();
void displayData(float temperature, float humidity);
void DHT11_Start();
unsigned char DHT11_Read();
void main() {
float temperature, humidity;
// Initialize LCD
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
// Set relay pins as output
TRISC0_bit = 0; // Heater relay
TRISC1_bit = 0; // Fan relay
while(1) {
// Read temperature from LM35
temperature = readTemperature();
// Read humidity from DHT11
humidity = DHT11_Read();
// Control heater and fan based on temperature
if (temperature < 32.0) { // Set your desired temperature
Heater_Relay = 1; // Turn on heater
Fan_Relay = 0; // Turn off fan
} else if (temperature > 34.0) {
Heater_Relay = 0; // Turn off heater
Fan_Relay = 1; // Turn on fan
} else {
Heater_Relay = 0; // Turn off heater
Fan_Relay = 0; // Turn off fan
// Display data on LCD
displayData(temperature, humidity);
Delay_ms(1000); // Delay for stability
float readTemperature() {
// Read temperature from LM35
int adcValue = ADC_Read(0); // Assuming LM35 is connected to AN0
return adcValue * (5.0 / 1023.0) * 100; // Convert ADC value to temperature
void displayData(float temperature, float humidity) {
char txt[16];
Lcd_Out(1, 1, "Temp: ");
FloatToStr(temperature, txt);
Lcd_Out(1, 7, txt);
Lcd_Chr(1, 13, 223); // Degree symbol
Lcd_Chr(1, 14, 'C');
Lcd_Out(2, 1, "Humidity: ");
IntToStr((int)humidity, txt);
Lcd_Out(2, 10, txt);
Lcd_Chr(2, 12, '%');
void DHT11_Start() {
TRISD0_bit = 0; // Set pin as output
RD0_bit = 0; // Pull pin low
Delay_ms(18); // Wait for 18 ms
RD0_bit = 1; // Pull pin high
Delay_us(20); // Wait for 20 us
TRISD0_bit = 1; // Set pin as input
unsigned char DHT11_Read() {
unsigned char i, j, data[5] = {0, 0, 0, 0, 0};
DHT11_Start(); // Start signal
// Wait for response
while (RD0_bit); // Wait for pin to go low
while (!RD0_bit); // Wait for pin to go high
// Read 40 bits of data
for (j = 0; j < 5; j++) {
for (i = 0; i < 8; i++) {
while (!RD0_bit); // Wait for pin to go high
Delay_us(30); // Wait