One of the many issues with developing modern applications is to keep the spikes and EMI at a minimum, especially when switching AC mains in and out. Most of today’s new applications are controlled by one or more microcontrollers and this gives the possibility to prevent this noise in a simple and cost efficient way.
Noise produced during switching is dependent on the amplitude of the AC sinus at the actual switching point. To get this noise as low as possible the ideal switching would be when the amplitude is 0 volt. The amplitude is crossing 0 volt at the sinus “zero crossing”. Switching mains in and out at the zero crossing requires a way of detecting when the next crossing will be and launching a switching action at the crossing. This raises the need for a cost efficient way to detect the zero crossing. This application note explains how to do that.
Zero cross detection can also be used for other purposes, such as frequency calculation and relative phase measuring.
An example of how to make a zero cross detector with a ATmega16 is given here which is based on the appnote AN_2508.
- MPLAB X IDE v6.25 or later
- ATmega_DFP 3.4.282 or later
- XC8 (v3.00) alternatively AVR/GNU C Compiler 5.4.0 can be used
- ATmega16 TQFN
- STK600 (Not required but is used to hold the TQFN version of the chip)
- STK600 Routing card
- STK600 TQFP44
- ATMEL-ICE to program since programming over STK600 is not supported in MPLABX
- Logic analyser & wave generator to simulate mains and verify signal
- Connect the wavesource/wave generator to
PD2
. - Connect the logic analyser/output to
PB0
.
- Clone or download this repo.
- Open
atmega16-zero-cross-detector-mplab.X
in MPLAB. - Connect the ATmega16 with your programmer of choice, we used a Atmel ICE.
- Make sure the kit is selected as the tool to be programmed under project settings.
- Press the make and program button to program the device.
- Generate a sinus waveform with max
VCC+0.5V
onPD2
with a frequency of for example 50 Hz. - The logic analyser will then show a pulse train equivalent to that frequency on
PB0
.
We have shown how you can make a simple zero cross detector with a ATmega16. For more detailed workings about this example please refer to the appnote AN_2508.