TEMPERATURE INDICATOR AND RECORDER PROJECT

on Wednesday, 20 November 2013
As you have seen tutorial on A/D converter in my previous post, in this post we will do a project based on it. If you dint red my previous post then I strictly suggest you to read it and come back here, Click here to read it.
Two most useful concepts we will see in this post, one is A/D converter and another is  EEPROM. We make use of  inbuilt A/D converter  for sensing the temperature and for saving/recording its high and low values we use EEPROM.
WHAT YOU WILL LEARN FROM THIS PROJECT?

1. Reading Analog values from LM35 temperature sensor
2. Saving data in EEPROM

AIM:
Aim of the project is to read the analog values from the LM35 and displaying on the LCD display and saving the lowest and highest temperature values in EEPROM.

CIRCUIT DIAGRAM:

All other connections are same i.e pin 13, 14 =crystal, 12,31=gnd, 1,11,32=power +5v.
Note PORTC connected to DATA PINS of LCD
 RS=portd.f0
 R/W=portd.f1
 E=portd.f2

Points to be noted from above fig:
  •  LM35's output is connected to analog pin RA0 i.e portA.0 pin
  •  LM35's 1st pin is power, 2nd is output, 3rd is ground. 
EXPLANATION:
16x2 LCD: Read my posts on 16x2 Lcd in which i have explained more clearly with the help of library functions as well as without using library functions. Click here to go to home page
 
