Automatic Temperature Control System using PIC Microcontroller – MikroC

Automatic Temperature Control with PIC

Automatic Temperature Control System using PIC Microcontroller – MikroC

Watch the Video Tutorial part 1:

Automatic Temperature Control Using PIC Microcontroller 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.

Automatic Temperature Control Block Diagram

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 continuously 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. If the temperature reaches a certain critical value 40⁰C or higher, the buzzer will sound continuously and an LED will blink until the temperature deceases below 40⁰C.

An LCD display shows the measured temperature continuously.

This project is complete and can be used as a base for Final Year Project For Engineering Students.

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 to PIC internal EEPROM. The heater and the fan are controlled using transistors and relays connected to pins RD0 and RD1 of the microcontroller respectively.

When the device starts, it reads the Reference Temperature from PIC internal EEPROM, if there is no value saved, it will prompt the user to enter a new Reference Temperature and save it to PIC internal EEPROM. In operation you can press and hold the ‘*‘ for 3 seconds to access the setup menu again if you need to set a new reference temperature.

Automatic Temperature Control Circuit diagram

Figure 2: Automatic Temperature Control Circuit diagram

Note: In hardware design it’s always a good practice not to leave unused pins floating. You can set them as output and connect them to ground preferably via a pull-down resistor to avoid EM interference. Like the unused RB3 pin can be a good candidate.


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.

Buy the components used in this Project form our Online Store

MikroC Source Code

 * Project name:  Automatic Temperature Control
 * Copyright:
     (c), 2018.
 * Test configuration:
     MCU:             PIC18F45K22
     Oscillator:      HS-PLL, 32.00000 MHz

// Keypad module connections
char  keypadPort 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 FAN PORTD.RD1
#define LED PORTD.RD3
#define ENTER 15
#define CLEAR 13
#define ON 1
#define OFF 0

void main() {
unsigned short kp,Txt[14];
  unsigned int Temp_Ref ;      //  Reference Temperature
  unsigned char inTemp;
  unsigned int temp;
  float mV, ActualTemp;

  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)
  TRISD3_bit=0;               //RD3 is output (LED)
  TRISB3_bit=0;               //RB3 is output (Unconnected)
  LATB.B3=0;                 //Drive low RB3
  Keypad_Init();               // Initialize Keypad
  Lcd_Init();                  // Initialize LCD
  Sound_Init(&PORTD, 2);     // Initialize the pin RD2 for playing sound

  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

  FAN = OFF;

 //ON startup, read the Referance Temperature from EEPROM or the Keypad
 //Read Temp Ref from EEPROM
        Temp_Ref= EEPROM_Read(0x02); // Read data from EEPROM address 2 and store it in Temp_Ref variable
        if ((Temp_Ref > 0) & (Temp_Ref < 100))
      goto START_PROGRAM ;
    else {
       Lcd_Cmd(_LCD_CLEAR);                     // Clear display
       Lcd_Out(1, 1, "Enter Temp Ref");
        Lcd_Out(2, 1, "Temp Ref: ");
        //Setup new Temp Ref
           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 )goto START;
         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
      Lcd_Out_CP(inTemp);          //Display Ref Temp
      EEPROM_Write(0x02,Temp_Ref); // Write Temp_Ref at address 2 of EEPROM
      Lcd_Out(2, 1, "Press # to Cont.");
      //Wait until # is pressed
      kp =0;
           kp = Keypad_Key_Click();  // Store key code in kp variable

   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/1023.0;        //Convert to mV
     ActualTemp = mV/10.0 ;             // Convert to degrees Celcius
     intToStr( Temp_Ref,Txt);         //Convert to String
     //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,12,"   ");

     //Compare ref temp with actual emp
      if (Temp_Ref >  ActualTemp)  //If Temp Ref is greater than actual Temp, Switch ON Heater
      HEATER = ON,
      FAN = OFF;
       if (Temp_Ref <  ActualTemp)  //If Temp Ref is less than actual Temp, Switch ON Fan
      HEATER = OFF,
      FAN = ON;

      if (Temp_Ref ==  ActualTemp)  //If Temp Ref is equal to actual Temp, Switch OFF Fan and Heater
      HEATER = OFF,
      FAN = OFF;

        //Check if the '*' key is pressed

         kp = Keypad_Key_Press(); // Store key code in kp variable
         if ( kp == CLEAR )
         delay_ms(3000);          //3s delay
         kp = Keypad_Key_Press(); // Store key code in kp variable
         if ( kp == CLEAR ) 
         goto START;
         //Sound the buzzer
      if (ActualTemp >=  40)  //If Temp reaches critical temperature of 40.
   Sound_Play(880, 300); // Play sound at 880Hz for 300ms

      LED = ~LED ;         //Blink LED
      delay_ms(200);          //200ms delay
      LED = OFF;


If you want to use MPLAB XC8 compiler, you may be interested in this project:

