Arduino Triac Dimmer Flicker

Hello,

I am attempting to control a dimmable AC LED via triac and zero cross interrupt. I have a basic test sketch with conservative timings to make sure that overshoot wasn't the issue (60hz mains). The circuit works perfectly when given an external power supply (e.g. FTDI programmer). However when I use a 5v transformer (cheapo from Amazon) connected in parallel to the same AC circuit I get substantial flickering. I've attempted both 3v3 and 5v Arduinos to test using it with or without the onboard regulator. I've tried with and without the 100uF capacitor across my 5v supply (I thought it might help filter if transformer was unstable).

The transformer is a prebuilt circuit including an MB10F bridge rectifier.

The 5v reads 5.13V on my multimeter. I've also tried with another cheapo 12v transformer instead. It feels like there is something basic I'm missing. The schematic is attached and here is the code:

int lamp = 3;
int dimVal = 128;
int state;

void setup()
{
    pinMode(lamp, OUTPUT);

    attachInterrupt(0, zeroCross, RISING);
}

void loop()
{
    int val = analogRead(A0);
    dimVal = map(val, 0, 1023, 0, 120);
}

void zeroCross()
{
    int t = dimVal * 65;
    delayMicroseconds(t);
    if (dimVal < 120)
        digitalWrite(lamp, HIGH);
    delayMicroseconds(20);

    if (dimVal > 0)
        digitalWrite(lamp, LOW);
}

Thank you for any help you can offer!

Maybe I am missing something: if it is a bare transformer you must rectify the output before powering Arduino. Do you have a scope?

You have the load connected to the gate of the triac. The load should be between MT2 and live, with MT1 to neutral, opto triac for triggering between MT2 and gate.

I'm surprised it works at all the way you have it.

You appear to be powering the Pro Mini from AC, I am surprised the room isn't full of smoke.

Instead of that transformer thingy use a mobile phone charger. Those you know produce pretty clean 5V DC. It sounds like you're having a very poor power source.

Hi,
Do you have a rectifier, filter capacitor and voltage regulator between the transformer secondary winding and the Pro Mini?
Tom... :slight_smile:

I apologize for the mistake. I had the triac wrong on the schematic and I wasn't using an accurate symbol for the transformer. I have redone it as a black box. It is a full AC-DC converter circuit. (https://www.amazon.com/gp/product/B07YXN8J6R/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1).
I was hoping that using 5V or 12V on a 3v3 arduino and going through the RAW pin for the onboard regulator would be sufficient.

wvmarle:
Instead of that transformer thingy use a mobile phone charger. Those you know produce pretty clean 5V DC. It sounds like you're having a very poor power source.

When it worked correctly, I was using a phone charger connected to an ftdi programmer for a quick source test. Unfortunately the goal of the project is to be self contained within/next to a wall switch housing with no external wires. If it is just a poor source then theoretically I should be able to clean it right?

Hi,
What output voltage is the Amazon power supply?
If it is 5V then you need to connect it to the Vcc pin of a 5V Pro Mini.

5V connected to Vraw will not provide sufficient voltage for the 5V onboard regulator.
5V to Vraw of 3V3 ProMini will possibly not provide enough voltage to the 3V3 on board regulator.

Tom.... :slight_smile:

TomGeorge:
5V to Vraw of 3V3 ProMini will possibly not provide enough voltage to the 3V3 on board regulator.

I have had great success with running 3.3V Pro Minis at 5V (connected through FTDI). Just remember to set the IDE to the 8 MHz/3.3V option, not the 5V/16MHz option, as otherwise all timing is off.

So that may be OPs solution as well: just run the thing straight off 5V.

I’ve never tried powering a 3.3V Pro Mini through the raw pin. Come to think of it, I don’t think I’ve ever used that pin to begin with… when testing it’s USB all the way, if the final project is using say 12V DC I’ll grab a buck converter to power the Arduino and peripherals.

Using DMM what is voltage between Vcc and GND?
Can you trace the source of the flickering? Is it wrong ZC detection? Firing to soon or too late?

I think some noise either affects your ZC detection or it causes unwanted triggers of the triac.

The 10k pullup on the opto isolator reading the zero-cross is a very high value - this will mean triggering
may be sensitive to noise in the circuit.

