WARNING: Some people try to build this with an optocoupler with zerocrossing coz 'that is better' right? Some are even told in electronics shops it is better to use such an optocoupler. WRONG. This will only work with a random fire optocoupler: NOT igniting at zerocrossing is the principle of this dimmer.

Switching an AC load with an Arduino is rather simpel: either a mechanical relay or a solid state relay with an optically isolated Triac. (I say Arduino, but if you use an 8051 or PIC16F877A microcontroller, there is stuff for you too here.)

It becomes a bit more tricky if one wants to dim a mains AC lamp with an arduino: just limiting the current through e.g. a transistor is not really possible due to the large power the transistor then will need to dissipate, resulting in much heat and it is also not efficient from an energy use point of view.

Phase cutting
One way of doing it is through phase control with a Triac: the Triac then is fully opened, but only during a part of the sinus AC wave. This is called leading edge cutting.
One could let an Arduino just open the Triac for a number of microseconds, but that has the problem that it is unpredictable during what part of the sinus wave the triac opens and therefore the dimming level is unpredictable. One needs a reference point in the sinus wave.
For that a zero crossing detector is necessary. This is a circuit that tells the Arduino (or another micro controller) when the sinus-wave goes through zero and therefore gives a defined point on that sinus wave.
Opening the Triac after a number of microseconds delay starting from the zero crossing therefore gives a predictable level of dimming.

Pulse Skip Modulation
Another way of doing this is by Pulse Skip Modulation. With PSM, one or more full cycles (sinuswaves) are transferred to the load and then one or more cycles are not. Though effective, it is not a good way to dim lights as there is a chance for flickering. Though it might be tempting, in PSM one should always allow a full sinuswave to be passed to the load, not a half sinus as in that case the load will be fed factually from DC which is not a good thing for most AC loads. The difference between leading edge cutting and PSM is mainly in the software: in both cases one will need a circuit that detects the zero crossing and that can control a triac.

A circuit that can do this is easy to build: The zero crossing is directly derived from the rectified mains AC lines – via an optocoupler of course- and gives a signal every time the wave goes through zero. Because the sine wave first goes through double phased rectification, the zero-crossing signal is given regardless whether the sinus wave goes up through zero or down through zero. This signal then can be used to trigger an interrupt in the Arduino.

PWM dimming
PWM dimming, as in LEDs is not done frequently with AC loads for a number of reasons. It is possible though. Check this instructable to see how.

It goes without saying that there needs to be a galvanic separation between the Arduino side of things and anything connected to the mains. For those who do not understand 'galvanic separation' it means 'no metal connections' thus ---> opto-couplers. BUT, if you do not understand 'galvanic separation', maybe you should not build this.

The circuit pictured here does just that. The mains 220Volt voltage is led through two 30k resistors to a bridge rectifier that gives a double phased rectified signal to a 4N25 opto-coupler. The LED in this opto-coupler thus goes low with a frequency of 100Hz and the signal on the collector is going high with a frequency of 100Hz, in line with the sinusoid wave on the mains net. The signal of the 4N25 is fed to an interrupt pin in the Arduino (or other microprocessor). The interrupt routine feeds a signal of a specific length to one of the I/O pins. The I/O pin signal goes back to our circuit and opens the LED and a MOC3021, that triggers the Opto-Thyristor briefly. The LED in series with the MOC3021 indicates if there is any current going through the MOC3021. Mind you though that in dimming operation that light will not be very visible because it is very short lasting. Should you chose to use the triac switch for continuous use, the LED will light up clearly.

Mind you that only regular incandescent lamps are truly suitable for dimming. It will work with a halogen lamp as well, but it will shorten the life span of the halogen lamp. It will not work with any cfl lamps, unless they are specifically stated to be suited for a dimmer. The same goes for LED lamps

If you are interested in an AC dimmer such as this but you do not want to try building it yourself, there is a somewhat similar dimmer available at www.inmojo.com, however, that is a 110 Volt 60Hz version (but adaptable for 220 50Hz), that has been out of stock for a while. You will also find a schedule here.

NOTE! It is possible that depending on the LED that is used, the steering signal just does not cut it and you may end up with a lamp that just flickers rather than being smoothly regulated. Replacing the LED with a wire bridge will cure that. The LED is not really necessary. increase the 220 ohm resistor to 470 then

