[SOLVED]Interrupts and ZERO CROSSING DECTECTOR

Hi everyone,

I am trying/building a zero-cross dectector using an OP AMP as a comparator, I am facing an issue, as you can see on the attached picture the zero cross is happening when the signal is either going from 0V to 5V or 5V to 0V, my problem is my interrupts is firing at any moments between those 2 values, I would like it to fire at exactly the cross point, any pointers or idea would be really appreciated. Thanks

Code :slightly_smiling_face:

volatile byte inter = false;

void setup() {
  analogReference(DEFAULT);
  Serial.begin(9600);

  //Setting the triacs to output

  /*for (int i = 3; i <=7; i++)
  {
    pinMode(i, OUTPUT);
    Serial.println(i);
  }*/


  pinMode (interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2),zeroCrossing,CHANGE);
}

void loop() {

  if(inter){
    InsidInter();
    inter = false;
  }
}
void zeroCrossing() //Function to detect the zero crossing (0 to 10ms)TODO: calculate freq
{ 
  inter = true;
}

void InsidInter()
{ 
  Serial.println("Crossed");
}

Hi,
Have you actually physically constructed this circuit and tried it?

Have you Googled;

arduino zero crossing detector

Tom... :smiley: :+1: :coffee: :australia:

Hi Tom,

Yes I did, I don't have an oscilloscope to see the signal output but during my tests with my multimeter, when I have a signal that is more than +3.3V(HIGH) or just 0V (LOW), the zero-crossing interrupt stops firing, but when I am in between for example at 2.2V the interrupt keeps on firing.
Thank you for the link.
As for the googling part I did and found quite a lot of interesting articles one of them being from Texas Instruments where this circuit is based.

Reads like a switch debounce thingy might be useful.

1 Like

Add positive feedback from the output to the + input.

1 Like

At what point in the circuit are you measuring this voltage?

1 Like

I will give it a try it tonight when i get back to the circuit.

Output of the OP Amp to ground.
When i apply 3.3V from the arduino(when the main input is disconnected) the interrupts doesn't trigger(same with 0V).

When i don't apply anything i get around 1.8 V, i guess feedback is needed to have near 0 V when nothing is connected to non inverting input(+).

Not sure that you would need to feed a signal to the inverting input of the comparator.
Have a look at this data sheet from TI.
Page 3 shows the circuit sim. You should get an interrupt at every zero crossing.

Funny enough thats the one i used and trying to implement with Arduino. But i am not getting the interrupts at the wanted time. Even with positive feedback applied.

What is "wanted time" and what do you get instead?

1 Like

Basically what i am getting is a interrupt thats always fires whenever or not the input goes through 0V, i can see that by just disconnecting the VIN+ on the input.

Could you please clarify:

  • How are you measuring when the interrupt fires?
  • How many interrupts/sec are you getting?
  • How are you determining the relationship between when the interrupt fires and the AC waveform?

Disconnecting an input is a bad idea and it is not surprising the circuit is behaving erratically. You need to connect it to some known voltage to get predictable behavior.

What resistor values are you using in the physical circuit. I hope not 220 ohms and 100 ohms

For now my debugging is pretty basic, i just look at the frequency of the interrupts and they are the same as if the input is left open!
Tomorrow i will try to add a function to find the really frequency (hope it is near 50hz)

1 Like

I did try it by applying 3.3V and 0V and the result are kind good.

If i apply 0V no interrupts same with the 3.3V.

The problem is in between those 2 the interrupt keeps ln firing

I am using 220 and 100 Ohm resistors yes

That could be a problem as the current the 100 ohms is excessive.

However in the mean time, please provide more detail for

Basically what i am getting is a interrupt thats always fires whenever or not the input goes through 0V, i can see that by just disconnecting the VIN+ on the input.

What is VIN+ in this case and where are you disconnecting it?

Hi,
What op-amp are you using?
What supply voltage are you operating it at?

Have you considered the other easier solutions?
Is this for a particular application?

Thanks.. Tom... :smiley: :+1: :coffee: :australia: