# 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 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.

### Coding of frequency measurement

Code is written in MIkro C for PIC

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

//************bilalmalikuet@gmail.com*********

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.

## ย ESP32 programming with Arduino VIDEO COURSE 73% DISCOUNT

1. ุงุจููููู ูุญูู October 13, 2014
2. ุงุจููููู ูุญูู October 13, 2014
3. ุงุจููููู ูุญูู October 13, 2014
• BILAL Malik October 13, 2014
• Nilesh February 14, 2017
4. NC November 17, 2014
5. mohameh November 18, 2014
6. tayebeh December 14, 2014
• BILAL Malik February 14, 2015
7. anil January 30, 2015
8. bilash February 13, 2015
9. smwal February 14, 2015
10. smwal February 14, 2015
• BILAL Malik February 14, 2015
• aayzee August 23, 2016
• Rashi October 17, 2016
11. G Vignesh March 22, 2015
12. nik kamil April 5, 2015
13. Marcus Lim August 3, 2015
14. sathsih August 11, 2015
15. RME October 27, 2015
16. Rino December 24, 2015
17. adil can balรงฤฑk January 14, 2016
• BILAL Malik January 14, 2016
18. Anand March 20, 2016
19. promotion pari sportif March 21, 2016
20. Anand March 31, 2016
21. rashi August 17, 2016
22. ali September 30, 2016
23. Rashi October 17, 2016
• BILAL Malik October 17, 2016
• dinhbaoduc December 5, 2016
24. dinhbaoduc December 5, 2016
25. dinhbaoduc December 5, 2016
26. Mangesh January 22, 2017
27. Sampathkumar March 11, 2017