# ZCD with PIC 18LF45K40

I want to implement phase angle control for a single phase ac fan. To achieve that we need a ZCD circuit interfaced with a microcontroller to get a pulse at zero crossing. As our circuit has some space constraints, we have implemented the same using PIC 18LF45k40 which has built-in ZCD circuitry which generates pulses at Zero Crossing. Below is the circuit for implementing zcd-

The above circuit is referenced from Technical Brief document - Link here

As you can see, the circuit has a common ground with the AC Neutral which makes the circuit bit questionable as the whole circuit has to be treated as live and raises safety concerns. (Please correct me if I'm wrong)

Putting the above concern apart, the problem I'm facing is the pulses generated from microcontroller is not stable. It swings in the x-axis(time) in the oscilloscope. The code for generating pulse is as follows -

void __interrupt() ZCD(void)
{
if(ZCDIE && ZCDIF)
{
LATA1 = 1;
enableTimerInterrupt();
ZCDIF = 0;
}

if(TMR0IF)
{
TMR0IF = 0;
counter++;
if(counter==switchingValue)
{
counter=0;
disableTimerInterrupt();
LATA1 = 0;
__delay_us(200);
LATA1 = 1;
}
}

}


The ZCD Input when the ground is not connected with neutral is a plain DC line. When the ground is connected with Neutral, sine wave superimposes on the DC line as shown below-

The ZCD in pin gets the following waveform when neutral and dc ground are common -

Following are my questions -

1 - Is the ZCD waveform expected this way? There is a very limited resource available on this issue.

2 - The below is the circuit which I've implemented for Zero Crossing. Is this circuit safe and practical?

Triac circuit -

3 - Please go through the code and let me know the issue.

4 - Please suggest any circuit or app note which implements the zcd.

• Looks to me, from your second scope plot, that this isn't working very well. A ZCD should give you square edges, suitable for a digital input. All you have there is a smaller noisy sine wave. – Aaron Apr 4 at 15:30
• How is the 3.3V implemented? And with respect to what signal do you measure with your scope? AC neutral? Be careful touching the scope... – Huisman Apr 4 at 18:14
• @Aaron the second scope is the input waveform for the ZCDIN pin for the PIC Microcontroller. The Pulses are generated from a Digital Pin of the microcontroller depending on the ZCD interrupt internally by the Microcontroller peripheral. The code shown above is the one which generates the pulses for the OptoTriac. – Sai Prasad Apr 5 at 6:13
• The 3.3v is implemented using a 9V adapter followed by SMPS buck converter (LM2596 based). The circuit has the AC neutral and Ground connected together so basically its measured with respect to neutral. – Sai Prasad Apr 5 at 6:15