For a good clean fast switching of an opto coupler something more like 470 ohm could be
worth trying.

Is that opto coupler not bipolar? You normally want to trigger on every zero crossing, not
just every mains cycle.

You also need to keep all the mains side of the circuitry well away from the logic to reduce the
risk of EMI radiated by mains wiring (typically due to switching transients on the mains from
other equipment in the building - these can be 100's or 1000's of volts even, enough to induce
capacitive pick up on nearby wiring.

Screening all the logic circuitry in a metal box is a great way to reduce the risk of EMI issues too.

For a good clean fast switching of an opto coupler something more like 470 ohm could be
worth trying.

BANG!

MarkT:
The 10k pullup on the opto isolator reading the zero-cross is a very high value - this will mean triggering
may be sensitive to noise in the circuit.

For a good clean fast switching of an opto coupler something more like 470 ohm could be
worth trying.

Is that opto coupler not bipolar? You normally want to trigger on every zero crossing, not
just every mains cycle.

The DF04M is a bridge rectifier which should allow the opto coupler to catch both rising and falling zero crossings right?
I chose the 10K resistor to achieve roughly 10ma across the opto coupler which has a forward voltage of 1.3V. It looks like I can go to around 2.2K without passing the max current (60ma) of the 4N25 so I'll try that.

enragedflamingo:
The DF04M is a bridge rectifier which should allow the opto coupler to catch both rising and falling zero crossings right?

True. It's just uncommon to do it that way - more common is using an AC optocoupler such as the HHAA1.

I chose the 10K resistor to achieve roughly 10ma across the opto coupler which has a forward voltage of 1.3V. It looks like I can go to around 2.2K without passing the max current (60ma) of the 4N25 so I'll try that.

100-200k is more common in this application - the 5W part is why. That's a lot of heat! 1 mA is ample for the opto to work well, doesn't leave you to deal with all the heat. Use a 1k pull-up; maybe 470Ω even. Check the CTR of your opto (it's in the datasheet).

wvmarle:
100-200k is more common in this application - the 5W part is why. That's a lot of heat! 1 mA is ample for the opto to work well, doesn't leave you to deal with all the heat. Use a 1k pull-up; maybe 470Ω even. Check the CTR of your opto (it's in the datasheet).

Thank you for clarifying, I misunderstood which resistor you meant before. The CTR is about 0.7 at my current 10ma. I tried it now with both a 470 and 1k. No change.
However, when I touch my DMM ground probe (with or without the positive probe as well) to the GND pad on the arduino, the flickering stops. I read 5.04V at VCC btw. I'm really confused because I didn't think a ground external to the circuit could have an effect like that.

Are you sure all grounds are properly connected?

Also that "flickering", how is that exactly? On/off or changes in brightness, or something different?

Are you distinguishing Line (Hot) vs Neutral properly in your circuit?

PE - If there’s a Hot/Neutral swap and something in the mix is earthed (like the PC), then things can get interesting (go sideways) “unexpectedly”.

wvmarle:
Are you sure all grounds are properly connected?

Also that "flickering", how is that exactly? On/off or changes in brightness, or something different?

I thought I wanted to keep the zero cross detector isolated completely but when I connected its negative terminal to my ground line the flickering resolved. Does that mean it just didn't have a good zero basis? Should I expect any issues with this configuration?
And just in case anyone else comes across this issue, the flickering was more of a flashing I suppose.

runaway_pancake:
Are you distinguishing Line (Hot) vs Neutral properly in your circuit?

PE - If there's a Hot/Neutral swap and something in the mix is earthed (like the PC), then things can get interesting (go sideways) "unexpectedly".

Yes I double checked by testing to ground and they are labelled correctly. Thank you though.
Thank you all for your help btw.

The circuit is sound, the method is well known.

Your symptoms sound mostly like poor solder connections or otherwise improper electrical connections. You best check all your connections again.

wvmarle:
The circuit is sound, the method is well known.

Your symptoms sound mostly like poor solder connections or otherwise improper electrical connections. You best check all your connections again.

Ok, thank you. Everything is working then. I just have to get the heat down. I'm going to look into the AC opto couplers. Thanks for all your help!