STOP: This circuit is attached to a 110-220 Voltage. Do not build this if you are not confident about what you are doing. Unplug it before coming even close to the PCB. The cooling plate of the Triac is attached to the mains. Do not touch it while in operation. Put it in a proper enclosure/container.

WAIT: Let me just add a stronger warning here: This circuit is safe if it is built and implemented only by people who know what they are doing. If you have no clue or if you are doubting about what you do, chances are you are going to be DEAD!

4N25 €0.25 or H11AA1 or IL250, IL251, IL252, LTV814 (see text in the next step)
Resistor 10k €0.10
bridge rectifier 400 Volt €0.30
2x 30 k resistor 1/2 Watt (resistors will probably dissipate 400mW max each €0.30
1 connector €0.20
5.1 Volt zenerdiode (optional)

Lamp driver
LED (Note: you can replace the LED with a wire bridge as the LED may sometimes cause the lamp to flicker rather than to regulate smoothly)
MOC3021 If you chose another type, make sure it has NO zero-crossing detection, I can't stress this enough DO NOT use e.g. a MOC3042
Resistor 220 Ohm €0.10 (I actually used a 330 Ohm and that worked fine)
Resistor 470 Ohm-1k (I ended up using a 560 Ohm and that worked well)
TRIAC TIC206 €1.20 or BR136 €0.50
1 connector €0.20

Piece of PCB 6x3cm
electric wiring

That is about €3 in parts

Step 1: Arduino controlled light dimmer: The PCB

You will find two pictures for the PCB: my first one, that I leave here for documentation purposes and a slightly altered new one. The difference is that I left out the zenerdiode as it is not really necessary and I gave the LED itś own (1k) resistor: it is no longer in series with the Optocoupler, that now has a 470 Ohm resistor. I made the PCB via direct toner transfer and then etched it in a hydrochloric acid/Hydrogenperoxide bath. There are plenty of instructables telling how to do that. You can use the attached print design to do the same. Populating the print is quite straightforward. I used IC feet for the opto-couplers and the bridge rectifier.
Download the print here.
Note: You need Fritzing for this. For the direct toner transfer, the printed side of the printed pdf file, goes directly against the copper layer for transfer. Once it is transferred, you will be looking at the ink from the other side and thus see the text normal again. I made slight alterations in thePCB: I removed the zenerdiode and the LED is no longer in series with the optocoupler.

I used a TIC206. That can deliver 4 amperes. Keep in mind though that the copper tracks of the PCB will not be able to withstand 4 Amperes. For any serious load, solder a piece of copper installation wire on the tracks leading from the TRIAC to the connectors and on the track between the two connectors.

In case it is not clear what the inputs are: from top to bottom on the second picture:
Interrupt signal (going to D2 on arduino)
Triac signal (coming from D3 on Arduino)

If you have an H11AA1or IL 250, 251 or 252 opto-coupler then you do not need the bridge rectifier. These have two anti-parellel diodes and thus can handle AC. It is pin compatible with the 4N25, just pop it in and solder 2 wire-bridges between R5 and + and R7 and -. The LTV814 is not pincompatible

<p>hello grate work any idea how to control washing machine motor and how to use the tacho for preventing low speed on load thank you</p>
<p>TRIACs are not the most ideal component for inductive loads but they can do it. make sure that you have a TRIAC capable of the power needed and add a snubber network.<br>I am not sure what you mean with &quot;how to use the tacho for preventing low speed on load&quot;</p>
<p>I constructed the circuit, and the triac seems to be firing correctly. My light won't dim correctly thought. I put the basic dimming program you provided in step 4 on the arduino, but the light flickers rather than dimming. I'm guessing it is a timing issue. I am on a 120v 60Hz grid, and have changed the values in the program accordingly. Any suggestions?</p>
<p>yes it seems your circuit isnt doing at all what the program is supposed to do. Can you do the following for me:</p><p>keep your arduino connected but simply write a LOW to the output-what happens?</p><p>then simply write a HIGH to the output pin.-what happns then?</p><p>Then disconnect your Arduino totally, but leave your triac circuit connected to the grid-what happens then?</p>
<p>I wrote LOW to output, light stayed off.</p><p>I wrote HIGH to output, light stayed on, no flickering.</p><p>Disconnected arduino, light stayed off.</p><p>So it looks like the triac is working as expected. I wonder if it's an <br>issue with the timing on the interrupt. I have the 60Hz signal coming <br>from the mains passing through the 400v bridge rectifier, which produces<br> a 120Hz signal. This is passed to the 4n25, then to the interrupt pin. I<br> changed the values in the program from 75 to 65, and from 10 to 8.33 as<br> instructed. Oscilloscope showed a pretty clean 120Hz signal going to interrupt pin.</p>
<p>Well if you looked at it on a scope you should have been able to see the timing.</p><p>The reason for the test was to see if there might have been haphazard firing of the TRIAC, which is usually caused by a 'dirty' PSU<br>Nevertheless it proves teh TRIAC circuit is working well.<br>As you also have a clean 120 Hz signal going to the interrupt pin (did you indeed measure it on the collector of the 4n25??) I doubt the problem is there<br><br>Final question... did you also adapt the values of R4 and R5?<br>Did the scope show you the voltage of the 120 Hz signal on 4n25? and did it show you the width of the pulse?</p>
<p>I changed the 30k resistors to 15k, and have included a picture of the scope reading which shows some of the values you asked for. The width of the 120Hz signal is 8.310mS, Vpk-pk is 5.12v, Vrms is 1.47v. Still getting inconsistent dimming with basic dimming program.</p><p>One thing I tried was removing the for() statement in the void loop() of the basic dimming program, and after I did that it started dimming consistently. I will attach a video. With the value &quot;dimming&quot; not changing, I was expecting a fixed brightness level, but instead it started running through the dimming cycle.</p><p>Thanks for all your help!</p>
<p>That is odd to say the least. So you are left with<br>dimming=i;<br><br> delay(10);<br>and it is dimming?<br>Well, at least it works :-)</p>
<p>I removed the whole for() statement, so there is nothing in the void loop(), and it is dimming as before (see first screenshot).</p><p>I have tried manually setting dimming = 0 and delay(10), but I am still getting a quick fading cycle. I thought that setting dimming = 0 would leave the triac on for the whole wave cycle, giving me full brightness, but it is still going through a dimming cycle (see second screenshot).</p><p>Can you think of anything that would cause this?</p>
<p>Honestly... I have no idea. Your program is going through a loop that isnt programmed. If you disconnected the Arduino, there was no fading correct?</p>
<p>if you haven' t adapted the 30k resistors into 15 k you should do that. </p><p>If the value is too high (like 30k for 120 V is) then the zerocrossing pulse will be rather wide and start long before the real zerocrossing</p>
<p>great job, can i use in unstable frequency? i want to control my speed of syncronous generator. thx in advance.</p>
I think i can paralel 0-x ing output to INT1 and use pulseIn function to measure frequency, agree? Thx for your atenttion
<p>well i wouldnt directly parallel the two ports, but each give them a seperate signal by means of an extra optocoupler, you could also use 1 pin to measure frequency and keep track of the phase cutting timing, but software wise it might be easier to use two pins, that depends.</p><p>You can use PulseIn, thats is a convenient way but i am not sure if it is the best way as PulsIn will let the program wait, but maybe that is no problem for you</p><p>The best way might be to just use one pin for the zero interrupt, but to use a timer to measure the time between interrupts</p>
<p>my one last question, its possiblle to control 3 phase mode with 1 controller? (i use AVR atmega 32A). If yes tell me how to do it. Thx before.</p>
<p>I presume it is but I am not experienced enough with 3 phase power to give any advice on it</p>
<p>Not entirely sure what you mean with unstable frequencies. You mean on the grid?<br>Syncronous generators in my knowledge are used to generate a voltage from a movement, so I am not sure what speed you want to control. Or do you mean a syncronous motor?<br>TRIACs are not ideal to regulate inductive loads but they can. Just make sure you have a snubber network</p>
<p>yes syncronous generator as you mean, i use water turbine as prime mover. , as we know that consumer load is always changing and i want to use your project to control total generator loads in a way compensate it to dummy loads (incandescent bulb) so that frequency always stable at arround 50 hz. If frequencies more than 50.5 hz increase (dimm up) the bulb, if freq. Less than 49.5 Hz dim down bulb, else do nothing (last state dimm). Thx for your answer</p>
<p>obviously you need a bit more than just this circuit.</p><p>as i understand you want to use this to switch in aload, as ipresume to get the frequency down.</p><p>The problem however is that the period changes and therefore the timing for the triac is difficult to establish.</p><p>what u need to do is to measure the frequency andindeed based on that calculate a new dimfactor. I do not know enough about asyncronous generator to know if and to what extend a load will bring down the frequency but if yes, then yes this circuit can do it, but it might be easier to use my other circuit for that </p><p><a href="http://www.instructables.com/id/AC-PWM-Dimmer-for-Arduino/">http://www.instructables.com/id/AC-PWM-Dimmer-for-...</a></p><p>but u would still need to add some hardware to measure the frequency</p>
<p>i was read your project pwm ac dimmer, but IGBT that can handle about 8 kw load so very expensive, can i parallel some mosfet with 1 gate driver?</p>
<p>It is mi first Instructable proyect that I made!!! I am very happy!</p><p>I should changed some resistors because the 4n25 does not fire. Instead of using two resistors of 30Kohms, I used two of 15Kohms. I suspect that the bridge rectifier has its own resistance.</p><p>Thanks for all the info.</p><p>Bye!</p><p>PS: My home AC line is 220Volts 60Hertz</p><p>Este es mi primer proyecto que hago de Instructable!!! Estoy muy contento!</p><p>Tuve que hacer unos cambios en las resistencias porque no se disparaba el 4n25. En lugar de usar dos resistencias de 30KOmhs, us&eacute; dos de 15KOmhs. Sospecho que el puente rectificador que us&eacute; tiene resistencia.</p><p>Gracias por toda la informaci&oacute;n.</p><p>Saludos!</p><p>PD: En mi casa la electricidad es de 220Volts a 60 Hertz</p>
<p>Congrats and I am happy you were succesful in building it.<br>The bridge does not have its own resistance. It doesnt work that way. What does happen though with 2x15k, rather than 2x30k is that the zerocrossingssignal becomes narrower. It could have been that you needed that, but the 4n25 should have fired with the particular values.<br>Video looks great !</p>
Could be that. Thatnks for your fast response.
<p>my pleasure. Make sure your 15k resistors can take the heat. should be minimally 800mW dissipation per resistor, so take 2 Watt resistors</p>
<p>I sure will change the resistor to 2watts. But I do not understand why need it.</p><p>If it is only a led pulling some little amps. Obviously I know very very little of electronics.</p><p>Thanks in advance.</p>
<p>Are we talking about the same circuit?<br>What LED are you talking about? The one in the 4n25?<br>that doesnt draw some little amps, it draws miliamps.<br>But the explanation is very simple. The average voltage when on 220V AC after rectifying is about 207 volt.<br>Your 4n25 only needs a few miliamps but it can take 60. The voltage drop over the 4n25 is 5 volt but for sake of easiness just lets forget about that.<br>You have 2x 15 k in series so that makes 30k.<br>The current through that then will be 7 mA<br>the power is U&sup2;/R=1600 mW is 1.6 Watt. Per resistor that is 800mW but as you dont want to push a resistor to its max a 1.5 Watt resistor would be fine</p>
<p>Yes, I am taking about the led inside de 4n25.</p><p>Great explanation!</p><p>Thanks again</p>
<p>My pleasure</p>
<p>can i use dimmable LED bulb 9 Watt?</p>
<p>together with the question 'can I use it to regulate a ceiling fan' this must be the most asked question :-)<br>Well yes and no. It all depends on the LED. There are dimmable LEDs that are made to work with classic (TRIAC) dimmers and there are those that work in a different way (usually by feeding a 1-10 Volt signal). It is impossible to say which one your LED is, but if you have a type number I am sure it can be found on the internet </p>
<p>And the above circuit uses the &quot;classic triac dimmer&quot; which means it will work with dimmable LED bulbs, correct?</p><p>Thanks</p>
<p>if you have a dimmable LED that is meant to work with the classic TRIAC dimmer, it will work with this circuit indeed as apart from the microcontroller it is a 'classic' dimmer</p>
<p>Great thanks</p>
<p>Can i use BTA12-600B instead of TIC206 </p><p>and use S1NB (1000V Bridge Rectifier) for this<br></p>
<p>yes, you can use those</p>
<p>it's nothing happen.<br>do i have to use zener diode and adjust any resistor?</p>
<p>no the zener is not necessary.</p><p>Do the following:</p><p>check ALL yr connections.</p><p>Remove Arduini</p><p>plug yr circuit into the main.</p><p>put a 5 volt signal on the 220ohm input resistor.</p><p>Is the lamp on?</p><p>is the LED on?</p><p>What color is yr LED?</p>
<p>Lamp still off<br>but LED is on <br>it's yellow light</p>
<p>ok, disconnect from the mains, remove the 4n35 optocoupler from its socket and connect pin 4 and 6of the socket with a piece of wire.</p><p>connect the circuit with the mains again and see if the lamp switches on.</p><p>how big is yr gate resistor?</p>
I follow your guidance but lamp is still off.<br><br>Which resistor is the gate?<br>I use 220 ohm connect with D3 same to your print.<br>It is gate resistor,right?<br>
<p>no the gate resistor is the one connected to the TRIAC, the one that is 1K in my circuit.<br>I am just wondering. If you seem oblivious about 'gate' are you sure you connected the TRIAC right?<br>If you did connect pin 4 and 6 of the IC foot after removing the 4n35 and the lam is still off, it suggests that the gate isnt triggered at all, so either yr gate resistor too large, or the TRIAc dead, or wrong connection or (I am asking) forgot to pluf into th emains.<br>are u sure you know which pins 4 and 6 were (sorry, just want to make sure)</p>
<p>now, i use 330 ohm to be resistor gate then the lamp is on.<br>when i use d3 to be triac signal, it's nothing happen though pin4 and pin6 are connected with wire. i'm sure about pin4 and 6 were.(i absolutely check many times)<br><br>how big is resistor at pin4 of 4n25?</p>
<p>OK so when you short pin 4 and 6 and use a 330 ohm gate resistor the lamp is on. Great.<br>I have no idea what you mean with d3 to be the triac signal, but I suggest you use the 330 ohm gate resistor, insert your 4n35 again and see if it works<br>the value of the resistor at 4n25 should be 10 k as is mentioned in the circuit</p>
<p>Finally the lamp switch ON !!<br>Problem is my 4n25 is broken.I change it and circuit WORK !!<br>Thank you very much for your help.<br>Thank you again.</p>
<p>ah i see. I am happy you found it :-)</p>
<p>ah now i see what you mean with d3 triac signal nothing happens. Ofcourse nothing happens as you took out the 4n35 when you connected pin 4 and 6. I asked you to connect pin 4 and 6 in order to test, not in order to make it work that way. So read my earlier response: remove the shortcut between pin4 and 6 use the 330 ohm gate resistor insert your 4n35 and try again</p>
<p>Hi, amazing work !</p><p> I was success to do the project as using the code in step 4. I wish I could use a smartphone to control the brightness with Bluetooth via serial with the code in step 5. Is it ok ?</p><p> And I have see some discussion about the code in step 5 with <a href="http://www.instructables.com/member/DovydasJ" rel="nofollow">DovydasJ</a>, is the code is wrong and needed to be modified ? Any suggestion will be appreciated, thank you.</p>
<p>pls read my prior reply as well.</p><p>you still could use that code with yr smartphone if u add a command to increase or decrease the dim value. all yr phone then needs is an up down arrow to increase or decrease the brightness</p>
<p>Ok, I am able to dim the light in 128 steps with Bluetooth connection. Yes, I modify it to let the Arduino to catch the 3 characters. However, it sometimes just go off after i send some number. I think it is the timing problem of interrupt and serial. If i have time, maybe i will try to solve it too. </p><p>By the way, thank you for your suggestions.</p>
<p>great. tnx for yr feedback</p>

About This Instructable


363 favorites


Bio: I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit slower and do things I ... More »
More by diy_bloke: AC PWM dimmer for Arduino The Arduino and daylight saving time (Europe) Using Reset pin / pin 1 on Attiny without bothering with Fuses
Add instructable to: