Triac/Optocoupler Dimmer

Hello! The Arduino community is a wealth of information, and I greatly appreciate any help that is offered.

In the long run, I'm trying to make five separate sets of LED strings fade and blink simultaneously. I plan to do this with one Arduino as a sensor driven trigger (I've got that working), wirelessly transmitting to the others (I have that working, too), but I never expected that the hardest part was going to be making the dimmers!

The problem is that I am trying to use off-the-shelf Christmas LEDs (for price and the fact that it's all pre-wired, since the final display will have about 2,000 LEDs), which means I need 120v power to run them. I believe that means I can't use my Mosfet Shield Power Driver Shield Kit - DEV-10305 - SparkFun Electronics, because it only handles up to 60v. The Mosfet shield would be ideal, since it runs off of PWM which I understand fairly well, is cheap, has six channels (I'd use 4), and already has a board.

But, since it doesn't handle the voltage I need, I am looking into TRIACS. I have assembled a prototype board based on this diagram:


-which I found in this forum topic http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1276992650/0.

I am having several problems with that circuit, as well as the code.

1)I keep burning up resistor 3, which in the diagram is a 56ohm resistor. I also tried both of the resistor values from similar circuits on the MOC3041M datasheet Fairchild Semiconductor 048xrw12q8wiqfuc09tqwfy5dgwy datasheet pdf. What happens is that the 4.8watt LED string I'm using for testing seems to work correctly, switching on and off, and even flickering like crazy when I send a PWM signal through the optoisolator, but if I plug in a 60w incandescent bulb everything is just fine (the circuit doesn't immediately start to fail), but as soon as I send a digital HIGH signal to the optoisolator, R3- no matter what value I've used, up to 1/2watt, starts on fire and will not stop until the 120v A/C is disconnected.

There's a picture of my breadboard circuit attached (you'll note that I had accidentally put a 330k resistor where a 330 should have been, which was later corrected but with no additional success).

I am also concerned that the optoisolator I have is for 240v and not 120v, but I had assumed (again) that it could run at either voltage, whereas the MOC3031M would not handle the higher voltage, since this was the specific isolator called for from the circuit diagram I am using.

For the record, I am using a BT138 TRIAC http://www.ee.latrobe.edu.au/internal/workshop/store/pdf/BT138.pdf.

2)I can not for the life of me figure out how I'm supposed to run this thing. I had foolishly thought that it would work with a PWM signal, but it seems I'm very wrong. From what I've read in the forums, it seems that I'll need to use the zero crossing circuit in my optoisolator to time the dimming through the TRIAC, and that the way to do that is through an interrupt on Timer1, but that's all new to me, and the code I'm looking at is either too complex for me to modify for my purposes, or not compatible with my hardware/circuit.

This is the code I was most interested in, from Ryan McLaughlin on this forum topic: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236998971/75. I tried to post it here, but I ran out of characters :~

For what it's worth, finding help might be a little easier if all of the relevant forums weren't locked, and I could just ask for help from the people who have already done this, but I don't think that's possible because of the forum migration that recently took place.

3)When the capacitor is connected, I get enough current to dimly light up my LED string before my Arduino was turned on, so I took it out and there was no change either way (as far as melting things or operation), other than that trickle of power when I wasn't sending a signal. Maybe this be irrelevant when I find what I'm doing wrong elsewhere, but I've included it in case it helps someone diagnose my error(s).

