zero crossing detector circuit using pic microcontroller

Zero crossing detector circuit is design to detect zero crossing of sine wave. It is used for AC Power control circuits. If you are an electronics engineer and you are working on power electronics projects, you may come across many situations where you have to read frequency of sine wave or you want to detect zero crossing of sine wave. Whenever sine wave cross from positive cycle to negative cycle or negative cycle to positive cycle. You can also detect zero crossing of sine wave with the help of simple operational amplifier. I have already posted an article on zero crossing detection using op-amp.

But in this article I am going to discuss how to detect zero crossing using pic16f877a microcontroller and some other simple electronics components. There are many project on zero crossing detection available on many websites. But none of them provides complete information about zero crossing detector circuit. In many power electronics projects you want to use zero crossing detection and also a microcontroller. For example zero crossing detector circuit is used in sine wave frequency measurement. In digital frequency meter, you want to display frequency value on liquid crystal display. So you must need a microcontroller which can measure time between two consecutive zero crossing detection. This measured time can be used to measure frequency of sine wave after some manipulation in program. I will try to post a separate article on sine wave frequency measurement. To get updates on upcoming articles in your inbox, subscribe to our blog. you may also like to read




Main components used in zero crossing detector circuit :

Following main components are used in this electrical project. Brief description of these components are also given below:

Voltage source:

220volt or 120 volt sine wave voltage source which you want to detect zero crossing using zero crossing detector circuit using pic microcontroller.

Voltage limiting resistors:

Two voltage limiting resistors are used to limit voltage. Maximum voltage drops across these resistors. AC voltage less than 5 volt appear across full bridge rectifier. Now the question is why we need voltage limiting resistors in this circuit. Because microcontrollers can not read voltage more than 5 volt or voltage more than 5 volt damage microcontroller permanently.  Two 220K resistor are used as a voltage limiting resistors in this project. You can use quarter watt resistors, because current flow is in the order of micro amperes through voltage limiting resistors.




Bridge rectifier:

After voltage limiting resistors, full bridge rectifier circuit is used. It is used to convert negative cycle into positive half cycle or it is used to convert AC voltage into pulsating DC voltage. Because after full bridge NPN transistor is used as a switch which turn off only on zero crossing.

NPN transistor:

NPN transistor is used in this project as a switch which provides microcontroller positive edge interrupt during zero crossing. You can use any NPN transistor. I have used 2N3904 NPN transistor in this project.

PIC16F877A microcontroller:

PIC16F877A microcontroller is main part of this project. because it measured zero crossing with external interrupt. Whenever sine wave cross zero point, positive edge interrupt is produce at the output of NPN transistor. Microcontroller measure this interrupt and generate a small pulse at PORT D pin number zero or PD0.

Circuit diagram of zero crossing detector circuit using pic microcontroller:

Circuit diagram of zero crossing detector is given. I have tried to explain all components of zero crossing detector circuit above. But if you still have any confusion, your comments are welcome.

zero crossing detector circuit

circuit diagram of zero crossing detector using pic microcontroller

Microcontroller PIC16F877A used external interrupt to read zero crossing point of sine wave. If you don’t know about programming of pic microcontrollers and want to learn pic microcontroller, check following articles:

Simulation result of zero crossing detector circuit:

Simulation results of zero crossing detector circuit is shown in figure below. Labels also shown about zero crossing, pulse and interrupt.

zero crossing detector circuit

zero crossing detector circuit

List of components for zero crossing detector circuit:

Category,Reference,Value,Order Code
Resistors,"R1",330,
Resistors,"R2",10k,
Resistors,"R4",10k,
Integrated Circuits,"U2",PIC16F877A,
Transistors,"Q1",2N3904,
Miscellaneous,"BR1",2W04G,
Miscellaneous,"X1",CRYSTAL,

Code for zero crossing detector circuit:

Code given below is written using Mikro C compiler. Necessary comments are also available in code for your understanding.

unsigned char FlagReg;
 sbit ZC at FlagReg.B0;

void interrupt()
 {
 if (INTCON.INTF){ //INTF flag raised, so external interrupt occured
 ZC = 1;
 INTCON.INTF = 0;
 }
 }

void main() {
 PORTB = 0;
 TRISB = 0x01; //RB0 input for interrupt
 PORTD = 0;
 TRISD = 0; //PORTD all output
 OPTION_REG.INTEDG = 0; //interrupt on falling edge
 INTCON.INTF = 0; //clear interrupt flag
 INTCON.INTE = 1; //enable external interrupt
 INTCON.GIE = 1; //enable global interrupt

while (1){
 if (ZC){ //zero crossing occurred
 PORTD.f0 = 1; //Send a 1ms pulse
 delay_ms(1);
 PORTD.f0 = 0;
 ZC = 0;
 }
 }
 }

I hope after reading this article you will be able to design your project. For more projects updates keep visiting our blog or subscribe to our blog via email. Thanks 🙂

11 Comments

  1. RAHUL August 9, 2015
    • Bilal Malik October 14, 2015
  2. Shailesh Tripathy October 11, 2015
    • Bilal Malik October 14, 2015
  3. Magnoum March 4, 2016
    • BILAL Malik March 5, 2016
  4. A May 14, 2016
  5. Shyama Sravani July 12, 2016
  6. ramana July 30, 2016
  7. andi cooper August 21, 2016
  8. Meng Kimsros August 25, 2016

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 706 other subscribers