# Sine wave frequency measurement using pic microcontroller

Sine wave frequency measurement project is used to measure frequency of sine wave using pic16F628A microcontroller. The basic  function of Sine wave frequency measurement project is to measure frequency of sine wave and to display frequency value of LCD. PIC16F628A pic microcontroller is used for Sine wave frequency measurement. PIC16F628A microcontroller measured sine wave frequency with the help of timer one and displays frequency value on LCD by converting integer value into string. I will explain it in later part of this article.

### Sine wave frequency measurement applications

Sine wave frequency measurement have many applications in power electronics circuits.  It is used in cycloconverters to control frequency of AC power supply. Sine wave frequency measurement circuits are also used in variable frequency driver circuits. It is also used in frequency controller projects. There are enormous applications this project in power electronics circuits. I will mention one practical example at the end of this article with practical circuit.

### Sine wave frequency measurement project main components

Following are the main components use to measure frequency of sine wave:

• Zero crossing detector circuit: Zero crossing detector circuit is used to detect zero crossing of sine wave from positive half cycle to negative half cycle and similarly from negative half cycle to positive half cycle.
• PIC16F628A microcontroller :  It is heart of Sine wave frequency measurement project. Because its detects zero crossing of sine wave and measure time between zero successive zero crossing detection with the help of timer one used as a counter. This measured time is used to calculate frequency of sine wave. As you know frequency is reciprocal of timer period. By dividing measured time with two and taking its inverse in programming, frequency of sine wave is calculated. ( For more details check programming part of this article)
• Liquid crystal display: 16 × 2 LCD is used to display frequency on LCD. LCD is connected to PORTB of pic microcontroller

### Sine wave frequency measurement circuit diagram

Circuit diagram for this project is given below. AC supply whose frequency is to measure connected with potential transformer. Potential transformer step down the AC voltage from 220V AC to 9V AC. Output of potential transformer is connected with full bridge diode rectifier. Full bridge diode rectifier converts 9V AC into pulsating DC. Bridge rectifier is used to convert negative cycle of sine wave into positive cycle. Because microcontroller can not read negative voltage due to reference of zero potential to microcontroller.

After full bridge diode rectifier,voltage divider is used to divide 9v pulsating  dc into two equal parts. because microcontroller can not read voltage more than 5 volt. Output of voltage divider is given to timer 1 pin of PIC16F628A microcontroller. Timer 1 of PIC16F628A microcontroller is used as a counter in this project. Which measures time between two consuctive zero crossings. Timer measured by counter is divided by two because frequency of pulsating full bridge rectifier is double than sine wave frequency. All calculations are done in programming. After calculating frequency pic microcontroller displays measured frequency on LCD.

### Frequency controller using pic microcontroller

One can easily design frequency controller project by using sine wave frequency measurement project.  What does sine wave frequency controller do? Sine wave frequency controller measure frequency of sine wave and connect and disconnect load from ac power according to frequency. If frequency is greater or less than specific limit, it disconnects load from AC power source. For demonstration I have used three LEDs in this project as shown in figure above.

• Green LED : If frequency is equal to 50Hz green LED will glow
• Yellow LED: If frequency is grater than 50Hz yellow LED will glow.
• RED LED: If frequency is less than 50Hz red LED will glow

These LEDs are connected to PORTA of PIC16F628A microcontroller. You can use relay to isolated load from  AC power source in case of disturbance or change in AC power source frequency. similarly there are many other applications of sine wave frequency measurement project.

### sine wave frequency measurement project code

Code for sine wave frequency controller is written using Mikro C pro compiler. 8 MHz crystal oscillator is used in this project.

``````// LCD module connections
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;
// End LCD module connections

char *freq = " 00";
void Display_Freq(unsigned int freq2write)
{
freq = (freq2write/10) + 48; // Extract tens digit
freq = (freq2write/1)%10 + 48; // Extract ones digit
// Display Frequency on LCD
Lcd_Out(2, 11, freq);
Lcd_Out(2,14,"Hz");
if(freq2write==50)
{
PORTA.F0 = 1;
PORTA.F1 = 0;
PORTA.F2 = 0;
}
else if(freq2write>50)
{
PORTA.F0 = 0;
PORTA.F1 = 1;
PORTA.F2 = 0;
}
else if(freq2write<50)
{
PORTA.F0 = 0;
PORTA.F1 = 0;
PORTA.F2 = 1;
}

}
void main()
{
TRISA.F0 = 0;
TRISA.F1 = 0;
TRISA.F2 = 0;
PORTA.F0 = 0;
PORTA.F1 = 0;
PORTA.F2 = 0;
OPTION_REG = 0b00101000; // set TOCKI as clock counter

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);

Lcd_Out(1,1,"freqency controller");

while(1)
{

TMR0=0; // clear TMR0
Delay_ms(1000); // Delay 1 Sec
Lcd_Out(2,1,"FREQUENCY:");
Display_Freq(TMR0/2); // divide by 2
TMR0=0;

}// while
}// void main``````

program for frequency measurement is easy to understood. But if you still face any issue while using this code in your project, you can ask me by writing comment on this post.

### 20 thoughts on “Sine wave frequency measurement using pic microcontroller”

1. hello Bilal i’m trying to simulate your frequency detection simulation i also built the circuit and copied your code to my pic but some how it is says compiler error i used xc8 compiler. it would be great if u could assist me thought my project work.

• code is written using Mikro C complier

2. hi an well done for your very helpfull project…i try to run it with a simple sine vdd but it gives me wrong numbers…except of that why is so slow ?it must pass some seconds before it shows th frequency…thank you!!

• you can take average of values and use some delay

3. is it necessary to use pwm circiut for finding frequency?

• Do you have any other way? Yes you can use frequency to voltage converter circuits

4. hyy…I got wrong frequency value using your program code…can u help me??

• you must check crystal value you used

5. heyy bilal….I got corrcet frequency value in proteus simulation..but the values are wrong and changing when i check it in the hardware

6. bilal sir,why only pic microcontrollers are used for frequency detection?can we use other microcontrollers like 8051,AVR,ARM microcontrollers,…?

7. Sir, why have you used pic16f628a for hardware but changed it to pic16f887A for software?

8. Mr. Bilal Malik i want an urgent contact with u regarding frequency detection, can you share whatsapp no with me? [email protected]
sir it is very urgent please.

9. good day. i would like to know how to change the LCD display to a 7 segment display. this circuit functions very well as i have tried it. i would like to build it with a 7 segment display for visual purposes. please let me know if you can help me. thank you.

ron

10. compiler version wouldn’t matter if you provided the Hex file

11. Hi.I want to change the frequency of AC power by using microcontroller and the frequency is 10 to 25 kHz

12. here can we measure the frequencies over 256hz.?
if yes
how??