I am trying to create a phase control AC dimmer. But I am not able to achieve smooth dimming. I am using this guide for the circuit:
On the zero detection part I am able to detect its zero crossing, but the output of the zero detection circuit is not a perfect square wave, it's kinda sinoidal.
So I have created my own code that when it reaches a value above 900 (very close to 0v) it will trigger the triac. I am using an Arduino Micro:
#define TPulse 10
#define SW 7
#define ldr A0
#define zeroDetector A1
int x;
int delayDuration = 100;
long int count = 0;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
digitalWrite(2, HIGH); // pull up
pinMode(TPulse, OUTPUT);
pinMode(SW, INPUT);
digitalWrite(SW, HIGH);
digitalWrite(TPulse, HIGH);
}
void loop() {
delayDuration = map(analogRead(ldr), 0, 1024, 0, 9000);
x = analogRead(zeroDetector);
if (x <= 900)
isr();
}
void isr () {
digitalWrite(TPulse, LOW);
delayMicroseconds(delayDuration);
digitalWrite(TPulse, HIGH);
}
I have calculated that in a 60Hz AC it will cross zero voltage 120 times a second thus the delay should be in range of 0 - 8300 microseconds.
But I am not able to achieve a smooth dimming, I am getting either of, flickering, fading (uncontrolled), and so on.
Is there something wrong with my analogy or am I missing something crucial?
I have replaced the LDR with a trimmer pot so I can easily adjust the resistance.
I trying to not use interrupt as of the moment. I am trying to achieve this type of dimming:

digitalWrite(TPulse, HIGH);should be followed bydigitalWrite(TPulse, LOW);....... the triac shuts itself off automatically at zero crossing once it is triggered ..... you only need to send it a short pulse to trigger it – jsotola Mar 12 '19 at 22:16