Thank you to anyone who can help! I am now in absolute crunch time for this project (it'll be canceled and done another way if I can't show proof of concept today, it's a project for a play at a local theater).

Also, for the record, I have handled mains power safely before in a professional environment and I am aware of the dangers to myself and my equipment.

Many thanks,
-Jason

keep burning up resistor 3, which in the diagram is a 56ohm resistor.

Well a quick sum shows that the resistor is burning 8.8W worst case so it is no wonder a quarter watt resistor sizzled.
You are dealing with 120V mains so that gives a peak voltage of 170v. The total resistance across this is 56 + 330 = 385 R, that with 170v across it gives about 0.4A so you have a peak of 53W on the other resistor.
Of course you don't get this continuously because the triac should fire and reduce the voltage to zero but is is an idea of the size of things.
The problem is that the circuit you used is not very suitable for dimming, it is more use as a simple switch. Yes you can make it dim if you synchronise the firing of the triac to the mains but it is a lot of trouble. There are SSRs that give you simple dimming options with PWM. These work by the PWM changing the brightness of an LED (in an opto isolator) and that being used to reduce the time constant of an RC circuit in the gate of the triac fed through a diac (DIAC - Wikipedia ). This gives you simple phase modulated switching which is what you need for dimming. This is a simple example of a circuit using a pot to control the dimming:-

This is the sort of waveform you are trying to get:-

I am also concerned that the optoisolator I have is for 240v and not 120v, but I had assumed (again) that it could run at either voltage,

That is no problem you can run it at any voltage.

Thanks for the advice!

I've heard of DIACs before, and thought they might be useful in this kind of circuit, but no one else on the locked forums I read seems to be having the problems I am having, so I have to believe their circuits are working. This particular circuit was said to be developed for 3 to 10 amps (with cooling for the TRIAC), and made no mention of needing a massive wattage resistor. I've attached pictures of the boards from the original developer (I PMed him recently, but no response yet), and they do not seem to show high wattage resistors, though I may be wrong.

Still, I'd love to try out your circuit, and I'm looking into where I could find a DIAC locally.

I don't think your example circuit is useful to me, though (or I lack the knowledge to use it), because the dimming is based on the variable resistance of the potentiometer, and I can't recreate variable resistance with an Arduino (to have it run by a program and not a human interface). However, I think having the SSR you mentioned that dims through PWM input into an optoisolator might be exactly what I'm looking for, but I don't know how to find one. Do you have a part number you can suggest?

Thanks again!

-Jason

The basic triac switcher won't work with a PWM signal. The zero cross detector in the MOC3041 won't allow firing at arbitrary phase angles. An MOC3011 would be needed along with a separate zero cross detector and voltage controlled timer connected to the firing circuit.

Also, I believe the 56 ohm resistor is burning up because the load is too close to the latching current of the SCR. The SCR can't latch itself on until the voltage is above 50V or so. In the meantime, the 56 ohm resistor and optocoupler are doing all of the conducting. The 330 ohm resistor doesn't fry because it's in parallel with the gate-MT1 resistance which is generally quite low.

How about this DC Controlled Light Dimmer Electronic Kit K8064?
http://www.apogeekits.com/light_dimmer.htm

I've seen this kit before, and I have two problems with it. First, it's pricey. Sure, one board is tolerable at $33, but I could buy a mosfet or a triac for about a dollar, and the rest of the parts for five more. There's a little room for adjustment in the plan, but I'm looking for 4-6 stations connected wirelessly with four channels each, two of which need dimming.

Second, I'm not sure the Arduino could run that. It takes a variable input voltage of 0-10vdc to operate the dimmer. I know that the Arduino can operate on up to 20vdc (12vdc recommended), and that you can get up to your input voltage (minus something like .7vdc I'm told) back out of the Vin pin (with some limitations), but how could the Arduino provide the adjustable voltage required to run the kit? Or am I mistaken and PWM would operate the kit?

I didn't really expect the triac circuit to "work" with PWM (I expected it to do something, I just didn't know what), so I tried it because I have no experience with interrupts. That's why I was asking for help with the coding.

As for the frying resistor, I guess I'm just lost. I simply have to believe that the boards in the picture I attached earlier work, I just don't know why. I believe I have replicated their circuit, but with the old forum locked, I can't really ask the tons of people who had success. I did begrudgingly PM the guy who made the boards, but I'm not surprised that I didn't hear back from him.

So, without someone explaining how to fix my hardware problem, or how to code the interrupts, I'm at a loss.

While I'm dead in the water with triacs, I'm still trying other things. Remember that mosfet board Power Driver Shield Kit - DEV-10305 - SparkFun Electronics? Well, I stated that it wouldn't work because it can only handle up to 60v, as per the mosfet datasheet: http://www.sparkfun.com/datasheets/Components/General/RFP30N06LE.pdf. So my next thought it "hey, could I just buy mosfets that run on 120v instead?" I'd have to rectify the 120vac before it gets to the board, but other than that, would it work?

