An automatic temperature control system has the ability to monitor and control the temperature of a specified space without human intervention. The primary purpose is to manage the temperature of a given area based on settings by a user of the system.

Figure 1: Automatic Temperature Control Block diagram

This project uses a PIC microcontroller to automatically control the temperature of an area. This area could be a small plant, a house or any place or device that require a controlled temperature like an incubator (egg) for example. Figure 1 shows the block diagram of the system to be designed. The desired temperature setting is entered using a keypad. The temperature of the area is measured using an analog temperature sensor, the LM35 precision integrated-circuit temperature sensor is used for this.

The microcontroller reads the temperature every 10 s and compares it with the desired value. If the desired value is higher than the measured value, then the heater is turned ON to heat the area. The heater is switched OFF once the desired temperature is reached. If on the other hand the measured value is higher than the desired value, then the fan is switched ON to cool off the area until the required temperature is reached. An LCD display shows the measured temperature continuously.

Figure 2 shows the circuit diagram of the project. The LCD is connected to PORTC. The LM35 precision analog temperature sensor chip is connected to the analog input pin AN0 (RA0). A 3×4 keypad is connected to PORTB. The ‘*‘ key of the keypad is used to clear the value entered during the temperature setup and the ‘#‘ key is used to ENTER (save) the setting. The heater and the fan are controlled using transistors and relays connected to pins RD0 and RD1 of the microcontroller respectively.

Figure 2: Automatic Temperature Control Circuit diagram

Important:

The Terminals ratings of the relay should depend on the power of the Heater and the Fan. If you decide to use 220V Heater and Fan, use appropriate relays which can handle that voltage and current. The low voltage DC of the coil should be preferably 5V and with low current for the BC108 transistor to handle, or you can use a different transistor. Please observe the safety precaution as 220V (or 110V if you are living in the USA) is very dangerous, if you have never worked with high voltage before, please seek assistance, don’t attempt to do it on your own.

MikroC Source Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

/*

* Project name: Automatic Temperature Control

* Copyright:

* (c) www.studentcompanion.co.za, 2014.

* Test configuration:

* MCU: PIC18F45K22

* http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf

* Oscillator: HS-PLL, 32.00000 MHz

* Compiler mikroC Pro for PIC v6.5.0

*/

// Keypad module connections

charkeypadPort at PORTB;

// End Keypad module connections

// LCD module connections

sbit LCD_RS at LATC4_bit;

sbit LCD_EN at LATC5_bit;

sbit LCD_D4 at LATC0_bit;

sbit LCD_D5 at LATC1_bit;

sbit LCD_D6 at LATC2_bit;

sbit LCD_D7 at LATC3_bit;

sbit LCD_RS_Direction at TRISC4_bit;

sbit LCD_EN_Direction at TRISC5_bit;

sbit LCD_D4_Direction at TRISC0_bit;

sbit LCD_D5_Direction at TRISC1_bit;

sbit LCD_D6_Direction at TRISC2_bit;

sbit LCD_D7_Direction at TRISC3_bit;

// End LCD module connections

#define HEATER PORTD.RD0

#define FAN PORTD.RD1

#define ENTER 15

#define CLEAR 13

#define ON 1

#define OFF 0

