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

sine wave frequency measurement

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[1] = (freq2write/10) + 48; // Extract tens digit
freq[2] = (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.

• BILAL Malik July 7, 2015
2. johny January 18, 2016
• BILAL Malik January 18, 2016
• BILAL Malik January 31, 2016
4. anand January 31, 2016
• BILAL Malik January 31, 2016
5. anand April 6, 2016
6. I.saikrishna October 8, 2016
7. rashi October 21, 2016
8. Ali February 27, 2017
9. Taofeeq March 8, 2017
10. ronald chapman June 5, 2017
11. r smoth August 21, 2017