I may need a mosfet rated higher than 120v, since I'm told that 120v power can actually be as high as 170v, but if that's the case, as long as I keep the load under 8amps (which is what I believe the line traces are rated for), I think I should be able to run my led string(s) through PWM.

I've made a post on the sparkfun forum about that as well, to see what they think: Power Driver Shield Kit 120v Upgrade? - SparkFun Electronics Forum

Thanks again for your help!

I know that the Arduino can operate on up to 20vdc (12vdc recommended), and that you can get up to your input voltage (minus something like .7vdc I'm told)

You are getting very mixed up. Yes you can put 20V into an arduino but this is regulated down to 5V, the chips can only run at 5V so that is the signal you get out. To get a bigger signal you have to pass it through a transistor connected to an external voltage of the size you want your PWM to be.

I didn't really expect the triac circuit to "work" with PWM (I expected it to do something, I just didn't know what),

So now you know what it does, it flashes a bit and burns out resistors, it is a perfectly fine circuit for using just to turn things on and off, if you get the component ratings correct. I would imagine a 3W resistor would be fine for low frequency flashing.

Trying to drive that circuit to make it dim while you can do it is not very practical because you won't have much time to do anything else. Yes buying a kit is more expensive but at your current state of knowledge I think it is your only option. Would you trust code written by someone else to control mains, I know I wouldn't.

Well, I've been told that you could draw power out of the Vin pin, but my point was that I completely agree with you that I can really only get 5v out of the board, and therefore wouldn't be able to run the kit you suggested.

The PWM didn't burn out the resistor on my triac circuit, which I know because after I burned the first one on my 60w bulb, I plugged the signal pin directly into the 5v pin on the Arduino, which should have told the optoisolator to turn on and stay there. The new resistor still melted. Also, the resistor never melted on my 4.8w light string, even with PWM. it just flickered a lot (many flickers when it was supposed to be dim, no flickers when it was supposed to be at full).

Again, I agree that i shouldn't be using PWM on the optoisolator/triac which is why I'd appreciate it if someone could point me in the direction of some good information about handling zero-cross detection/interrupts. And yes, I would trust someone else's tested code over code written at my level of knowledge.

Is the 56? resistor connected to MT2 or MT1? If you have the main terminals backwards that's another situation where it could end up doing all of the conducting. After doing a little more math, the resistor should not have fried with a 4.8W load and a 60W bulb should be far enough above the latching current. In fact, I wouldn't be surprised if the optocoupler can handle a 4.8W (40mA) load all by itself.

I'll have to hunt around a bit. I can't find that "perfect", easy to follow and well explained example of a dimmer circuit with a zero cross detector triggering a 555 timer driving the optocoupler. I'll bet it is still in the browser history at work though. Take that circuit, connect the zero cross detector to an Arduino input, generate the desired delay in software, and connect the Arduino output to the optocoupler.

The one I wanted to post is simpler but there is a good circuit in this app note:

Thanks everyone for the help, advice, and links. I ordered a bunch of parts (optoisolators with and without zero crossing, mosfets, diodes, triacs, and various other recommended items), and I'll see what I come up with.

Also, Cachehiker, I don't think I had the triac terminals backwards, but it is definitely worth testing. I ordered a second triac with the same model number, and when I get it I'll try the terminals both ways (since I don't want to try that before I have a replacement in hand, or else I'll be dead in the water if the triac is damaged).

I'll report back any findings here, hopefully within a week.

Thanks again to everyone who offers their advice and expertise in the forums!

I just wouldn't expect a 56? resistor to go like that in the absence of extreme power line noise, highly reactive loads, or some kind of misconnection. I have no problems with 100? and some of the triac drivers in my projects have been driving inductive loads for over 10 years. The math says use line peak divided by the maximum optocoupler output amps, about 170/2. Even if 56? is too low, resistors will often take a 50% overload for months before failing.

It's easy to check with a DMM. Gate-MT1 typically has low resistance. Gate-MT2 does not. IIRC, getting them backwards means you're firing in the wrong quadrants or possibly not at all. In reality, triacs aren't near as bidirectional as they appear.

Status update: it works!

