Zero Cross Sensing with PulseIn()

Iv'e read that there is function called PulseIn() in the arduino but not much talked about. coud somebody tell me more about it.

This is what it says in description

eads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or 0 if no complete pulse was received within the timeout.

So does that mean it can be used to sense the Zero crossing from a fully rectified 9v transformer and a 1kOhm resistor in series and depending on the value of the Microseconds a pin can triggered to run a relay.

No, It can't handle negative voltage and the switch point is around 2 V.

You'd have to clean the sine wave up with a Schmitt trigger and remove the negative component, but I doubt it would be any good for zero crossing detection, you could maybe get the period but you already know that.

With some experimenting you could determine the time from the pulse to the zero point, but it would vary with different components.

Anyway, why do you need zero crossing to drive a relay? Normally you do this to cut down noise when switching with a triac.

EDIT: Just noticed "fully rectified", OK no negative component, but still over voltage.

Fully rectified makes it an even worse zero crossing detector.

True.

So does that mean it can be used to sense the Zero crossing from a fully rectified 9v transformer and a 1kOhm resistor in series and depending on the value of the Microseconds a pin can triggered to run a relay.

With 9VAC (not rectified) you could just use a series connected 33K to 100K resistor. One end connected to 9VAC and the other connected to an input pin or interrupt pin. The zero cross would be close to when the pin changes state. The rise time would be a little late and the fall time would be a little early, but this could be determined and corrected for.

With the relay chattering at 50 to 60 Hz, the contacts are gonna arc a bit... Not gonna be a happy relay......he he he...

Zero crossing detection using an optoisolated device like the H11AA1 is easy, safe and inexpensive.

If you have a proper resistor in series with the input, such that the current through the protection diodes is very low when it's negative, the negative voltage is okay - atmel's own app notes describe using a 1 Meg resistor from an analog pin to mains AC for zero cross detection!

Hi,

Using a 9vrms AC transformer and sensing the zero crossing at 2v means a loss of angle of about 9 degrees out of the 180 degree half cycle. Amazingly, this only represents a power loss of about 0.1 percent of the full power if the triac is delayed by 9 degrees.
The switching transient may cause some extra noise however, although for a 120vac line it will switch at about 18v which isnt too bad.

From what i know about pulseIn is that when you call it with the right argument it will wait for the next transition before returning, therefore it might work, but only if done right. A question that comes up though is do you really need that function. The answer is probably not, because any pin can detect when the level has changed from a high to a low and vice versa.

Also, for a full wave rectified sine you can detect zero crossing as long as there is a load resistor that takes the full wave down close to zero close to the actual zero crossing points. With no load any capacitance at all will prevent this from happening.
If the full wave is filtered with a capacitor however, it will never work. That means it would have to be isolated from the filter cap(s) with another diode. The filter cap will then filter normally while the other side of the diode will go from 0v up to about 13v peak, then back down again, then repeat, looking like the positive half cycle of a sine wave for both half cycles.

If you intend to compensate for that loss of 9 degrees however you have to be very careful because the timing for that would come from the PREVIOUS cycle, where the present half cycle time could be slightly different than the previous one. This could cause false triggering if we tried to get it exact. The specifications of the line frequency would have to be reviewed to find out how much error could possibly exist between one cycle and the next, and whether or not we use an adaptive strategy.
Probably the best bet would be to detect the falling edge of the previous half cycle and start the delay at that point, maintaining some minimum delay time.

Luckily, the frequency is a known and stable quantity. If 50Hz sine, the true positive cycle is 10ms. If the measured interval is 9 degrees late on rising edge and 9 degrees early on falling edge, you would measure 9ms for the pulse width. To correct for this, the true zero crossings would be at (riseTime - 500µs) and (fallTime + 500µs). Using an external interrupt would be a good way to determine the pulse rise and fall timing.

dlloyd:
With 9VAC (not rectified) you could just use a series connected 33K to 100K resistor. One end connected to 9VAC and the other connected to an input pin or interrupt pin. The zero cross would be close to when the pin changes state. The rise time would be a little late and the fall time would be a little early, but this could be determined and corrected for.

