Digital frequency meter using pic microcontroller

Digital frequency meter with LCD display  using PIC16F877A

Digital frequency meter with LCD display  can be used to measure frequency of square wave. With little bit modification in this embedded systems project you can also measure frequency of sine wave. I will also discuss it at the end of this article.In this digital frequency meter PIC16F877A microcontroller is used to measure frequency with the help of external interrupt and 16 * 2 LCD is used for digital display of frequency.

Applications of Digital frequency meter

Digital frequency meter have many application. There are many sensors which don’t give output in voltage form. To interface these kind of sensors with digital system signal conditioning has to be done. For example in capacitive sensors extra circuit is used to convert change in capacitance to another physical parameter which a digital system can easily read.In capacitive sensors change in frequency with respect input is usually convert into proportional frequency square wave with the help of some extra circuitry. Example of such sensor is humidity sensor.

There are many other examples of digital frequency meter like :

Components

Complete list of components you need to make a digital frequency meter:

Resistors,"R1",4k,
Capacitors,"C1",22pF,
Capacitors,"C2",22pF,
Integrated Circuits,"U1",PIC16F877A,
LCD,"LCD1",LM016L,
CRYSTAL,"X1",CRYSTAL 20Mhz,

Implementation

Frequency and time period are inversely proportional with each other .

Frequency = 1 / time period

If one know time period of one wave , frequency can also be easily calculated with above relation . we can’t measure frequency directly with this method. In this project time period of square wave is measured first which is converted into frequency by using above relationship of frequency and time period.

PIC16F8877A microcontroller externnal interrupt at rising edge and timer are used to measure time period of square wave. algorithm is developed to convert time period into frequency. 16 * 2 LCD is used to display measured frequency digitally.

Circuit diagram

Digital frequency meter
Digital frequency meter circuit diagram

In above circuit diagram pulse generator is used to generate square wave of any frequency.This is just for simulation purpose. You can connect any device to this pin which frequency you want to measure.We have connected 6Khz square wave with interrupt pin to measure its frequency. But LCD is showing 5.99 KHz. This is because of real time operating issues of microcontroller. I will not discuss them here. But to remove this error. Read frequency 5-10 times and display their average on LCD.

Frequency measurement using pic microcontroller

Coding of frequency measurement

Code is written in MIkro C for PIC

//************Digital frequency meter*********

//************[email protected]*********

sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
int on=0,value,freq;
char text[7];
void interrupt(void) // high portD
{
if(T1CON.TMR1ON==0)
{
T1CON.TMR1ON=1; // turn on the timer1
INTCON.INTF = 0; // clear the interrupt flag
}
else if(T1CON.TMR1ON==1)
{
T1CON.TMR1ON=0; // turn off the timer1
value=(TMR1H<<8)|(TMR1L);
INTCON.INTE = 0; //Enable RB0/INT external Interrupt
freq=(5007999/value);
//freq=1/value;
IntToStr(freq, text);
Lcd_Init();
lcd_out(1,1,”frequency:”);
lcd_out(2,1,text);
delay_ms(1000);
TMR1H=0;
TMR1L=0;
INTCON.INTE = 1; //Enable RB0/INT external Interrupt
INTCON.INTF = 0; // clear the interrupt flag

}

}void main(void)

{
TRISB.f0 = 1;
T1CON=0X00;
TMR1H=0;
TMR1L=0; // intialization of timer one prescalar and internal clock
INTCON.GIE = 1; //Enable Global Interrupt
INTCON.INTE = 1; //Enable RB0/INT external Interrupt
OPTION_REG.INTEDG = 0; //Interrupt on rising edge

do
{

} while(1);
}

With little bit modification in above circuit only, you can also measure frequency of sine wave.Only a small external circuitry is required which can convert sine wave into square wave with same frequency. You can easily do this with a simple operational amplifier. Operational amplifier can be used as a comparator for this task and you can also use zero detection circuit. I will also post article on sine wave frequency measurement and one example of digital frequency meter in humidity sensor.

Now I am done with my part. Its your turn now. comment with your issues on this post. It will be pleasure for me to solve your issues. If you feel this useful and could be useful for your friends. don’t forget to share with your friends. Good luck! 🙂 If you need complete simulation and code of this project comment on this post with your email address.

60 thoughts on “Digital frequency meter using pic microcontroller”

  1. Hello Bilal,
    I am a fresh-woman. I am interested to work Proteus. I tried but I couldn’t run. Is it possible, please send Proteus file for me?
    Thank you.

    Reply
  2. i thin, as the given measured frequency increases the measurement accuracy will decrease .whats the maximum frequency which can be measured say with 1 or 2 percent of error.also the micro controller be run at 4 – 16 Mhz what will be the range of measurement when mcu is run at its max speed.

    Reply
  3. Thank You very much Mr. Malik… I am a student in power electronics doing my project. Your posts are really worthy and you prove that knowledge grows by sharing.

    Reply
  4. Hi Malik, appreciate your work. May I ask how do you get this number in your coding?
    “freq=(5007999/value);”. That 5007999 number, it is obviously frequency=1/T but can you explain how you did the calculation to get that number?
    Thank you so much

    Reply
  5. hi , your projects are so sucsessful , everything is easily understandable.

    thank you so much , your information is grateful for me.

    the other people who wants the”full code” are stupid because all they need is at your article 🙂

    Reply
  6. hi!,I like your writing so much! share we communicate
    more approximately your post on AOL? I require an expert in this
    house to solve my problem. May be that is you!

    Taking a look ahead to look you.

    Reply
  7. Could you please send full code and simulation to me at [email protected]? Also, how would you modify code to read 3 separate variable frequencies input to the chip, A, B and C and then add A and B and subtract result from C ie. (X = C – (A + B)) and display X on the LCD…,. Thanks…Terry

    Reply
  8. Hello!
    What if I have more than one source of frequency generator and I need to measure their frequencies simultaneously?
    Please help.
    Thank you

    Reply
  9. I am happy and serious when I watched this program
    Can you help me with a program similar to this if I told you about the details
    Thank you so much

    Reply
  10. Respected Mr Malik,
    Your project is appreciable. Kindly elaborate the frequency calculation formula to enable novice like me to understand.Thanks a lot sir.

    Reply
  11. Respected Mr Malik,
    Thanks a lot Sir for this beautiful article. I would like to know that you have used external interrupt in square wave frequency measurement but ISR in code is quite big, does PIC 16 F 877 MCU contains enough memory space in Interrupt vector table ? Kindly elaborate. It will be a great help for the novice like me.

    Reply
  12. The Code is complete and 100% working
    i have two questions which are still unclear to me
    1. How we get 5007999?
    2. why the LCD display 5.99k instead of 6K. (not cleared from your video)

    Reply
  13. What is the figure 5007999 in the code. I know that frequency is the inverse of time, and time period is stored in the value variable , then what is the need for this figure.

    Reply

Leave a Comment