TEMPERATURE SENSOR LM35: Sensor which we are using for the purpose of temperature sensing  is lm35. Look at the pin diagram below
From right 1st pin= ground(0v
2nd pin= output. (+6v to -1v range)(which will be connected to microcontroller analog pin)
3rd pin= Vcc (+35v to -2v range).

*** For every 1°C rise/fall of temperature the output voltage will be 10mv.
What does it mean? It means if the temperature is  1°C then output from lm35 will be 10mv, if the temp is 2°C then lm35 output will be 20v , if the temp is 5°C then output from lm35 will be 50mv, so on... 
Now answer me, if output from the lm35 is 250mv then what will be the temperature?
Obviously it will be 25°C (250/10=25).  
Its a simple math everyone can do it, if you understand this then you have successfully understood output of lm35. 
Here 10mv is the stepsize of our sensor(lm35), if you are taking someother sensor first know its stepsize. 

A/D CONVERTER:We are using pic18f452 which have inbuilt 10 bits wide Analog to Digital(A/D) Converter, so it is  not necessary to interface a separate A/D converter module to our micro-controller. 

As sensor have its step size, similarly A/D too have its step size, we can find its value using below formula.

Step size =  Vref / max bit value

Vref =5v (let us choose) . 
Max bit value=  1024, as i said A/D in Pic18f452 is 10 bits wide so max binary value for 10bits 1111111111 , when we convert it in decimal it will be 1023(starts from 0) so total 1024.
Step size =5v/1024 = 5000mv/1024 = 4.88mv

Whatever value we get from the sensor we need to multiply it by 4.88 to get actual value.


Example: Read analog values from channel 0 and storing  in variable 'xyz' and multiply it by step size of A/D and divide by step size of lm35
xyz = Adc_readx(0);
   xyz= xyz*5000/1024;
                                                                   xyz=xyz/10;

But we dont use library function for coding, we will create our own function for this purpose which i have explained in A/D converter tutorial.

EEPROM: 
EEPROM is the Electrical Erasable Programmable Memory, which is Read Only Memory but can be erasable and rewritten i.e can store data even after power is off, its not volatile like RAM .
EEPROM is inbuilt in PIC18f452 with the storage capacity of 256byes so we make use of it for storing highest and lowest temperature values.
MikroC provides handy library functions for this purpose, lets see two functions for reading and writing.

EEPROM_READ(address)
Example: Reading data from eeprom address 0x6
             some_variable= EEPROM_READ(0x6)
                               or
            some_variable_x= 0x6;
            some_variable= EEPROM_READ(some_variable_x) ;

EEPROM_WRITE(address, data)
Example: 'xyz' contain the data 25,  Write that data into EEPROM location 0x3
              EEPROM_WRITE(0x3,xyz)
                              or
              EEPROM_WRITE(0x3,25)

ALGORITHM:

1. Display "TEMPERATURE" for 1sec on 16x2 LCD Display.
2. Display old session's High and Low recorded values( For the first time high and low values will be 0).
3.Take input from sensor and multiply it with stepsize of microcontroller and divide with stepsize of sensor.
4. Show the present temp reading in °C in the format " Its now xx°C" (xx is the current temp value).
5. Update the temp reading for every 1sec and displays it.
6. After updating check with its previous 'high' and 'low' recorded values which have already saved in EEPROM and if current temp is higher or lower, then update EEPROM data with the current value. 
7. If microcontroller is reset then go to step1 and repeat all steps...



PROGRAM CODE:

//COMPILER: MIKROC FOR PIC
//MICRO CONTROLLER: PIC18F452
//CRYSTAL FREQ: 4MHZ

//----- Global variables---------//


 unsigned long high,low,vin;
 long mv;
 unsigned char op[12],an_insert;
 unsigned int AD_value=0,vvv=0;
//-------------------------------//
// --Function to read Analog to digital data--//
 unsigned int Adc_readx(unsigned char c)
 {
     c=((c<<3)|(0b0000001));
     ADCON0=c;
     delay_ms(100);
     ADCON0.F2=1;
     while(ADCON0.F2==1);
     AD_value=ADRESL;
     vvv=ADRESH;
     vvv=vvv<<8;
     AD_value=AD_value|vvv;
     return(AD_value);
}
 //-- Finding temperature from input data--// 
long Temperature()
{
    vin=Adc_Readx(1);
    mv=(vin*5000);
    mv=mv/1024;
    mv=mv/10;
    return(mv);
}
//--Function to show high and low values--//
void show_HL()
{
    if(EEPROM_read(0)==255){ EEPROM_write(0,0);delay_ms(20); EEPROM_write(1,0);}
//By default all memory of eeprom will be filled with 255(0xff) , we are checking for the first time and making 0
    high= EEPROM_read(0);
    low= EEPROM_read(1);
    longtostr(high,op);
    Lcd_cmd(_lcd_clear);
    delay_ms(50);Lcd_out(1,1,"HIGH=");lcd_out_cp(op);
    longtostr(low,op);
   delay_ms(50); Lcd_out(2,1,"LOW=");lcd_out_cp(op);
   delay_ms(2000);
}
// --Configuring LCD--//
sbit Lcd_RS at RC4_BIT;
sbit Lcd_EN at RC5_bit;
sbit Lcd_D4 at RC0_bit;
sbit Lcd_D5 at RC1_bit;
sbit Lcd_D6 at RC2_bit;
sbit Lcd_D7 at RC3_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;
//--Main function--//
void main()
{

 TRISC=0;
 TRISB=0xff;
 Lcd_Init();
 Lcd_cmd(_Lcd_Clear);
 Lcd_out(1,1,"TEMPERATURE");
 Delay_ms(1000);
 ADCON1=0X80;
 Show_HL();//Show High and low temp noted in last session
 vin=temperature();//Read temperature
 low=vin;
   while(1)
   {
        Lcd_cmd(_Lcd_clear);
        vin=temperature();
          if(vin>high)
         {
             high=vin; EEPROM_write(0,vin);
              delay_ms(50);
          }
          if(vin<low)
          {
              low=vin; 
              EEPROM_write(1,vin);
            }

        longtostr(vin,op);
        ltrim(op);
        lcd_out(1,1,"Its now");
        lcd_out(1,9,op);
        lcd_chr_cp(223);//ASCII code for degree symbol (°)
        lcd_out_cp("C");
       delay_ms(1000);//update for 1sec
        lcd_cmd(_lcd_clear);
     }
 }

AUTHOR: YOUSTRON SIC










 








 







1 comments:

Unknown said...

Are you going to finish the advanced projects? I am very interested in them. I am a hardware engineer trying to learn the firmware side of microcontrollers. Thanks

Post a Comment