Well, not the triac circuit, I've done it much more simply with a mosfet and a rectifier to avoid zero-cross detection. I also ran my own numbers and figured out why my resistors were frying (after some of you pointed out that I should do my own math), and I just don't know how those other guys got that circuit to work. I'll get back to triacs eventually, but this way seem infinitely simpler because it avoids the zero-cross interrupt coding. I have also used this circuit (with higher amp parts and the addition of a schottky diode- but the explanation below is for my 4amp dimmer circuit) to control the speed of a 12vdc drill.

What I've done is to take wall power and run it through a 4amp rectifier bridge, then run that power through my 9am 200v mosfet with an outlet in line with the positive wire for me to plug my light string (or anything) into.

To control the gate, I put a 5v voltage regulator (and capacitors) on a 9v transistor battery, with positive running through the detector on an optoisolator and then to the gate, and ground tying into my 110v DC ground. There is also a 100k 1/4watt pull down resistor from the gate to ground.

The emitter of my optoisolator is running the "fade" sketch off of a battery powered Arduino Uno.

I am very pleased with the results, and I have effectively faded LED strings and a 60watt incandescent bulb without any noticeable problems or heat generation. However I still have some questions.

1)I read some things I don't understand about how my 100k pulldown resistor may be effecting the PWM signal due to power dissipation or recovery time or something. Is there a better resistance or wattage for the circuit, and why?

2)By using separate batteries for my voltage regulator and Arduino, I believe I am getting the best isolation. However, this is not the most practical. In the future, I intend to use a transformer to step down a separate branch of AC to 12vac, then rectify it to 12vdc, so that my voltage regulator can take it to 5vdc, which is within the specs of my mosfet gate. This will all be on the detector side of the optoisolator, so it shouldn't affect the isolation between the 110v and the Arduino.

I would also like to run the Arduino off of the 110vac, but I want to know how that will effect my isolation.

If I plug a standard wall wart into the same 110vac source to power my Arduino, have I lost some or all of my isolation by sharing a common ground?

If not, could I instead just branch off some of that 12vdc from the transformer/rectifier setup before it goes to the 5v regulator and power my Arduino on that (given that I believe I have accounted for amp draw), or will that negate the purpose of the optoisolator?

The way I see it, the only way to get the Arduino completely isolated from that circuit is to have it on batteries, which I don't want to do (it'll be in a theatrical play for 6 weeks, and that's a lot of batteries). But is sharing a common ground acceptable?

Thanks everyone for your help! I could not have gotten this far without the input you guys have given me.

-Jason

PS- I plan to do some pictures and a schematic when I have this completely solved and functional when I have some time, so other people can see what I've done, so any ideas to make this as safe as possible are welcome. I have a liquid, brush on insulation material that I plan to use on my 110v parts, save the mosfet's heat sink (which will be physically isolated from being touched).

jasmattkrz:
Status update: it works!

Well, not the triac circuit, I've done it much more simply with a mosfet and a rectifier to avoid zero-cross detection. I also ran my own numbers and figured out why my resistors were frying (after some of you pointed out that I should do my own math), and I just don't know how those other guys got that circuit to work. I'll get back to triacs eventually, but this way seem infinitely simpler because it avoids the zero-cross interrupt coding. I have also used this circuit (with higher amp parts and the addition of a schottky diode- but the explanation below is for my 4amp dimmer circuit) to control the speed of a 12vdc drill.

What I've done is to take wall power and run it through a 4amp rectifier bridge, then run that power through my 9am 200v mosfet with an outlet in line with the positive wire for me to plug my light string (or anything) into.

To control the gate, I put a 5v voltage regulator (and capacitors) on a 9v transistor battery, with positive running through the detector on an optoisolator and then to the gate, and ground tying into my 110v DC ground. There is also a 100k 1/4watt pull down resistor from the gate to ground.

The emitter of my optoisolator is running the "fade" sketch off of a battery powered Arduino Uno.

I am very pleased with the results, and I have effectively faded LED strings and a 60watt incandescent bulb without any noticeable problems or heat generation. However I still have some questions.

1)I read some things I don't understand about how my 100k pulldown resistor may be effecting the PWM signal due to power dissipation or recovery time or something. Is there a better resistance or wattage for the circuit, and why?

