Temperature Control Using PIC16F877A

61
1 Mechatronics Design Assignment Autonomous Liquid Temperature Controlling System

Transcript of Temperature Control Using PIC16F877A

Page 1: Temperature Control Using PIC16F877A

1

Mechatronics Design

Assignment

Autonomous Liquid Temperature Controlling System

Page 2: Temperature Control Using PIC16F877A

2

Contents:

Introduction 3

Project Design 5

Project Working 31

Testing and Result 52

Discussions 60

References 60

Appendix 61

Page 3: Temperature Control Using PIC16F877A

3

Introduction:

We built an autonomous liquid temperature control system for our Mechatronics design. The

design is fully autonomous and mainly aimed at the controlling action without human

intervention. Our design can be used by companies as heat exchangers or liquids temperature

regulators.

In chemical companies (mainly but can be used at homes, offices, hospitals and schools to

control the liquid ‘s temperature depending upon the desired value assigned) various liquid

substances are needed to be cooled down to a particular level before being further processed, this

is the place where we can implement our design as by controlling the values of a heat exchanger

(heat exchanger is a device which is used to regulate the heat of two substances using the

concept of heat transfer, it has 2 parts, the inner part is made up of many thin tubes through

which hot liquids flow, through the outer part cooler water or liquid flows, heat is exchanged

between two different substances, hot substance loses its heat to the cooler substances) and also

the design can be used to get a regulated or desired temperature output (liquid), without using the

heat exchanger, which is the temperature of the liquid is maintained by mixing cooler liquid the

hot liquid (same).

For our assignment we would be focusing mainly on the second part which is liquid’s

temperature regulator, whose main function is maintaining the temperature of liquid in a vessel,

