Zero Cross Detect

Last modified by Microchip on 2023/11/09 09:03


The Zero Cross Detect (ZCD) module is a peripheral within select PIC® microcontrollers, that can be used to monitor when an A/C signal crosses through a zero cross detect threshold. It is connected to the designated ZCD pin on the device through a current-limiting series resistor.

The ZCD module is useful when monitoring an AC waveform for, but not limited to, the following purposes:

  • A/C period measurement
  • Accurate long-term time measurement
  • Dimmer phase delayed drive
  • Low EMI cycle switching

zcd example

On the PIC Microcontroller, the zero cross-detect threshold is offset slightly from true zero volts. The actual zero crossing threshold is defined in the electrical characteristics of the device datasheet as the ZCPINV value. This value is the voltage level that sets the zero cross detection point. Any signal above that value will require the PIC MCU ZCD pin to sink current and any voltage below it will require the PIC MCU ZCD pin to source current.


ZCD Output Operation

The ZCDOUT bit in the ZCDCON Register will switch between a logic zero and logic one setting when the ZCPINV threshold is crossed. The output polarity may be changed through the ZCD control register setup but the main output of the ZCD is a logical bit setting that can be monitored in software or trigger an interrupt.

zcd wave

Current Sink/Source

The ZCD relies on circuitry inside the ZCD module to either sink or source current to maintain the VCPINV voltage at the I/O pin. This is key to understanding how to use the ZCD module and there are limitations on how much current the ZCD module can sink or source.

When the external applied voltage is greater than the ZCPINV voltage, the ZCD module sinks current.
When the external applied voltage is less than the ZCPINV voltage, the ZCD module sources current.

zcd operation

The ZCD module is shown in the simplified block diagram below. The zero cross point voltage is set by the comparator input which is shown here as the ZCPINV voltage.


ZCDxCON: Zero Cross Detection Control Register

All operations of the ZCD are controlled through the Zero Cross Detection Control Register (ZCDCON). It's through bit settings in this register that the ZCD can be enabled and also the output indicator signals controlled.


zcdxcon bits

Back to Top

Enabling ZCD

The ZCD module can be enabled in software by setting the Zero Cross Detect Enable (ZCDEN) bit in the Zero Cross Detect Control (ZCDxCON) register. It can also be automatically enabled after a Power On Reset (POR) by clearing the Zero Cross Detect Disable (ZCDDIS) bit in the Configuration register.

Back to Top

Selecting the Current Limiting Resistor

The ZCD module requires a current limiting resistor in series with the external voltage source. It's through this current limiting resistor that the ZCD module applies a current source, or sink, to maintain a constant voltage on the ZCD pin. The series resistance needs to be sized to limit the input current to 300 microamps (300 ua).

The resistance and rating of this series resistor depend on the external signal source peak voltage. Select a resistor value that will drop all of the peak voltage when the current through the resistor is 300 uA. An example of the resistor calculation is given below.


If a 240 Vpp sine wave signal is detected;

(1) SeriesResistor = 240 / 0.0003 = 800k

(2) PowerRating = i2 ∗ r = (0.0003)2 ∗ 800k = 0.072 watt (use 1 watt)

Back to Top

Logic Output

The ZCD can be monitored through a bit in the ZCDCON register. The bit is the ZCDxOUT bit and it will indicate if the ZCD pin is sinking or sourcing current. The state of the bits can be controlled by the polarity bit ZCDxPOL in the ZCDCON register.

If ZCDxPOL = 0 then:
ZCDxOUT = 1 when the ZCD pin is sinking current which means the input signal is higher than the ZCD threshold.
ZCDxOUT = 0 when the ZCD pin is sourcing current which means the input signal is lower than the ZCD threshold.

If ZCDxPOL = 1 then:
ZCDxOUT = 0 when the ZCD pin is sinking current which means the input signal is higher than the ZCD threshold.
ZCDxOUT = 1 when the ZCD pin is sourcing current which means the input signal is lower than the ZCD threshold.

Back to Top


The ZCD can trigger an interrupt to indicate that a zero cross has been detected. The Zero Cross Detect Interrupt Flag (ZCDIF) in the Peripheral Interrupt Register 3 (PIR3) will be set to a one if a zero cross occurs.

Further, the interrupt circuitry can control what signal is indicated by the ZCDIF bit.

Rising and Falling Edge Interrupts

To only set the ZCDIF bit on a rising edge crossing then the ZCDxINTP bit in the ZCDxCON register is set to a one.

To only set the ZCDIF bit on the falling edge crossing then the ZCDxINTN bit that is set to a one in the ZCDxCON register.

Setting both ZCDxINTP and ZCDxINTN bits will trigger an interrupt in either direction.

Interrupt Enable

The Zero Cross Detect Interrupt Enable (ZCDIE) bit must be set in the Peripheral Interrupt Enable 3 (PIE3) register to enable ZCD interrupts. The Peripheral Interrupt Enable (PIE) and Global Interrupt Enable (GIE) bits must also be set for the ZCD interrupts to work.

Back to Top

Correcting for ZCPINV offset

The ZCPINV threshold voltage prevents the zero cross from being detected at true zero volts. This can cause detections that are too early or too late relative to the true zero voltage crossing. This may be critical to low input voltages such as 24 volts or less. To correct for this a pull-up resistor placed between the current limiting resistor and the Zero Cross Input pin can erase the ZCPINV offset.

The formula for sizing that resistor is as follows:

(3) Rpullup = Rseries(Vpullup − ZCPINV) / ZCPINV

First, we calculate the series resistance for the 24 Vpp sine wave:

(4) Resistor = 24 / 0.0003 = 80k

Then calculate the Rpullup value using a ZCPINV value of 0.75v and pull-up resistor connection to a Vdd of 5v:

(5) Rpullup = 80k(5v − 0.75v) / 0.75v = 453.3 kohms

Therefore, for this example, a 453k 1% resistor works well.

Back to Top

Setting a Zero Cross Offset

For higher voltages, there may be times when the cross indicator is actually desired at a higher voltage than zero. This can be accomplished with a pull-down resistor. For example, consider a design where the input signal has a peak voltage of 300 V. This would require a 1 megohm resistor to limit the current to the 300 ua the ZCD module requires. The zero cross point will occur when the input voltage at the ZCPINV voltage of 0.75 volts or near zero relative to the 300v peak.

Now if a 7.5k resistor were added from the input pin to ground, the 7.5k resistor would require the ZCP module to source current through the 7.5k resistor to maintain the 0.75 volts ZCPINV level. That current would be equal to:

(6 ) 0.75v / 7.5k = 100ua

The input signal would need to supply that 100 ua extra current to balance out the ZCD input so it will not source or sink current. When the input signal is at 100 volts, then the 1 meg resistor will limit the current to 100 ua thus supplying the 100 ua for the 7.5k pull-down. The ZCD can consequently maintain the ZCPINV voltage without sourcing or sinking current.
The pull-down resistor has shifted the zero cross point from near zero volts to near 100 volts thus offsetting the zero cross detection point.

The same technique can be used to offset the zero cross detect point in the negative direction with a pull-up from the ZCD pin and Vdd of the PIC MCU.

Back to Top

Sleep and Reset


The ZCD input will still sink and source as normal and will trigger an interrupt during sleep mode.


A reset will disable the ZCD unless the ZCDDIS bit is cleared in the configuration register. If cleared the ZCD will start operating after a POR. If this configuration bit is set, then the initialization software must set the ZCDxEN bit in the ZCDxCON register to enable the ZCD module.

Back to Top