Automatic Temperature Control System with MPLAB XC8 compiler

Watch the Video Tutorial part 2: 

Watch the Video Tutorial part 3: 

Schematic design Using EAGLE

Any PCB Schematic design software can be used. In this project we used EAGLE due to its simplicity and free (the free version has some limitations but for simple 2 layer boards like this one, those limitations won’t affect us). 

PCB design in EAGLE is a two-step process. First we’re gonna design the schematic, then lay out the board based on the schematic.

Start with a new EAGLE Project. In the control panel, under the “Projects” tree, right click on the directory where you want to save your project, we prefer to use the default EAGLE directory. Select “New Project”. Give it a meaningful name.

To add a schematic to a project folder, right-click your project folder, hover over “New” and select “Schematic”. Add the parts using the the ADD Tool and design a schematic like the one on figure 3 below.

Automatic Temperature Control Schematic in EAGLE

Figure 3: Automatic Temperature Control Schematic in EAGLE

Board Layout Using EAGLE

From the schematic editor click the Generate/Switch to Board command in the toolbar or Switch to Board under the File menu. This will prompt to create a new board based on the schematic when switching to board from the first time. All of the parts you added from the schematic should be there, stacked on top of each other, ready to be placed and routed.

Place all the parts using the MOVE tool on the blank board and route all the airwires. The board is small can be easily routed manually using the ROUTE tool.

Automatic Temperature Control Board Layout in EAGLE

Figure 4: Automatic Temperature Control Board Layout in EAGLE

We routed all the traces on Top layer (Red colour) and created a ground plane on the Bottom layer (Blue colour) by adding a Copper Pour using the POLYGON tool. add some Text on Top and Bottom Silk layers.

Generating The Gerber Files

Now we have reached the final stage of the PCB design, it’s time to generate the Gerber files that we can send to our manufacturing house to build our boards.
Gerber files contain the data about the PCB, where traces, pads, holes are placed, their width and so on. You can generate Gerber files in Eagle by clicking on the CAM processor icon on the toolbar. But there is another easier way to generate Gerber files online from an Eagle board file from PCBWay, the company we use to manufacture our Printed Circuit Boards. Just upload your Eagle board file *.brd to this link: , their online converter supports Eagle boards from 1 to 10 layers. These are the three easy steps you need to follow:

  1. Click on the Browse for files button to upload your EAGLE board (.brd) file.
  2. Click the Run the conversion button to generate Gerber files from your board. There is also a Gerber viewer, this will give you one last chance to analyse your PCB to make sure that everything is correct before you send it to your manufacturing house.
  3. Click on the Download Gerber button to download your Gerber files in a zipped format. The figure 5 below illustrates PCBWay, online Gerber converter.

PCBWay online Gerber converter

Figure 5: PCBWay online Gerber converter

Manufacture the PCB

In any electronic design, the Printed Circuit Board or PCB is one of the most crucial parts with its quality affecting the overall quality of all these devices. For rapid prototyping or for commercial products it’s always a must to use specialized reputable PCB manufacturer instead of doing it yourself.

There are many PCB manufacturing companies in the world that can make you good quality boards, but to find a company that can produce cheap boards of high quality on small order because the first batch of any project will likely be of small quantity and only after you have tested successfully everything and you are happy it performs all its functions as required then you can order boards at high quantity at much small unit cost. You’ll need to select a company specializing in prototype PCB. One of them that we used is called: PCBWay

PCBWay Home page

Figure 6: PCBWay Home page is a China Shenzhen-based manufacturer specializing in PCB prototyping, small-volume production and PCB Assembly service all under one roof with more than a decade experience. They offer quick turnaround PCBs at a very budgetary price. Thousand of engineers, students and hobbyists are using their PCBs for their daily work and study. You can get 10 PCBs for only $5. If you are an Engineering Student or an Educator, you can get your PCB for free from PCBWay with their Students Projects Sponsorship program. For more information on Sponsorship program please click here:

From PCBWay  home page: ,you can use the free online quote system to get your price instantly and immediately after you input a few parameters online. You’ll need first to create an account with them for free by clicking on the “Join” on top right corner as shown on figure 6 above.

Below on figure 7 is the Printed Circuit Board with components in 3D

Automatic Temperature Control Printed Circuit Board with components in 3D

Figure 7: Automatic Temperature Control Printed Circuit Board with components in 3D

You can download the Gerber files of this project for free or you can simply order the PCB of this project from this link:

Each time a person orders this PCB, we will get 10% commission of the total PCB cost. That’s how you can also support us for more tutorials.

Please share, like and vote for this project.

Watch the Video Tutorial part 4:

You can also download the full project files (MikroC source code and Proteus Schematic design ) below here.
All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).

MikroC Source Code: Automatic Temp Control mikroC Project

Proteus Schematic: Automatic Temp Control mikroC Proteus Schematic

Share this post

You've just added this product to the cart:

Select your currency
ZAR South African rand
EUR Euro