AC phase shift after zero corss- not working [SOLVED]

Hello All,

Trying to dim a load such as bulb using a constant value after zero cross. Can't figure out what is wrong here. The bulb just stay ON in full power.

const byte ZERO_PIN = 2; //Input pin from zero cross detector
const byte TRIAC = A1; //Output pin to TRIAC / SSR
volatile boolean zeroCrossingFlag = false;
//int SET_PIN = A3; //Analog pin for setting the dutyCycle value with a pontentiometer

void setup()
{
  attachInterrupt(0, setFlag, FALLING); // zero cross
  pinMode(ZERO_PIN, INPUT);
  pinMode(TRIAC, OUTPUT);
}
void setFlag()
{
  zeroCrossingFlag = true; // interrupt sets flag true
}

void loop()
{
  zeroCrossingFlag = false;
  while (!zeroCrossingFlag)
  {
    delayMicroseconds(2000);
    digitalWrite(TRIAC, HIGH);
    delayMicroseconds(200);
    digitalWrite(TRIAC, LOW);
  }
}

You have the bracketing wrong. You want to trigger the triac after you break from the while loop on a zero crossing, not while you are waiting for the zeroCrossingFlag to be true.

void loop()
{
  zeroCrossingFlag = false;
  while (!zeroCrossingFlag) {}
    
   delayMicroseconds(2000);
   digitalWrite(TRIAC, HIGH);
   delayMicroseconds(200);
   digitalWrite(TRIAC, LOW);
  //}
}

initially i had it like that but the TRIAC would not switch on.

Can you show a circuit diagram, and the Triac data sheet (link)?
Eventually you have to revert the polarity, i.e. start at the raising edge instead of the falling zero crossing edge.

Data sheet to the triac

SCR schematic attached. I am just copying the same part of the code that works in another project on the same PCB. The spot welder PCB. :smiley:

Its only here that the code is not working.

The circuit looks good to me. The data sheet is for a thyristor, but the diagram reveals an opto triac that will work regardless of polarity.

I suggest that you test the code on another board, so that defective components can be excluded. If you have a scope at hand, check the signals. If not - too bad :frowning:

what bulb?

Juraj:
what bulb?

aka incandescent lamp. in short we indians call it BULB :slight_smile:

nothing happening

const byte ZERO_PIN = 2; //Input pin from zero cross detector
const byte TRIAC = A1; //Output pin to TRIAC / SSR
volatile boolean zeroCrossingFlag = false;
//int SET_PIN = A3; //Analog pin for setting the dutyCycle value with a pontentiometer

void setup()
{
  pinMode(ZERO_PIN, INPUT);
  pinMode(TRIAC, OUTPUT);
  attachInterrupt(0, setFlag, FALLING); // zero cross
}
void setFlag()
{
  zeroCrossingFlag = true; // interrupt sets flag true
}

void loop()
{
  zeroCrossingFlag = false;
  while (!zeroCrossingFlag) {}

  delayMicroseconds(2100);
  digitalWrite(TRIAC, HIGH);
  delayMicroseconds(200);
  digitalWrite(TRIAC, LOW);
}

the code that works in another project on the same PCB.

Certainly suggests a hardware problem.

If you have a scope at hand, check the signals. If not - too bad :frowning:

If you can flash an led (as well as turn on the triac) in the code following the exit from the while loop() you can determine if you are getting the zero cross interrupt.

figured out it was that i had missed to set the pullup resistor in the code for Zero_Pin

anishkgt:
figured out it was that i had missed to set the pullup resistor in the code for Zero_Pin

If you had shown the zero crossing detector on your schematic we might have been able to tell you that a pullup was required.

That goes to show that you should always give us all the information.

Yes i understand, my bad. I was a bit carried away about the fact that it was just a copy paste from my existing code and it would work straight.

anishkgt:
aka incandescent lamp. in short we indians call it BULB :slight_smile:

I asked, because some try to dim an AC LED bulb (12 V LED with switching power supply).