how to detect the zero crossing for an AC input signal

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
I want to determine time at every zero crossing for 5V AC input signal using the PIC micro-controller.in my search i found that I have to use interrupts to detect when the sine wave crosses zero. when interrupt will enable, timer will be start, it will run untill the interrupt become disable stop the timer and store time value in variable repeat this process continousely

I want measure the time taken the first crossing and store that value in variable then measure the time takan the second crossing. I want to measure time taken by every crossing

Does anyone know how to detect the zero crossing for an input signal?
 

MrChips

Joined Oct 2, 2009
34,908
For a 5VAC signal into a microcontroller, AC-couple the signal through a series capacitor and bias the signal to ½Vcc, i.e. at 2.5V.
Use an analog comparator with a reference set to 2.5V.
Instead of starting and stopping a timer, use the input capture function of the timer module.
 

danadak

Joined Mar 10, 2018
4,057
Preferred method is to use a comparator with hysteresis. To eliminate
effects of system noise on timing.

A photocoupler approach is also very good because of isolation from
line.

Simply ac coupling signal to a pin asking for trouble because of time
CMOS input spends in active/linear region and noise.

Regards, Dana.
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Thanks I am getting something

Which interrupt I have to use for AC signal to measure time I think I have to set comparator interrupt
 

BobaMosfet

Joined Jul 1, 2009
2,211
I want to determine time at every zero crossing for 5V AC input signal using the PIC micro-controller.in my search i found that I have to use interrupts to detect when the sine wave crosses zero. when interrupt will enable, timer will be start, it will run untill the interrupt become disable stop the timer and store time value in variable repeat this process continousely

I want measure the time taken the first crossing and store that value in variable then measure the time takan the second crossing. I want to measure time taken by every crossing

Does anyone know how to detect the zero crossing for an input signal?
You can find answers in the forums here. I pointed one out in this thread that will give you exactly what you want, and is opto-isolated. Does not use an OpAmp, that's just the question the original TP had asked about.

https://forum.allaboutcircuits.com/...-ic-to-measure-frequency.146659/#post-1251323
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
For a 5VAC signal into a microcontroller, AC-couple the signal through a series capacitor and bias the signal to ½Vcc, i.e. at 2.5V.
Use an analog comparator with a reference set to 2.5V.
Instead of starting and stopping a timer, use the input capture function of the timer module.
I think you are talking comparator interrupt. but I don't understand how to set interrupt for every crossing I mean asking for interrupt. which interrupt shall i use to measure time at every crossing.
 

DickCappels

Joined Aug 21, 2008
10,661
if I connect 230-5 v AC transformer to Pic16f877A. Will it damage to Microcontroller
Yes, probably unless you include some circuitry to protect the controller. A single resistor in series with the input pin will often be enough to protect the controller.

I have built a few using Pic, I used the AN958 app note for a starting point.
Also see the Fairchild AN-3006 for timing detection.
Max.
If you reference these application notes, be sure that your design includes a transformer to isolate your circuit from the power line. This will help protect both your controller and YOU from being harmed.
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
hi F174,
Curiosity, what is the project designed to do with all the sine periods it measures, what is its purpose.?;)

E
I don't exact because I am also helping some of my friend's. They want to determine time at every zero crossing for 5V AC input signal using the PIC micro-controller.

I searched on google and I am thinking that I have to use comprator of PIC. I have to set comprator interrupt. I am confused on connection I have to connect output of transformer to comprator input of PIC micrcontroler with series resistor. The input pins of comparator are with Input port pins RA0 through RA3

Where to connect the output of transformer to PIC(comprator)?
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
hi,
There are many circuits for mains zero crossing, I would choose the opto-coupler using a step down mains transformer that would give safe isolation.
Look thru these options, lets know which would suit your project.
E
https://www.google.co.uk/search?q=z...PM8rbaAhXkLMAKHc7SDUgQsAQIKg&biw=1396&bih=896
I think connection look like this

input 230 v Ac (Transformer ) output 5v Ac ----- series resistor ------ Pic MCU (comprator pin )

Is it necessary to use isolation circuit then I will go with opt-coupler
 

ericgibbs

Joined Jan 29, 2010
21,466
hi,
If you use a step down transformer to say 5V, I would say that Mr Chips circuit in post #3, would be one option.

The method I have used is to add a series diode after the full wave bridge rectifier, pick off the full wave rectified voltage, [un-smoothed] and drive a opto or a comparator.
This will give a pulse at every zero crossing, which can be used on say PORTB.0 of a PIC, using Interrupt on change.
Do you follow OK.
E
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Thanks ericgibbs

how to set comparator interrupt pic16f877a

this is demo code
Code:
void main (void)
{
    Initialize Comparator 1
   clear the CMP1 interrupt flag
    enable CMP1 interrupt
}

void interrupt isr(void)    
{
  
}
 

AlbertHall

Joined Jun 4, 2014
12,636
Interrupt on change can be a bit tricky. Better to use RB0/INT.
12.10.1
INT INTERRUPT
External interrupt on the RB0/INT pin is edge triggered,
either rising, if bit INTEDG (OPTION_REG<6>) is set,
or falling, if the INTEDG bit is clear. When a valid edge
appears on the RB0/INT pin, flag bit INTF
(INTCON<1>) is set.
 
Top