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;
 }
 }
 }

[button-brown url=”http://store.microcontrollerslab.com/product/zero-crossing-detector-circuit-using-pic-microcontroller-proteus-simulation/” target=”_self” position=”center”]Click here to purchase proteus simulation [/button-brown]

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 🙂

33 thoughts on “zero crossing detector circuit using pic microcontroller”

  1. What is the difference between a ZCD realised using an Op-amp and a ZCD realised using a microcontroller? If we can create a microcontroller based ZCD why are Op-amp ones still in use?

    Reply
    • Using microcontroller you can use it for many other function which requires for power electronics circuits like firing angle control circuit. But you can also use op amp for same function. Microcontroller reduces required circuitry for designing any circuit

      Reply
  2. hi thank you but what component you use on the other side of the resistor and also i try to run it on multism the scem diagram but it doesnt run what did ya think the problem i made please tell me this two things

    Reply
  3. Hi

    Thanks for your information.. Can you please tell me whether this zero crossing circuit can be applied to STM8S Discovery micro controller also, If no can you please suggest for this zero crossing with STM8S Discovery micro controller…

    Reply
  4. Helpful discussion – I loved the specifics – Does someone know where I might be able to acquire a blank a form copy to edit ?

    Reply
  5. Dear sir,
    Might be i am so young.
    I see a project that is a circuit for control temperature and it also use zero cross. But i do not know why we use zero cross and what the zero cross processing in it. Could you explain me, please?

    Reply
  6. How to measure time at the output of Zero cross detector to find out frequency of voltage signal because f=1/2T so if we find out the time between zero crossing we can calculate frequency

    Reply
  7. Why npn transistor is giving pulses on zero crossing here? Shouldn’t it be always on because there is always positive voltage across base-emitter junction? Please explain.

    Reply
    • wow this is wonderfull, i really benefit frm this,
      can you please answer this questions
      * how do we now if the crossing is from negative to positive or opposite, and from my multimeter the emitter -collector voltage which is what i consider as the interrupt value is always giving me 4.04mV when i sent in 220Vac

      Reply
  8. I got previous one but, how will we determine whether the zero crossing is negative to positive transition of positive to negative transition of voltage?

    Reply
  9. Sir help me
    write an arm microcontroler program to find zero crossing in sine wave and display number of zero crossing upon occurance of an interrupt

    Reply
  10. I am using stm32f4 series , how we can detect the zero crossings of multilevel inverter using only the microcontroller without any kind of additional circuit using keil vision for programming.

    Reply
    • You have to use external components. You can give high voltage to a microcontroller directly. Use a step down transformer to convert 220-12 volts. Then use a voltage divider to further step down to less than 5 volts. After that convert negative cycle into postivie cycle using a bridge rectifier or op-amp ( by increasing dc level of AC) Then you can use this circuit.

      Reply
  11. How to differentiate between positive to negative and negative to positive because it will be very important a my three phase firing circuit

    Reply
  12. Hello Folks,
    I want to make program for ZCD using Atmega16, my aim is to make dimmable light without using PWM, Can anyone help me? What should I do? I have written sample program of ON and OFF which I have shown below.

    //#ifndef F_CPU
    #define F_CPU 16000000UL
    //#endif
    #include
    #include
    #include

    void init_ports()
    {
    DDRB=0x08; /* Make PORTB as output PORT*/
    PORTB=0x08; /* Write the value on PORTB*/
    }

    void init_interrupts()
    {
    cli(); //Disable global interrupt
    GICR=(1<<INT2); //Set interrupt2(INT2)
    MCUCSR=(1<<ISC2); //Set MCU Control and Status Register on Rising Edge
    sei(); //Enable global interrupt
    }
    ISR(INT2_vect)
    {
    PORTB=1<<PORTB3; //HIGH
    _delay_ms(50);
    PORTB=0<<PORTB3; //LOW
    _delay_ms(50);
    }
    int main(void)
    {
    init_ports();
    while(1)
    {
    init_interrupts();
    }
    return 0;
    }

    Reply
  13. sir , I have made a zero crossing circuit using your instructions. My simulation is working correctly but my hardware part is didn’t work. I have connected all the things according to your diagram. sir please help me

    Reply

Leave a Comment