Odd AC Dimmer Behavior

Hello all. I am working on making a 120V 60HZ AC dimmer using the Arduino Mega2560.

On the input side, I am using a simple zero detect. AC in, 100K Resistor on each leg - full phase recifier - 22Ohm resistor - 4N35.

I am getting a good input i believe. I did a HIGH & Low pulseIn() and get about 4447 3866 respectivly.
1/(4447+3847 / 1000000) = ~120HZ. So I think the zero corssing signal is good.

On the AC end: pin 12 hooked to SSR-40 DA (FOTEK) & common gounded.

The theory was to trigger the SSR partially through the wave (typical AC dimmer) using attachInterrup of the Zero input. I am using the following CODE:

int ledPin = 12; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
  
  //Test the SSR
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);

  attachInterrupt(0, dimm, RISING);
}

void loop(){
  val = digitalRead(inPin);  // read input value
  Serial.println(val);
}

void dimm() {
  delayMicroseconds(1000);  //The Dimmer timer...
  digitalWrite(ledPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(ledPin, LOW);
}

This code yields random flickering. I am not sure what I am doing incorrect. Being that the SSR is triggered by a zero crossing (which gets triggered), i am not sure how the result could be out-of-phase. Any thoughts/suggestions. I am not quite sure where to go now.

I have also tried ths code in this site (only chnagign pins). Also random flickering.

Let me know if I missed any info.

Any help would be appreciated! Thanks!

Are you trying to dim a compact fluorescent lamp (CFL)? That will probably not work..

The SSR-40DA is itself a zero-crossing SSR, so it won't work as a dimmer. You need either a random-fire SSR or a triac and opto triac.

I've worked with triacs.
As noted, you need a triac and a triac driver (or a random-fire SSR)

Thanks for the advice all. I am using a incandescent, so no issue there. Also, I guess I thought tha azeroncrossing ssr meant that it powered off at the zero, not on. So, I will have to get a random fire ssr. If into a triac route I will have to get a 240 40A one, which I assume exist since they are wha power ssr in some cases. I would want to optoisolate it though.

Another thought though. If I have a code triggering off a zero cross, always waiting the same delay with a zero cross ssr it seems like it would always be on or off. Still shouldn't be random flickering.

Any thoughts?

Cement102:
Another thought though. If I have a code triggering off a zero cross, always waiting the same delay with a zero cross ssr it seems like it would always be on or off. Still shouldn't be random flickering.

Any thoughts?

If your 4N35 is connected between the input pin and ground, with a pullup resistor to +5V (either external or the internal one), then the rising edge will occur just before the zero cross. As you are only delaying 1ms before turning on the SSR, you may be triggering it sufficiently close to the actual zero crossing point to turn it on. If you use a longer delay, I think you will find that the lights stay off.

You can't use a delay function inside an interrupt call.

mlutteral:
You can't use a delay function inside an interrupt call.

That's not entirely true. delay() doesn't work inside an isr, but delayMicros() does. However, it's generally a bad idea to have delays longer than a few microseconds inside an isr because it prevents other interrupts being serviced.