voidmain(){

unsignedshortkp,Txt[14];

unsignedshortTemp_Ref;// Reference Temperature

unsignedcharinTemp;

unsignedinttemp;

floatmV,ActualTemp;

Keypad_Init();// Initialize Keypad

ANSELC=0;// Configure PORTC as digital I/O

ANSELB=0;// Configure PORTB as digital I/O

ANSELD=0;// Configure PORTD as digital I/O

TRISA0_bit=1;//Configure AN0 (RA0) as input

TRISC=0;//PORTC are outputs (LCD)

TRISD0_bit=0;//RD0 is output (Heater)

TRISD1_bit=0;//RD1 is output (Fan)

Lcd_Init();// Initialize LCD

Lcd_Cmd(_LCD_CLEAR);// Clear display

Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor off

Lcd_Out(1,4,"Automatic");

Lcd_Out(2,2,"Temp Control");

delay_ms(2000);//2s delay

HEATER=OFF;

FAN=OFF;

//ON startup, read the Referance Temperature from the Keypad

START:

Lcd_Cmd(_LCD_CLEAR);// Clear display

Lcd_Out(1,1,"Enter Temp Ref");

Temp_Ref=0;

Lcd_Out(2,1,"Temp Ref: ");

while(1)

{

do

kp=Keypad_Key_Click();// Store key code in kp variable

while(!kp);

if(kp==ENTER)break;

if(kp>3&&kp<8)kp=kp-1;

if(kp>8&&kp<12)kp=kp-2;

if(kp==14)kp=0;

if(kp==CLEAR)gotoSTART;

Lcd_Chr_Cp(kp+'0');

Temp_Ref=(10*Temp_Ref)+kp;

}

Lcd_Cmd(_LCD_CLEAR);// Clear display

Lcd_Out(1,1,"Temp Ref: ");

intToStr(Temp_Ref,Txt);//Convert to String

inTemp=Ltrim(Txt);

Lcd_Out_CP(inTemp);//Display Ref Temp

Lcd_Out(2,1,"Press # to Cont.");

//Wait until # is pressed

kp=0;

while(kp!=ENTER)

{

do

kp=Keypad_Key_Click();// Store key code in kp variable

while(!kp);

}

Lcd_Cmd(_LCD_CLEAR);// Clear display

Lcd_Out(1,1,"Temp Ref: ");

Lcd_Chr(1,15,223);// Different LCD displays have different

// char code for degree

Lcd_Chr(1,16,'C');// Display "C" for Celsius

//Program loop

while(1){

//Display Referance Temperature and Actual Temperature

temp=ADC_Read(0);//Read temperature from AN0

mV=temp *5000.0/1024.0;//Convert to mV

ActualTemp=mV/10.0;// Convert to degrees Celcius

intToStr(Temp_Ref,Txt);//Convert to String

inTemp=Ltrim(Txt);

//Lcd_Out(1, 1, "Temp Ref: ");

Lcd_Out(1,11,inTemp);//Display Ref Temp

Lcd_Out(2,1,"Temp= ");

FloatToStr(ActualTemp,Txt);//Convert to string

Txt[4]=0;

Lcd_Out(2,7,Txt);

Lcd_Out(2,12," ");

//Compare ref temp with actual emp

if(Temp_Ref>ActualTemp)//If Temp Ref is less than actual Temp, Switch ON Heater

You can try but you’ll have to limit some few features of this project. The PIC16F877A has 14KB of program memory and this project has about 15KB i size.

Emil Leones

Thanks for responding .. 😀 i have another question if you may. We used the same code for the program and it works in Proteus but why does the keypad not responsive in hardware/actual circuit? We checked the connections but still it doesn’t work.

You can. You’ll have to select your PIC in the project and change your oscillator frequency to match the one of your pic kit if this one operates with a different frequency than 8MHz. And recompile the project.

Mohd Hafirulddin

im newbie about the pic programmer, im using your project for my university final year project…i need more well explaination…how to change the oscillator frequency?

Okay sir, thanks for the guide. Im going to try it. so when I change the PIC, how about the wire connection? how im going to reconnect with the new PIC, different PIC got a different wire connection right?

we are desperately need your help sir, we have less a month before our final year project exhibition. can you help us recoding and make schematic for the same project but with another pic. we are using pic kit UP00B. there are certain pic cant be detect on that kit including pic18f45k22.

JC Pineda

Hi! We used the same code for the program and it works in Proteus but why does the keypad not responsive in hardware/actual circuit? We checked the connections but still it doesn’t work.

Mohd Hafirulddin

sir, what type of pickit do you for coding the pic18f45k22?

JC Pineda

Hi Sir! We are having a problem with the actual circuit. We used the same program and connections but the LCD displays 333333333333333333333333333. What could be the problem? Thank you!

You can use the PIC18F4520:
1. In project settings, change the PIC to PIC18F4520
2. Delete the ANSEL statements and replace them with ADCON1=0x0D; //This is to configure all PORTS as digital except AN0 and AN1.
3. Rebuild your project.

Jackson Chung

So sir, can I change both the heater and fan into LED as the output? If can what I need to do?

If you want to use assembly code, than you can change the code yourself, we don’t support assembly as we don’t even have one article in assembly.
The line of code ADCON1=0x0D; replaces all the ANSEL statements. All of them.

Sir, I want to ask you about the simple code programming if you can make a program just like a circuit above using assembly source (asm) based on PIC18F458 microcontroller because I’m looking for it to studying for my exam test. Please help me sir. I’m appreciate for your effort to help me to become pass for my exam test. Thanks.

i really appreciated your time for me. i did the proteus becusea when i download your proteus schematic does not have file.pdsprj or i do not know where is it? but the code works with me. my problem when i run proteus and i give the keypad any number it’s usually show 27 and the fan will work and after 15 second the proteus program will close. please advice me what should i do ?

Did you watched the videos tutorials on this article as well? Because we explained everything step by step in the video. What compiler are you using? Because we saw you raised questions on the MPLAB XC8 version of this project now you are on mikroC.

Naif Al Ghamdi

yes i watched it and i used mikroC, my questions was on the MPLAB XC8 before i started a project but i decided to use mikroC. also, when i run your code on mikroC the massage showed me demo limit and finished(with errors), but i downloaded your file of code and i saw file.hex and run by proteus. i’m sorry for my a lot of questions, but i really need the project.

This project is more than 2KB of size that mikroC allows you with their free version of compiler. You’ll have to buy a licence. But you can use MPLAB XC8 as it’s completely free but with code optimization for licensed users.

Naif Al Ghamdi

ok that’s why i have the error. thank you so much i’ll buy it and try it again

Obviously it’s gonna depend on your circuit and what you want to achieve and whether you are using the USB peripheral, or using MCLR or whatever fuse you want to activate like WDT, BOR and so on. Generate your configuration bits according to your need as we explained in this tutorial, you can also watch the video on this page to learn more: http://www.studentcompanion.co.za/introduction-to-mikroc-pro-for-pic-compiler/

Hi.
I see some few problems in your code:
1. LCD module connections:
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;

You are selecting to use LCD pins RS at RB4 and EN at RB5 but your proteus circuit you connected RS at RB5 and EN at RB4. it won’t work.

2. OSCCON = 0X70; // Set internal oscillator at 8MHz
You don’t need to configure this OSCCON register because you are using an external oscillator
3. ADCON1 = 0x0F; // Configure AN pins as digital
by setting ADCON1 == 0x0F will set all analog pins as digital pins. How are you going to read your analog sensor on AN0 if all pins are set as digital? see datasheet on page 266
4. You connected your keypad on PORTD but this port TRIS register is not configured.

amirul

hai sir ! i want to ask a question .. can i use pic16F877A ? and can i use the your programming using pic16F877A ?

Yes you can use it, but you will have to modify the code a bit. You must remove the ANSEL register and configure your PIC with ADCON as the PIC16F877A does not have ANSEL register. To write to a PORT, you must also use PORT register instead of LAT register as PIC16F series don’t have LAT register.

I am sure you can, you just have to configure correctly your Input/Output pins correctly as you know the pic16F1937 has a built-in LCD display on PORTC, you also have to configure well your clock settings

The problem looks like it’s a hardware problem the way you are describing it. We can’t help you with that as you are not even descriptive. Can you get even one thing working? even displaying the initial message on LCD?

faiz abdalkareem

initial message is ok.enter ref.temeperature ok too.by touching differant pin of 18f i enter ref. temperature.fan and heater controlling the temperature very good.the proplem is keypad did not working.thank you sir.

Hi Sir! When I put the temperature from key for example 12 and at the sensor it s 12 also,the fan or the heater are still working.Why it s happen like that, because in the C code we have” if (Temp_Ref == ActualTemp) //If Temp Ref is equal to actual Temp, Switch OFF Fan and Heater

Hello Dear sir! I would like to ask a help from you. I Have the same project like this, the control of temperature by presence but to compilate is difficult to me and i know only MicroC pro for PIC. I don’t have a licensed MicroC, if you can help me to compilate and send it to this adresse: rodriguekiyiro@gmail.com . My source code is here below:

What have you tried then failed? You haven’t posted your errors, you just asked for a schematic and we advised it’s more or less the same as this one. Just download our schematic and replace the PIC with the one you are using. What are you struggling with?

nandan patil

i want the code of this project for MPLAB ide software.you can send code of this project on nandanpatil845@gmail.com mailid

The ratings of the relays will depend on the ratings of the fan and the heater.

David Bagashie

Dear Sir,
My actual design is not working but the proteus simulation is working. Please what could be the problem? What have I done wrong? I cross checked all the connections over and over again and found them okay.

Your question is vague, is everything not working or only the functionality? If nothing at all is displayed on the LCD then it can only be your hardware the way you built it.
Try with simple stuff, like just blinking an LED to make sure your microcontroller is working, receiving correct voltage and correct oscillator clock. Then you can test the functionalities of this project.
We can’t help you in this scenario as we don’t know what could be wrong, it could be anything…
Many people have designed this project especially students, they even sent us photos of the working hardware on facebook

David Bagashie

Thank you for quick reply,

I had the display working now. The problem was the dry joint on EN pin of LCD. However, I have another problem with the character displayed .eg. 333331111333 re-occurring numbers. Also some of the Keypad keys are not working. Please what should I do?

Hi Sir , can I know the problems when I try to edit “Automatic Temperature Control ” to my own words such as ” Welcome To Temperature Controll” ,then I convert it to Hex file using MikroProgSuite for Pc but my words doesn’t appear and still with your programming words “Automatic Temperature Control” when its running/ I try to simulate it again.. then the Hex file that I created also 92kb while your hex file is only 15kb ,I just modified their words only ,In your tutorial video2 also you not show how you convert the c file to the hex file … I hope you will help me,Tq very much ^^

Hi. That is very strange. When you compile your code, does it compile successfully without any errors?

Nelson Sam Martin

Dear sir, can you help me … my problem is because I used the mikroC cracked version so when I try to compile it ,the hex file does not generated because there is demo limit when using the cracked version ones. Do you know other alternative to solve this problem or any recomended apps to compile this code ? ,Tq for response

Hallo sir , can you teach how to program a max limit of the ref temp that can be set up by user let sey <40 only …I just want to modify a little bit about this project… I am sorry sir because I'am a beginner of learning this C languange 🙂

Hi. Everything is explained step by step in the tutorial, especially the video tutorial including the setting of maximum. Watch carefully the video tutorial and if need be, watch the previous tutorials as well so that you can better understand this tutorial. Unfortunately it doesn’t work to skip steps. If yo are beginner, please watch the previous videos first.

Vincent Lam Heng

Can you give me the link of that tutorial video please , (setting of maximum) I tried to find it but not found.

Dear sir,
Can I know this project is using Ac/Dc?
Where is it power supply based on this circuit?
Then if i replace the 12V heater with the 120V and 150W heater and using a 12V relay switch , does the new heater will turn on/ running ?

The PIC microcontroller in this project requires 5V for its operation, whether you are going to get it from DC or AC through an external power supply, it’s up to you. The external power supply is not shown here. As for the heater and relay, please read the important notes we highlighted in red in this article.

RAOUF RAOUF

I did not understand the operation of the relays?
Is what it is of the transformer,
The trasistor circuit that is common emitter

That’s how relays work. A low voltage powering the relay coil, can switch ON/OFF high voltage relay contacts. In our case a 5V relay is used to control 12V devices.
Watch this video to learn how relays work: https://youtu.be/WodS7NA1NlY

Dominic Toretto

Hi Sir. good day.. Can i use Pic16f877a as the microcontroller?. Thank you:) Im an electronics engrng student and we usually use 877a as the microcontroller and i would like to do your proj as our thesis with your consent 🙂

Hi. Yes you can use PIC16F877A. We also give you consent t use this project as long as you will incluse our website in your references.
You’ll have to make some small modification for this project to work, like changing LAT to PORT, removing the ANSELx with appropriate configuration.

Dominic Toretto

Hi sir good day! 🙂 is ADCON the appropriate config for pic877a instead of ANSEL? it is use for disabling analog i/o and for digital i/o only? Please explain sir thaaank you so much

Hi. PIC16F877A does not have ANSEL registers. it will give you error messages if you use ANSELC = 0 or ANSELB=0. You must use ADCON1 instead if you want to configure these pins as digital pins. You know you can’t connect a keypad or a relay to an analog pin, it must be digital.
In this project, you’ll need only one pin as analog, the pin where your temperature sensor is connected to.
You can read these comments by other users, they asked the same question and we explained to them how to configure the ADCON1. or you can read the PIC16F877A datasheet.

Hi sir! 🙂 I would like to ask if how can we troubleshoot the keypad on the actual ckt, because as we pressed the keypad buttons they are not scanning on our 4×20 lcd..

Charles Chilumbu

What changes need to be done to the code to make the user set humidity (with capability for the system to alarm if the humidity falls bellow preset value) and also number set of days of incubation (with capability for the system to show remaining number days to hatching)?

sorry, i can sign up this account for comment in forum. its too difficult. im lost when getting password. so, i want to ask. is it possible to change the microcontroller and also the software we used such as arduino. and for pic, i dont know how to make a change the circuit. pls help me. im doing this because its for my project. but i want to use arduino. thats matter. sorry

Stay Update:

Search Here

Translate Website into your Language

Please support our work with a Donation

Writing tutorials and recording tutorials videos requires a lot of resources and time. If you found these tutorials helpful, please show your support and donate any amount you wish so that we can do even more.
Thanks