below a certain range of temperature using the 2 LM35DZ temperature sensors (they are the ones

which detects the temperature change in the 2 liquids vessels, 1 RC servo motor (required to

open and close the valve or clit for allowing the liquid to flow from one vessel to other), 1 DC

geared motor (required to cool down the liquid as by attaching a fan or a stirrer to it), PIC

16F877A (the brains of the system which decides when to open the valve and allow the cooling

process to start and stop) and 16x2 LCD (which is used to display the temperatures in the 2

different vessels).

Page 4: Temperature Control Using PIC16F877A

4

We are going to explain about the system developed below with the aid of software’s such as

MPLAB, Proteus, Lab View and Autodesk Inventor. Final step would be developing the

prototype of the design described. In the end the developer would tell the limitations faced and

the suggestion to improve the design for further use.

Page 5: Temperature Control Using PIC16F877A

5

Project Design:

Overall Design

1. Objectives:

The main objective is to construct a system which autonomously regulates or controls the

temperature. Many companies specifically, chemical companies requires water at set

temperature for cleaning purposes, boilers, cooling purposes etc. To meet that demand

they can flow 2 ways:

The first way is they use some kind of cooling source which works continuously, keeping

the temperature of the water at defined temperature. But, for the cooling system, the need

for electricity is high, which is expensive.

The other way is that they use cool water from some source in company and mix it

together with the hot water, and then they can mix the two together using some kind or

mixer or cool it by using fan until the temperature comes down to the desired limit and

then stop. This method has low electricity consumptions and results are got faster than

the previous which has to get the temperature down by cooling continuously.

The system we want to construct can be even used in schools, hospitals etc. If somebody

wants to have bath in summer and the tank is containing hot water, the temperature

sensor detects the temperature and opens the valve from the other cool water source using

the servo motor, so that it can mix the water from that source with cool water using the

mixer attached to dc geared motor or can cool down the mixed water using fan. Any of

the two methods can be used. Once the temperature comes to the range defined by the

user, computation done by the microcontroller. The dc geared motor stops and the servo

motor closes the valve, hence closing the source of the water from cool water tank to the

tank which had hot water.

Page 6: Temperature Control Using PIC16F877A

6

So the developers have to make a system which can work as defined above, as the system

is more economical and gives faster results.

Actually the system was designed for using the cool water source to cool the hot water

without using any fan or mixer. As, the assignment demanded to have a dc geared motor.

We put up the fan or mixer to increase the speed of cooling.

Page 7: Temperature Control Using PIC16F877A

7

2. Function:

The system design is fairly simple as this is the first time the developers are using the

microcontrollers, if would have more time to experiment, the developers could have

come up with more complex system. This system is for easy understanding, and gaining

basic knowledge about Mechatronics design.

The system consists of 2 LM35DZ temperature sensors whose input is the temperature of

the liquids or water in Tank A and Tank B. The output of these sensors is analog voltage

signals which go into PIC16F877A microcontroller. The temperature sensors are

connected to the ADC ports of the microcontroller, which takes in the analog signal and

gives out digital values across specified ports.

Across the defined output port of the microcontroller, LCD is connected with takes the

values of the temperature sensor and displays them, which change as the temperature of

liquids in Tank A and Tank B change.

Now comes, the actuators which are the hands and legs of the system. We have RC servo

motor which is connected to a slit (valve) in the Tank A. When the temperature is above

50 C the servo motor moves right (2.35ms) and cause the slit to move aside let the cool

water from Tank A to flow into hot water Tank B. While at same time the dc geared

motor is given command to run at full speed when the temperature in above 50 C and mix

hot and cool water or cool the mixed water using fan, anything can be chosen as they are

replaceable.

When the temperature drops below 50 C the servo motor attached to Tank A turn left

(0ms) and causing the slit to close and stopping the flow of cool water into the Tank B

having hot water, while at same time the dc geared motor stops. This remains until the

temperature increases again above 50 C.

Page 8: Temperature Control Using PIC16F877A

8

This is the basic function of the system designed. Improvements will be suggested at end,

which can be done once the developers have time to research more and experiment using

the microcontroller and other IC’s.

Page 9: Temperature Control Using PIC16F877A

9

Mechanical:

1. 3D Design of the System:

Tank B

Tank A

Mixer

Slit

Page 10: Temperature Control Using PIC16F877A

10

Page 11: Temperature Control Using PIC16F877A

11

Page 12: Temperature Control Using PIC16F877A

12

2. Components Used Mechanical:

Developer wanted to use the design created by him on Autodesk inventor as can be seen

above. But, as this is scale down model of the actual concept which developer had in

mind, very simple and easy methods have been used for reaching the result according to

the function described above. Due to limitations on money, time and experience

developers concept had setback.

Hard Plastic Containers:

Developer has chosen to use 2 hard plastic containers to simulate TANK A and TANK B.

The Tank A has been represented by a hard plastic glass of volume 200 ml and Tank B

has been represented by big hard plastic glass of 650 ml.

The hard plastic glass (tank A) is put at some height above hard plastic glass so that water

can easily flow from A to B with using any sorts of pump. The volume of tank B is

chosen to be greater as our system doesn’t have an effective feedback system mainly

because the sensor required to detect the water level in tank B (ultrasonic sensor) wasn’t

functioning well when tried to detect water level. So to protect from over flow of water

we used bigger tank B compared to tank A. The other change in the design happened at

very last stage, which is the day of submission. The valve needed to open and close water

flow from tank A to B could not be found. The developer planned to use a tap as valve

but could not connect to his plastic container and when he tried to use the slit as valve,

the water rushing out came directly on the servo motor, which could damage it.

So finally after many tries and many failures, the developer planned to modify his

concept a bit. Instead of the valve for opening and closing of the tank A using the servo

motor, the user connected (attached) the servo motor to the outside of Tank A with Tape,

so whenever the opening of the valve is needed to be simulated, the servo will rotate and

with it would the Tank A pouring its contents directly into Tank B.

Page 13: Temperature Control Using PIC16F877A

13

Electrical:

1. Components Used Electrical:

LM35DZ Temperature Sensors:

For our assignment we are using Two LM35DZ sensors to measure the temperatures in

tank A and tank B. Below are some of the characteristics and properties of the LM35DZ.

The LM35 series are precision integrated-circuit temperature sensors, whose output

voltage is linearly proportional to the Celsius (Centigrade) temperature. The LM35 thus

has an advantage over linear temperature sensors calibrated in ° Kelvin, as the user is not

required to subtract a large constant voltage from its output to obtain convenient

Centigrade scaling. The LM35 does not require any external calibration or trimming to

provide typical accuracies of ±1⁄4°C at room temperature and ±3⁄4°C over a full −55 to

+150°C temperature range. Low cost is assured by trimming and calibration at the wafer

level. The LM35’s low output impedance, linear output, and precise inherent calibration

make interfacing to readout or control circuitry especially easy. It can be used with single

power supplies, or with plus and minus supplies. As it draws only 60 μA from its supply,

it has very low self-heating, less than 0.1°C in still air. The LM35 is rated to operate over

a −55° to +150°C temperature range, while the LM35C is rated for a −40° to +110°C

range (−10° with improved accuracy).

Features

Calibrated directly in ° Celsius (Centigrade)

Linear + 10.0 mV/°C scale factor

0.5°C accuracy guarantee able (at +25°C)

Rated for full −55° to +150°C range

Suitable for remote applications

Low cost due to wafer-level trimming

Operates from 4 to 30 volts

Page 14: Temperature Control Using PIC16F877A

14

Less than 60 μA current drain

Low self-heating, 0.08°C in still air

Nonlinearity only ±1⁄4°C typical

Low impedance output, 0.1 W for 1 mA load

PIC 16F877A Microcontroller:

Microcontroller is the brain of the system; all the decisions are taken by it autonomously.

We are using this particular series of PIC microcontroller because:

It is very simple yet very powerful to use

It can be programmed and reprogrammed numerous times

It is small in size while have sufficient input/ output ports

It has low current consumption

It has onboard multichannel analog to digital convertor

It has in- built PWM function

Page 15: Temperature Control Using PIC16F877A

15

Below are some of the features present in the PIC 16F877A, its pin and block diagram:

Pin Count 40 pin - PDIP

Program Memory 14KB or 8K 14-bit Flash

Max Crystal Speed 20MHz

RAM bytes 368

EEPROM bytes 256

Timers 2 x 8 bit, 1 x 16-bit

Digital Communication 1xA/E/USART, 1 x MSSP(SPI/I2C)

Capture/ Compare/ PWM 2 x CCP

ADC 8ch, 10-bit

Comparators 2

Page 16: Temperature Control Using PIC16F877A

16

Page 17: Temperature Control Using PIC16F877A

17

(Register File Map)

Page 18: Temperature Control Using PIC16F877A

18

Analog to Digital Converter (ADC):

The ADC is used to convert analog signals (input) to digital output, for our system we

use 2 LM35DZ sensors whose outputs are analog voltage values. Therefore to convert

them and use for various purposes we use ADC which is an integrated module inside the

PIC 16F877A.

The Analog-to-Digital (A/D) Converter module has five inputs for the 28-pin devices and

eight for the 40/44-pin devices. The conversion of an analog input signal results in a

corresponding 10-bit digital number. The A/D module has high and low-voltage

reference input that is software selectable to some combination of VDD, VSS, RA2 or

RA3. The A/D converter has a unique feature of being able to operate while the device is

in Sleep mode. To operate in Sleep, the A/D clock must be derived from the A/D’s

internal RC oscillator.

The A/D module has four registers. These registers are:

A/D Result High Register (ADRESH)

A/D Result Low Register (ADRESL)

A/D Control Register 0 (ADCON0)

A/D Control Register 1 (ADCON1)

The ADCON0 register controls the operation of the A/D module. The ADCON1 register,

configures the functions of the port pins. The port pins can be configured as analog inputs

(RA3 can also be the voltage reference) or as digital I/O.

Page 19: Temperature Control Using PIC16F877A

19

(ADCON0)

Page 20: Temperature Control Using PIC16F877A

20

(ADCON1)

Page 21: Temperature Control Using PIC16F877A

21

The ADRESH: ADRESL registers contain the 10-bit result of the A/D conversion. When

the A/D conversion is complete, the result is loaded into this A/D Result register pair, the

GO/DONE bit (ADCON0<2>) is cleared and the A/D interrupt flag bit ADIF is set. The

block diagram of the A/D module is shown below. After the A/D module has been

configured as desired, the selected channel must be acquired before the conversion is

started. The analog input channels must have their corresponding TRIS bits selected as

inputs.

After acquisition time has elapsed, the A/D conversion can be started. To do an A/D

Conversion, follow these steps:

1. Configure the A/D module:

Configure analog pins/voltage reference and digital I/O (ADCON1)

Select A/D input channel (ADCON0)

Select A/D conversion clock (ADCON0)

Turn on A/D module (ADCON0)

2. Configure A/D interrupt (if desired):

Clear ADIF bit

Set ADIE bit

Set PEIE bit

Set GIE bit

3. Wait the required acquisition time.

4. Start conversion:

Set GO/DONE bit (ADCON0)

5. Wait for A/D conversion to complete by either:

Polling for the GO/DONE bit to be cleared (interrupts disabled); OR

Waiting for the A/D interrupt

Page 22: Temperature Control Using PIC16F877A

22

6. Read A/D Result register pair (ADRESH: ADRESL), clear bit ADIF if required.

7. For the next conversion, go to step 1 or step 2 as required. The A/D conversion time

per bit is defined as TAD.

(A/D Block Diagram)

Page 23: Temperature Control Using PIC16F877A

23

The conversion process for ADC (10 bits) is as follow:

ADC have 10 bits, so the number of outputs are = 210

= 1024 or 0 -1023

As we are using LM35DZ, 10mv output from the sensor = 1 C

So, if output is 100mv, the temperature is as = 1 C\10 * 100 = 10 C

But, we are using the reference voltage as 1.024V

So, resolution is 1.024\1024 = 1mv

So, if LCD display 102.4 then it means the voltage is = 102.4*10mv=1.024v

Servo Motor:

For moving the value (but now tank A), we are using Servo Motor C36R from Cytron

Technologies. The main advantage of servo motor over dc geared motor is to control the

angle of rotation. The properties of C36R (180 degrees rotation angle) are as follow:

Speed (sec\60 deg) 0.16/4.8 V, 0.14/6 V

Torque (Kg/cm) 3.5/4.8 V, 4.5/ 6V

Features

Servos are controlled by sending them a pulse of variable width. The signal wire is used

to send this pulse. The parameters for this pulse are that it has a minimum pulse, a

maximum pulse, and a repetition rate. Given the rotation constraints of the servo, neutral

is defined to be the position where the servo has exactly the same amount of potential

rotation in the clockwise direction as it does in the counter clockwise direction. It is

important to note that different servos will have different constraints on their rotation.

Control position through Pulse Code Modulation

Pulse width range: 0.5ms – 2.5ms

Come with servo horn and screw accessories

Page 24: Temperature Control Using PIC16F877A

24

Servos are controlled by sending them a pulse of variable width. The signal wire is used

to send this pulse. The parameters for this pulse are that it has a minimum pulse, a

maximum pulse, and a repetition rate. Given the rotation constraints of the servo, neutral

is defined to be the position where the servo has exactly the same amount of potential

rotation in the clockwise direction as it does in the counter clockwise direction. It is

important to note that different servos will have different constraints on their rotation.

The angle is determined by the duration of a pulse that is applied to the signal wire. This

is called Pulse Width Modulation. The servo expects to see a pulse every 20 ms. The

length of the pulse will determine how far the motor turns. For example, a 1.5 ms pulse

will make the motor turn to the 90 degree position (neutral position). However, the exact

correspondence between pulse width and servo varies from one servo manufacturer to

another.1.5ms is not necessarily neutral or middle position.

The position pulse must be repeated to instruct the servo to stay in position. When a pulse

is sent to a servo that is less than 1.5 ms the servo rotates to a position and holds its

output shaft some number of degrees counterclockwise from the neutral point. When the

pulse is wider than 1.5 ms the opposite occurs. The minimal width and the maximum

width of pulse that will command the servo to turn to a valid position are functions of

each servo. Different brands, and even different servos of the same brand, will have

different maximum and minimums. Generally the minimum pulse will be about 1 ms

wide.

Page 25: Temperature Control Using PIC16F877A

25

DC Geared Motor:

Developer would be using four SPG50-10K dc geared motors to provide the power to the

fan or the mixer inside Tank B.

Page 26: Temperature Control Using PIC16F877A

26

Below are the properties of this dc geared motor:

Voltage 12V DC

Output Power 3.4 W

Rated Speed 170 RPM

Rated Current 0.9 A

Rated Torque 196 mN.m

For controlling the DC Geared motor we are using the motor driver MD10B. It is

designed to drive high current brush motor or application. It is designed for wide range of

robotics and automotive applications. The board incorporates most of the components of

the typical applications. With minimum interface, the board is ready to be plugged and

play. Simply add in power, this driver is ready to drive high current motor. It has been

designed with capabilities and features of:

Industrial grade PCB with heavy copper material for high current applications

Each component is soldered properly and tested

Support up to 10A maximum

5V logic level compatible inputs

12V as Vcc

PWM speed control up to 10KHz

Bi-directional control for 1 motor

Very low standby power consumption

System ground is isolated from motor’s power source with opto-isolator

4 Schottky diode as clamping diode

Dimension: 6cm x 4.5cm

Page 27: Temperature Control Using PIC16F877A

27

Page 28: Temperature Control Using PIC16F877A

28

(Connection of the driver to MCU)

(Connection of driver without MCU)

Page 29: Temperature Control Using PIC16F877A

29

LCD:

We are using 16*2 (LCD) for displaying the values of the temperature from the

temperature sensors after the ADC conversion through PIC 16F877A.

Connections

Page 30: Temperature Control Using PIC16F877A

30

Page 31: Temperature Control Using PIC16F877A

31

Project Working:

My work in the group was defined to interfacing 2 LM35DZ temperature sensors with PIC

16F877A microcontroller, then getting the value to be displayed on the LCD, coding of DC

geared motor, RC servo motor, LCD and ADC, lab view and PCB in PROTEUS. But I would be

explaining only the temperature sensors and displaying the value onto the display.

Block Diagram:

Page 32: Temperature Control Using PIC16F877A

32

Flow Chart:

Page 33: Temperature Control Using PIC16F877A

33

Circuit Diagram:

Page 34: Temperature Control Using PIC16F877A

34

Page 35: Temperature Control Using PIC16F877A

35

Page 36: Temperature Control Using PIC16F877A

36

Above the developer has used PROTEUS to construct the circuit for interfacing the PIC

16F877A with the LCD and the 2 LM35DZ temperature sensors.

First the SK40C PIC 16F877A driver was used to run the circuit, which was in the testing stages.

As the lecturer demanded SK40C circuit to be built on bread board or PCB we had to built the

driver circuit ourselves.

Crystal X1 of frequency on 20 MHz is used to as clock; 12V DC power supply is supplied at Vpp

which is connected in series with push button switch which is used to power on and off the PIC.

The 2 LM35DZ temperature sensors output are connected to AN0 and AN1 (analog input

channels) which are used for A to D conversion to give digital outputs. A potentiometer of 1k is

used to create Vref for the ADC; the output of this potentiometer goes to AN3. The pins from

number 7 to 14 (8 bits) on LCD are connected across PORTD of the PIC, which are 8 bits from

D0 – D7. The enable pin on the LCD is connected to RA5; the RW (read/write) pin on LCD is

connected to the ground, the RS (register select) pin on LCD is connected to RA2, VEE is

connected to output of 1K potentiometer which is used to change the contrast, VDD is connected

to +5V DC and VSS is connected to the ground.

This is the main connection of the circuit which is used to get the input from temperatures

sensors to the ADC port of PIC, change the analog signal from the sensors to digital and show

the output in Celsius across the 16*2 LCD. The other main connection for the circuit is the RB0

which is connected to the enable of the RC servo motor and RE0 which is connected to PWM of

the DC geared motor driver MD10B.

Below developer has shown the PCB layout generated using PROTEUS for the circuit shown

above. The first PCB is the auto generated PCB which has 2 layers and the second PCB is the

one made by the developer itself.

One of the toughest part of this assignment was generating this PCB as it took very long time to

get it right, but still could not manage to get absolutely right PCB had to remove a potentiometer

from the PCB to be able to complete it.

Page 37: Temperature Control Using PIC16F877A

37

Page 38: Temperature Control Using PIC16F877A

38

Page 39: Temperature Control Using PIC16F877A

39

Codes for MPLAB:

Below are the codes which the developer generated for the whole system. All codes were

developed by me (help from cytron). MPLAB helps to interface the PIC to PC with help of USB

driver. MPLAB is IDE which helps to write codes for the PIC. It uses Hi – Tech C Compiler for

compiling all the codes written together. It can be built and then burned into the PIC using the

USB burner.

//For interfacing the 2 temperature sensors with PIC 16F877A, displaying the output on LCD and

//controlling the movement of the RC servo motor and DC geared motor with respect to change

//in the voltage output from temperature sensors

#include <pic.h>

#include <htc.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#ifndef _XTAL_FREQ

#define _XTAL_FREQ 20000000

#endif

//===============configuration==============================

__CONFIG (0x3F32);

//===============define IO port=============================

#define lcd PORTD

#define RS RA2

#define E RA5

Including the Libraries needed for this

program…….

Crystal frequency of 20 MHZ defined for

oscillations……..

Setting up the PIC 16F877A for the

codes……..

Assigning or Defining ……..

Page 40: Temperature Control Using PIC16F877A

40

#define CHANNEL0 0b10000001 // AN0

#define CHANNEL1 0b10001001 // AN1

//==============FUNCTION PTOTOTYPE=========================

void e_pulse(void);

void delay(unsigned short i);

void send_char(unsigned char data);

void send_config(unsigned char data);

void lcd_goto(unsigned char data);

void lcd_clr(void);

void dis_num(unsigned long data);

void increment(unsigned long data);

void read_adc(void);

void motor_full(void);

void motor_stop(void);

void servo_one(void);

void servo_two(void);

unsigned short read_temp(void);

//====================MAIN================================

unsigned short result;

unsigned short temp,tempA,tempB;

void main(void)

{

ADRESH=0; //clear A/D result

ADRESL=0; //clear A/D result

Declaring the variables…….

High and Low ADC registers

Page 41: Temperature Control Using PIC16F877A

41

//setting ADCON1 Register

ADCON1=0b11000101; // A/D result right justified,

// configure RA2 and RA5 as digital

I/O

TRISA=0b11011011; //configure PORTA I/O direction

TRISB=0b00000000; //configure PORTB as output

TRISC=0b00000000; //configure PORTC as output

TRISD=0b00000000; //configure PORTD as output

TRISE=0b00000000; //configure PORTE as output

PORTA=0;

PORTB=0;

PORTC=0;

PORTD=0;

PORTE=0;

while(1)

{

send_config(0b00000001); //clear display at lcd

send_config(0b00000010); //Lcd Return to home

send_config(0b00000110); //entry mode-cursor increase 1

send_config(0b00001100); //diplay on, cursor off and cursor blink off

send_config(0b00111000); //function set

lcd_goto(0);

//display character on LCD

send_char(' ');

Assigning bits in ports

which to be output and

which to be input…..

Clearing all the ports before using…….

Configuring the LCD before use……

Page 42: Temperature Control Using PIC16F877A

42

send_char('T');

send_char('E');

send_char('M');

send_char('P');

send_char('.');

send_char('A');

send_char('=');

lcd_goto(20);

//display character on LCD

send_char(' ');

send_char('T');

send_char('E');

send_char('M');

send_char('P');

send_char('.');

send_char('B');

send_char('=');

while(1) //infinity loop

{

//sensor A

ADCON0=CHANNEL0; //CHANNEL1=0b10001001

lcd_goto(8);

read_adc();

temp=read_temp();

dis_num(temp/10);

send_char('.');

ADC 1 setup for TEMP A which is sensor 1

(Getting the actual value in Celsius)…….

To get TEMP A value to 1st line of LCD……….

To get TEMP B value to 2nd

line of LCD……….

Page 43: Temperature Control Using PIC16F877A

43

dis_num(temp%10);

send_char(0b11011111);

send_char('C');

send_char(' ');

send_char(' ');

tempA=temp;

//sensor B

ADCON0=CHANNEL1; //CHANNEL0=0b10000001

lcd_goto(28);

read_adc();

temp=read_temp();

dis_num(temp/10);

send_char('.');

dis_num(temp%10);

send_char(0b11011111);

send_char('C');

send_char(' ');

send_char(' ');

tempB=temp;

ADC 2 setup for TEMP B which is sensor 2

(Getting the actual value in Celsius)…….

Page 44: Temperature Control Using PIC16F877A

44

if(tempB>500)

{

motor_full();

servo_one();

}

else

{

motor_stop();

servo_two();

}

delay(2000);

}

}

}

The command is used to call the delay

for servo motor and dc geared motor if

TEMP B is more than 50 C……..

The command is used to call the delay

for servo motor and dc geared motor if

TEMP B is anything below 50 C…….

Page 45: Temperature Control Using PIC16F877A

45

//==================subroutine LCD setting ==========================

void send_config(unsigned char data)

{

RS=0;

lcd=data;

delay(500);

e_pulse();

}

void e_pulse(void)

{

E=1;

delay(500);

E=0;

delay(500);

}

void send_char(unsigned char data)

{

RS=1;

lcd=data;

delay(500);

e_pulse();

}

void lcd_goto(unsigned char data)

{

if(data<16)

{

When no register is selected……

When register is selected………

When enable goes high and low (read and write)……….

Page 46: Temperature Control Using PIC16F877A

46

send_config(0x80+data);

}

else

{

data=data-20;

send_config(0xc0+data);

}

}

void lcd_clr(void)

{

RS=0;

send_config(0x01);

delay(600);

}

void dis_num(unsigned long data)

{

unsigned char hundred_thousand;

unsigned char ten_thousand;

unsigned char thousand;

unsigned char hundred;

unsigned char tenth;

hundred_thousand = data/100000;

data = data % 100000;

ten_thousand = data/10000;

data = data % 10000;

thousand = data / 1000;

Converting the values got in Voltage to that

needed to display as degrees C (related to linear

characteristics of the LM35DX)………

Page 47: Temperature Control Using PIC16F877A

47

data = data % 1000;

hundred = data / 100;

data = data % 100;

tenth = data / 10;

data = data % 10;

if(hundred_thousand>0)

{

send_char(hundred_thousand + 0x30); //0x30 added to become ASCII code

send_char(ten_thousand + 0x30);

send_char(thousand + 0x30);

send_char(hundred + 0x30);

send_char(tenth + 0x30);

send_char(data + 0x30);

}

else if(ten_thousand>0)

{

send_char(ten_thousand + 0x30); //0x30 added to become ASCII code

send_char(thousand + 0x30);

send_char(hundred + 0x30);

send_char(tenth + 0x30);

send_char(data + 0x30);

}

else if(thousand>0)

{

send_char(thousand + 0x30); //0x30 added to become ASCII code

send_char(hundred + 0x30);

send_char(tenth + 0x30);

send_char(data + 0x30);

}

Binary output to ASCII which can be

understood……..

Page 48: Temperature Control Using PIC16F877A

48

else if(hundred>0)

{

send_char(hundred + 0x30); //0x30 added to become ASCII code

send_char(tenth + 0x30);

send_char(data + 0x30);

}

else if(tenth>0)

{

send_char(tenth + 0x30); //0x30 added to become ASCII code

send_char(data + 0x30);

}

else send_char(data + 0x30); //0x30 added to become ASCII code

}

void increment(unsigned long data)

{

unsigned short j;

for(j=10;j>0;j--)

{ lcd_goto(32);

data=data+1;

dis_num(data);

delay(10000);

}

}

//==================subroutine ADC=========================

void read_adc(void)

{

unsigned short i;

Page 49: Temperature Control Using PIC16F877A

49

unsigned long result_temp=0;

for(i=2000;i>0;i-=1) //looping 2000 times for getting average value

{

ADGO = 1; //ADGO is the bit 2 of the ADCON0

register

while(ADGO==1); //ADC start, ADGO=0 after finish

ADC progress

result=ADRESH;

result=result<<8; //shift to left for 8 bit

result=result|ADRESL; //10 bit result from ADC

result_temp+=result;

}

result = result_temp/2000; //getting average value

}

unsigned short read_temp(void)

{

unsigned short temp;

temp=result;

return temp;

}

//==================subroutine DELAY==========================

void delay(unsigned short i)

{

for(;i>0;i--);

}

Will continue until i=0…….

Page 50: Temperature Control Using PIC16F877A

50

//==================Motor full========================

void motor_full(void)

{

PORTE=0x01;

__delay_ms(10);

}

//==================Motor stop========================

void motor_stop(void)

{

PORTE=0x00;

__delay_ms(10);

}

//==================Servo one========================

void servo_one(void)

{

PORTB=0x01;

__delay_ms(2.35);

Subroutine to run the DC motor………

Subroutine to stop the DC motor……..

Subroutine to move servo motor to right……

Page 51: Temperature Control Using PIC16F877A

51

CLRWDT();

PORTB=0x00;

__delay_ms(17.65);

}

//==================Servo two========================

void servo_two(void)

{

PORTB=0x01;

__delay_ms(1);

CLRWDT();

PORTB=0x00;

__delay_ms(19);

}

Subroutine to move servo motor to the left…….

Page 52: Temperature Control Using PIC16F877A

52

Testing and Result:

Testing Method:

For the testing the circuit which is the sensor circuit and LCD display circuit, the developer used

the Proteus software to simulate the circuit first. After the circuit worked good the developer

went ahead with the constructions on the bread board using the SK40C circuit to place the PIC.

After that went good the developer used the construct the SK40C circuit on bread board himself

and tested it, after it being also successful the developer used the PCB to construct the circuit.

Page 53: Temperature Control Using PIC16F877A

53

Page 54: Temperature Control Using PIC16F877A

54

Page 55: Temperature Control Using PIC16F877A

55

Page 56: Temperature Control Using PIC16F877A

56

Result Obtained:

The circuit worked well in all the stages. Developer was very sure his system would come out

with flying colors but in the end while doing the design, in hurry the developer connected the

PIC to 12V power supply than 5V power supply and the IC burned. So in end the user had to

show the design using the bread board.

The sensors were working fine they put out voltage which was converted to digital value using

ADC in PIC and then in end displayed the changing values on LCD.

Page 57: Temperature Control Using PIC16F877A

57

LABVIEW:

Lab VIEW is a graphical programming language that uses icons instead of lines of text to create

applications. In contrast to text-based programming languages, where instructions determine

program execution, Lab VIEW uses dataflow programming, where the flow of data determines

execution. In Lab VIEW, you build a user interface with a set of tools and objects. The user

interface is known as the front panel. You then add code using graphical representations of

functions to control the front panel objects. The block diagram contains this code. In some ways,

the block diagram resembles a flowchart.

Lab VIEW programs are called virtual instruments, or VIs, because their appearance and

operation imitate physical instruments, such as oscilloscopes and multimeters. Every VI uses

functions that manipulate input from the user interface or other sources and display that

information or move it to other files or other computers.

Below we have made the block diagrams which we use to test the signals from the temperature

sensor LM35DZ. The signal from the sensor is interfaced with the laptop using a DAC (data

acquisition card). Once the signal comes from sensor it is in mille volts, we multiple it by 100

using multiplier as, per 10mV (0.01V) change in the voltage from the sensor is equal to 1 degree

Celsius. So, if we have 0.02V from sensor we multiply it by 100 which is equal to 2, which in

turn is 2 degree Celsius. The output of the multiplier goes to the thermometer, numeric display

and wave form graph to see the simulation of change in temperature.

One part of the output from multiplier goes into the greater than and equal to comparator. We

have compared the output of the multiplier with 50, which is the minimum temperature our

system will come to action. If the output of multiplier is above 50, the LED would light up which

indicates that the temperature is more than 50 degree Celsius.

Page 58: Temperature Control Using PIC16F877A

58

(Panel when there is no input to the data acquisition card)

Page 59: Temperature Control Using PIC16F877A

59

(Value on connecting to the LM35 sensor) (Wrongly Done)

Page 60: Temperature Control Using PIC16F877A

60

Discussion:

The developer learnt a lot about the Mechatronics design from this assignment, he learnt to code

in C language and assembly language; he got familiarized with MPLAB, learnt how to use

Proteus and develop PCB and at last the Lab View for getting signal to PC from sensor.

Developer also learnt a lot about PIC 16F877A, DC geared motor, servo motor, temperature

sensors and LCD. All this would be very useful for him for his FYP.

Suggestion

For the future the developer suggest using the Liquid level sensor the ultrasonic sensor to

measure the contents of tank B, protecting it from overflowing. Then using pump to feedback

water to tank A if due to any reason it gets hotter than tank B. And the last would be using a

timer in the circuit to protect continuous running of dc motor in hot temperature of tank B.

References:

www.Cytron.com accessed on 2nd

May,2011

http://sine.ni.com accessed on 3nd

May,2011

http://forum.allaboutcircuits.com accessed on 3nd

May,2011

http://extremeelectronics.co.in/microchip-pic-tutorials/interfacing-lm35-

temperature-sensor-with-pic-microcontroller/ accessed on 5nd

May,2011

http://en.wikipedia.org/wiki/Operators_in_C_and_C accessed on 5nd

May,2011

Page 61: Temperature Control Using PIC16F877A

61

Appendix: (Extra Info on LCD used)

FLOW CHART FOR LCD INITIALIZATION

FLOW CHART TO DISPLAY ASCII CHARACTER