2)By using separate batteries for my voltage regulator and Arduino, I believe I am getting the best isolation. However, this is not the most practical. In the future, I intend to use a transformer to step down a separate branch of AC to 12vac, then rectify it to 12vdc, so that my voltage regulator can take it to 5vdc, which is within the specs of my mosfet gate. This will all be on the detector side of the optoisolator, so it shouldn't affect the isolation between the 110v and the Arduino.

I would also like to run the Arduino off of the 110vac, but I want to know how that will effect my isolation.

If I plug a standard wall wart into the same 110vac source to power my Arduino, have I lost some or all of my isolation by sharing a common ground?

If not, could I instead just branch off some of that 12vdc from the transformer/rectifier setup before it goes to the 5v regulator and power my Arduino on that (given that I believe I have accounted for amp draw), or will that negate the purpose of the optoisolator?

The way I see it, the only way to get the Arduino completely isolated from that circuit is to have it on batteries, which I don't want to do (it'll be in a theatrical play for 6 weeks, and that's a lot of batteries). But is sharing a common ground acceptable?

Thanks everyone for your help! I could not have gotten this far without the input you guys have given me.

-Jason

PS- I plan to do some pictures and a schematic when I have this completely solved and functional when I have some time, so other people can see what I've done, so any ideas to make this as safe as possible are welcome. I have a liquid, brush on insulation material that I plan to use on my 110v parts, save the mosfet's heat sink (which will be physically isolated from being touched).

Could you please post your code and circuit?

I'm trying to make a dimmer using the ray circuit and code from ryan (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230333861/15), but didn't work.

I'll post my code, maybe someone could help me: (is the same of ryan)
#include <TimerOne.h>

volatile int i=0;
volatile boolean zero_cross=0;
int AC_pin = 10;
int Dimmer_pin = 0;
int LED = 3;
int dim = 0;
int freqStep = 65;
void setup() {
pinMode(AC_pin, OUTPUT);
pinMode(LED, OUTPUT);
attachInterrupt(0, zero_cross_detect, FALLING);
Timer1.initialize(freqStep);
Timer1.attachInterrupt(dim_check, freqStep);

}

void zero_cross_detect() {
zero_cross = 1;
}
void dim_check() {
if(zero_cross == 1) {
if(i>=dim) {
digitalWrite(AC_pin, HIGH);
delayMicroseconds(5);
digitalWrite(AC_pin, LOW);
i = 0;
zero_cross = 0;
} else {
i++;
}
}
}

void loop() {
dim = analogRead(Dimmer_pin) / 8;
analogWrite(LED, dim);
}

I still haven't solved zero-cross or interrupts, they are both complicated hardware and software issues that I don't have the expertise or training to handle.

However, if you just want to dim a standard light (LED strings or incandescent lights) off of AC power, just rectify the AC to DC and then control that voltage with PWM (native to the Arduino) at the gate of a MOSFET of appropriate voltage and amperage for the project.

You can then use even the simplest code to control the light (including the "blink" and "fade" example sketches).

Add a schottky diode to the circuit in the right place and you can control DC motors, too!

Using an optoisolator is recommended to keep the 120v isolated from the Arduino.

If you want help with that solution, let me know. If you want to learn about someone else's circuit (including interrupts and zero cross detection), I can't help you. I know this works and it's amazingly simple.

Using DC on incandescent bulbs does theoretically reduce the lifespan of the bulb, but not drastically (or for me, even noticeably!).

Good luck!

Just in case someone ends up here looking for answers: have a look here: http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/
It has a circuit with zerocross detection and software based on interrupt

Just an additional note on this project which is not clearly stated in the previous post.

When we speak about dimming, we do NOT want the TRIAC to switch on at zero crossing.
The TRIAC already switches OFF at zero crossing, so no dimming would be possible.
The maximum we could get is a flicker if it doesn't switch on on every halfwave.

What we want is to sense the zero crossing and switch on the TRIAC at a variable delay compared to it.

Hope this helps.

I want to control speed of Fans i.e inductive load through Arduino. Can anyone please share the schematic how I can do it?