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 :
- Firing angle control circuit for thyristors
- For protection used with frequency sensitive devices.
- And many others applications.
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

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.
need compelet porj.
good
send to my email
Everything is available in post code +schematic use this post to make schematic and write your code
Want this code but frequency required to generate from ICL8035
Hi , can you send me the full project please. Thank you!
thank you
i need coplete siulation and code of these project
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.
code and schematic is available in post
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.
please give the full code
can you explian your code
from where you bring this number 5007999
It is used to convert time measured by timers into frequency
how did u calculate this number?
Do tell about how u calculated that.
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.
i need complete simulation and code of this project
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
code please
You could spend me your code and simulation?
could you send me full code and simulation please?
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 🙂
thanks for compliments 🙂
i need complete simulation and code of this project. Could you fwd it?
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.
hyy please mail me this program code with hex file
Can a frequency meter be made with an 8051 or similar microcontroller?
nice one. please mail me this program code with hex file
when will u post for sine wave,sir?
http://microcontrollerslab.com/sine-wave-frequency-measurement-using-pic/
is the code full, sir ???
Is the code full,sir??
Is the code full, sir?
Hi bilal sir. Really nice project. Sir I want to build its dual channel version. What is changes in coding for it?
Sir why you divided into this…what is this value…5007999
hey can u send me its header file and where are you calling that interrupt function?
i want complete code for reading the frequency of the square wave
SIR i got error on this line lcd_out(1,1,”frequency:”);
Invalid expression
hola seria posible el programa completo mi correo electronico es
[email protected]
gracias
how did you find 5007999
good
Hi thx for job.. i Want this code plz
[email protected] .
Can you give me please this programme with mplab xc8.
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
Complete code with detail explanation and also circuit explanation
could you please send me this code ?
[email protected]ù
code is given in article
Hello!
What if I have more than one source of frequency generator and I need to measure their frequencies simultaneously?
Please help.
Thank you
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
Respected Mr Malik,
Your project is appreciable. Kindly elaborate the frequency calculation formula to enable novice like me to understand.Thanks a lot sir.
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.
hi, i need this project can you send me in mail
Thank you
This is complete by the way
Please can you give me the full project of this with full coding and complete simulation ,thank you
Hey there, I would appreciate so much if you send me the project . Thank you so much. You are great
Send the complete project
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)
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.
May I have the codes and simulation? Which simulation app did u use?.
Thank youu