So this is interesting, can't wait to try this out. i have a few 330k resistors lying around. Would that be ok, would there be significant loss anywhere ? To begin with, am guessing the the signal would go into the digital port of the arduino, am i rite on that or is it analog. my mains are 220v at 50Hz. Now to detect it, i should check the state of the pin so that would be a 1 or 0. 1 being high that is when the wave reaches zero, rite ?

Nope the relay was just an example, my load would be a MOT (Microwave Oven Transformer)1100A when shorted and 3v when open. Waiting for a 22Ohn 50W resistor which go in series with the primary.

Zero crossing detection using an optoisolated device like the H11AA1 is easy, safe and inexpensive.

Yea i did come across a lot of them. but was not sure which one to go with and the H11AA1, if am not wrong is outdated and these days the MOC3083M are used. the zero is completely new to me so i'am learning these before i start with the circuit. Since i could not find one i came down to having a resistor in series and just using delay() for a 50 - 100ms would do the job, but i would go for the zero crossing always since i understand a little better now.

One of the members here had made a this and that how i learn to start off with the idea. So he has the one of the 9v ac into pin 3 where he detects the zero cross with the arduino sketch(can't seem to find, guess its in his library, Albert.h mentioned in his site) and fires the SCR with an optocoupler.

So i'll try to figure out the code for the zero crossing. So how much volts on the input of any pin would be ok for the arduino to sense the input.

Connecting AC through a current limiting resistor to an analogue pin also might work.
Pick resistor value so peak fault current stays under 1mA.
Read the pin as often as you can during an expected zero crossing window.
Anything with a value of 0 is one state, and with a value >= 1 is the other state.
With 1.1volt Aref enabled you can have a switching window of 1mV.
Two schottky diodes from pin to ground could symetrically clamp AC to +/- 0.4volt.
Leo..

You could use several 330K in parallel to get 150K. This would give a better signal and would still allow the input to protect itself from overvoltage. I would suggest using an interrupt pin to get more accurate timing information.

Note: Make sure the transformer has an isolated output. Is it center tapped? If so, would need to connect the isolated center tap to GND.

dlloyd:
I would suggest using an interrupt pin to get more accurate timing information.

Problem with using a digital pin is switch levels. 0.3x and 0.6x VCC.
The 9volt sine has to rise above 3volt to register a HIGH, and fall below 1.5volt for a LOW.
Zero crossing has to be centered around 0volt.
Leo..

Not a problem really, as its a known constant. It only changes the ratio of correction for the leading and trailing edge. Yes, the sine signal needs to be centered at 0V ... perhaps the transformer has no center tap and is not compatible. In any case, the suggestion in reply#7 provides a more accurate solution. A marginally more accurate way Atmel's method, but this is not needed (especially with 220VAC mains).

I don't know how "constant" those switchpoints are.
They could be temp dependent, and they surely are VCC dependent.

Zero crossing detection could also be done with an opamp/comparator with common mode that includes ground, like the LM393.
Leo..

anishkgt:
So does that mean it can be used to sense the Zero crossing from a fully rectified 9v transformer and a 1kOhm resistor in series and depending on the value of the Microseconds a pin can triggered to run a relay.

A fully rectified 9V transformer is not a signal. You need to detect zero crossings of a sine-wave waveform (usually).

Assuming the sinewave signal is a bit noisy..... you'll maybe need a circuit that doesn't get a heap of false triggers when the noisy sinewave gets to around zero Volt. Noise can make the 'zero' Volt value keep changing between small positive to negative values during.... and could make the output false trigger a number of times until the signal gets high enough to get clear of the triggering region. So a hysteresis circuit (schmitt trigger circuit) should come in handy, as somebody suggested already.

The output of the hysteresis circuit will usually be a nice square wave, where an edge of the square wave will usually be associated with the zero crossing event.

The hysteresis feature can also be implemented either in hardware or software.

So once i ship those transformers will start off with the zero crossing.

Hysteresis can be done in software.
Not so different from debouncing a switch.
Leo..