on Monday, 25 November 2013

INTRODUCTION: Are you worried about the protection to your home? Are you  worried about the security of your precious belongings? Are you burdened to pay salary to 'security guard' every month? Then this is the best choice to protect your home and save your money spending on security guards.
This is a simple project, the main intention of this project is to make you aware of  interfacing micr0controller with simple sensors.

AIM: The aim of this project is, whenever an unknown person tries to enter into home through windows then buzzer alerts. 

We are using a laser beam across  the window (look below fig.), Whenever anyone tries to enter through the window then laser light cuts, sensor (L14F1) senses the missing of laser light and sends signal to buzzer and by which buzzer starts beeping..





 

ALGORITHM:

>> 'ON' the laser source, try to make the laser light beam to fall directly onto the sensor L14F1, which is on the opposite side of the window.
>> As long as the light beam fall on the sensor buzzer will be in OFF state.
>>As soon as the light beam cuts then microcontroller will make a buzzer ON and alerts.

BASIC UNDERSTANDING:
Before going into the project schematic and code, we need to understand few basic modules of the project i.e. Sensor.
Here we are using L14F1 sensor to detect the laser beam.
L14F1 Sensor: It is the 3 terminal device, it is a type of transistor which have emitter, base and collector. Look at the below fig.

See the below fig. to know which are the  Emitter, Base and Collector pins


Now let us analyze the connections to microcontroller(pic18f452).


>>when L14F1 sense the laser light then 'C' will be short to 'E' so current flows to ground, i.e pin21 will be '0' low.
>>As long as the sensor L14F1 senses the laser light then the  input 'pin 21' will be '0' i.e low, when the light is cut then pin21 will be high i.e '1'.
Laser Source:
In this project we are using Laser pointer as the source. It look something like the below image


PROJECT REQUIREMENTS:
  • MikroC compiler(download free from official website)
  • Micro controller- pic18f452
  • Buzzer,Led
  • Resistors-200 ohm(2) , 10k ohm(1)
  • capacitors-30 pf(2), 0.1 uf, 100uf  crystal oscillator 4Mhz
  • 9v battery , Lm7805 voltage regulator
CIRCUIT DIAGRAMS:
POWER SUPPLY:As we are using 9v input battery , so we need to convert it to 5v using 7805 Ic.


Overall circuit look like this:


The general connections for pic are the same
i.e crystal oscillator between 13 and 14th pin, pin31=pin12=ground,pin11=pin32=pin1=vcc.

SOFTWARE:
MikroC is one of the comprehensive software which have lot of inbuilt libraries. We need not to worry about writing code for general modules interfacing like Lcd's, keypad's, and many more.. so basic idea at C language is enough to code using MikroC compiler.
Now let us analyze the code, its very simple code.

void main()
{
 TRISC=0X00;
 TRISD=0XFF;
 portc.f4=0;   // Buzzer off
 for(;;)
 {
          if(portd.f2==0)//i.e laser is not cut i.e sensor sensing the laser
          {
              portc.f5=1;   //RED led on
              portc.f4=0; // Buzzer off
          }
          if(portd.f2==1)    //if  laser light is cut
          { 
              portc.f5=0;       //Red led off
              portc.f4=1;       // Buzzer on 
          }
      }
 }

AUTHOR: YOUSTRON SIC
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