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.

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>sir can i use 250v and 1a bridge rectifier? thanks..</p>
<p>yes u can use a 300 watt lamp</p><p>you can also use 250 volt</p><p>the circuit currently has a bridge rectifier so i am not sure what u mean</p>
<p>I'm trying to dim a 300 watts halogen lamp. is it ok?</p>
<p>I have few stupid questions (I know it's basics but I can't find answers and I have not anyone who can help me with this) ...How does &quot;zero<br>crossing detector&quot; works? What happend if the Voltage is 0? What<br>happend if Voltage is more than 0? Where current flows in this two<br>cases (left side of 4N25) and what Voltage value is there?</p><p>I understand this in this way: when Voltage is 0<br>the current flows the green way and when it has some Voltage &ndash; red<br>way. But in that case why 10k resistor? Voltage will not be very low...? </p>
<p>if the voltage is zero, that is when the zero is detected. The optocoupler blocks and no or only a small current will flow through the transistor: Therefore the voltage on the collector will rise to close to the Vcc<br>If the AC voltage is not 0, the transistor in the optocoupler will be triggered and go into conduction therefore the collector will be pulled close to 0</p><p>With regard to yr question about the value of the 10k resistor and 'will the voltage not be very low' Well.. which voltage, the voltage when there is no conduction or the voltage when there is conduction? Maybe that already answers yr question: The value is somehat irrelevant. The voltage only needs to be high or low. <br>Theoretically that resistor could even be 100 MegaOhm because if there is no current flowing, the Voltage on the collector will still be 5Volt.<br>The reason why i dont make it 100MOhm is because in practice a small current will flow into the input pin, but even then 100MOhm might still give a HIGH when needed.<br></p>
<p>Thanks, that helped me, but now I have new questions :) Why there is 30K resistors? This is because the bridge or 4N25 can't stand 220V? And here my second question: what does '400V' under bridge mean? Is this max voltage of it? And the last one: why 1k resistor is conected to MOC3021? I tried understand this by reading datasheets, but I'm not so good at it...</p>
<p>It is the 4n25 whose current needs to be limited, hence the 30k resistors<br>400 Volt is the max rating of the bridge<br>The 1k is there to limit the Gate current</p>
<p>I need your help again. I build the circuit, I upload photos and schema. I use BT136 triac and MOC3021. Now the problem: the bulb is on only a short part of time when I plug or unplug: https://youtu.be/_EshGUidjB0 at 17s you can see that nothing happend, bulb is off. I connect wire direcly to triac and then bulb is on so triac is working fine. I change MOC3021 to another MOC3021 and that's change nothing. Zero crossing detection is working fine. What can be wrong here? I can show you my code, but i use STM32F0DISCOVERY instead of Arduino</p>
<p>well if yr triac circuit works and yr zerocrossing also works, the problem must be in your software. And as I dont know your code, there isnt much i can do other than to advise you to really check your code</p>
<p>I bought another MOC3021, at the begining everything was good light is on but then MOC3021 become very hot ... what can be a reason?</p>
<p>Then I start to think you may have made a faulty wiring in your circuit</p>
<p>I have 230v in my country as you can see on the picture, is it ok for this circuit? I try to find the reason why this is not working because it seems to that i made everything right...</p>
<p>yes, i have 230 as well.</p><p>but let me summarize yr findings:</p><p>you have a working zero cross signal</p><p>you have a triac that triggers if you put a signal on it</p><p>that shows functionality, suggesting the problem is in yr software</p><p>you have a moc3o21 that gets hot, that suggests a fault most likely around the secundary connections.</p><p>The circuit when build properly should work uncomplicated.</p><p>So i suggest:you recheck your connections, especially around the triac and secundary side of the MOC</p><p>if you are sure it is ok, remove your microcontroller and put 5volt on the entrace of the circuit. does your lamp burn then? </p><p>the </p>
<p>I check software and it is working fine... This is what I did after buing new MOC3021:</p><p>1. Change old MOC to new</p><p>2. Remove my microcontroller and put 5volt on the entrace of the circuit</p><p>3. Plug for ~20s</p><p>4. Bulb is on, everything is all right</p><p>5. Unplug and plug again</p><p>6. Bulb is flashes so i unplug fast</p><p>7. I touched MOC and it was hot</p><p>2 questions:</p><p>* In MOC: does it matter if I change wire conected do &quot;4&quot; with wire conected to &quot;6&quot;?</p><p>* Same questions about triac and T1 and T2</p>
<p>the reply is in fact very simple: if your software is OK, it must be your hardware.<br>If you checked and rechecked all your connections and resistor values and they are all good, it must be one of your components. It cant be the MOC as you have replaced that already. That leaves the TRIAC, again if you are sure you connected it all correct.<br>Nevertheless, my money is on a wrong connection.<br>With regard to the TRIAC and the MOC although there seem to be various ideas about that I do make a difference between T2 and T1 and usually it is pin 4 of the MOC that I connect to the gate.<br>My advice.<br> Take your entire breadboard set up apart. Forget about the zerocrossing part for now and rebuild it. From personal experience I know that one tends to overlook wiring mistakes in ones own wiring</p>
<p>I find it ... stupid mistake :) I connect both ends of a resistor to the same path(like on the picture). Funny, i check this couple of times and I didn't notice that :) Thanks a lot for your help. I will add some photos when I finish my project :) </p>
<p>:-) we have all been there Tomasz.<br>Exactly as I said: we tend to overlook the wiring mistakes we made. I am happy it is now working for you. I always appreciate pictures</p>
<p>I also try a differnet kind of bulb and another plug</p>
<p>Could you explain why using a optocoupler with zero crossing detection won't work (e.g having a MOC3042 instead of a MOC3021)? And what the zero crossing detection actually does in a optocoupler like MOC3042? </p>
<p>A MOC3041 or any other zerocrosing optocoupler when receiving an ON signal on the LED, switches at the moment there is a zerocrossing detected. But that is not what you want. You want to switch on the lamp at a moment of your choice and with that moment I mean a moment on the AC sinus curve. That is the entire principle of this circuit. So.. you detect a zeocrossing, wait a desired period and then switch on the lamp. The longer you wait, the less bright the lamp will be.<br>The Moc3041 doesnt give you that choice: it switches on the zerocrossing</p>
<p>I am still confused with the concept of zero crossing detector that I will apply to light 220v regulator circuit pakek moc3041, triac, together arduino. if for example of the pin out of my arduino give high voltage (5VDC) to the input pin moc3041 then the lights will be bright .. I ask if I give the voltage on pin out arduino 2,5vdc moc3041 input into whether the lights can be moderate? I've tried wearing moc3021 but to no avail only be on / off only .. please help .. I'm sorry if my language is not good ..</p>
<p>ef.ee there are a few things wrong with your thinking.<br>1. you cannot put a voltage of 2.5 volt on a pin. Yes you can put a pwm signal on a pin that may give an average of 2.5 Volt, but the optocoupler will still see that as a number of 5 and 0 volt signals.</p><p>2. Even if you could put a 2.5 volt signal on a pin, the optocoupler will either conduct or not<br><br>3. If you were to use a PWM signal and thus switch the triac on and off very fast, that has no relation to the amount of light coming from the lamp as you do not know when the lamp will be switched in relation to its phase.</p><p>As you are using an AC voltage the voltage is zero twice within the period of the sinus. and it is max twice within that period as well.<br>If you would briefly switch on the light when there is zero voltage and switch it off immediately again, yr lamp would not burn coz there is no voltage on it. If you would do the same at its peak, the lamp would burn at its brigthest. Hence a PWM signal is totally uncapable of regulating the brightness of an AC lamp in any predictable way.</p><p>The only way to do it in a predictable way is when you know where in the sinuscurve you are and then switch on the lamp.<br>If you would switch on the lamp when the voltage is zero and leave it on, it would get the full amount of power (Area under the curve). if you would switch it on halfway the sinus and leave it on, it would get only half the amount.<br>So that is how you regulate it... but ofcourse you then need to know when the AC sinus starts.... and that is why you need the zerocrossing signal.. to tell yr controller when the sinus starts<br></p>
<p>Hello. First of all thanks a ton for the amazing tutorial. Really helped me put together my own home automation system. </p><p>I am facing a problem though. The circuit works flawlessly under normal situations. But if i connect a load to some other source in the house there are momentary flickers in the load connected to the circuit. I am assuming this is caused due to noise in the circuit. </p><p>Here is the link to a video-<br><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/45WM_nNSN-o" width="500"></iframe></p><p>Would appreciate any help what so ever. </p>
<p>I actually have never experienced that but indeed when you plugin another load, especially repeatedly, you can get spikes on yr mains lines and that may either directly tigger your triac However, it may also put spikes on the psu of your Arduino (or other microcontroller).. To pinpoint the problem try doing the same with yr arduino disconnected or fed from a battery.</p>
Thanks for your prompt response. Really appreciate it.<br>I will give it a try today and let you know, if it works for me.<br>Thanks!
<p>Hello,</p><p>I have tried using battery as per your suggestion but it didn't work. Even with battery supply, if there is another load connected to any other switch in my home, bulb flickers when it is off.</p><p>The code doesn't even dim the light. Below is pseudo code -</p><p>while(1){<br>switch on the bulb<br>wait for 30 sec<br>switch off the bulb<br>wait for 30 sec<br>}</p><p>With this logic, it just turn on and off the bulb and stays in one state for 30 sec.<br>If I try to connect another bulb to some other switch (like shown in video), bulb connected to this circuit flickers when it is off.</p><p>Also, if I switch on the power supply (AC mains) to this circuit, there is an initial flicker (on and off just once). Here is the video.</p><p></p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/fLYwiOE-K0g" width="500"></iframe></p>
<p>first got to sauy that instructables acts weird. My earlier reponse to you seems to have disappeared. Good thing I check otherwise both of would be thinking: why no reply?<br>Anyway. Just like you to do one more thing. disconnect your arduino completely, connect yr circuit to the mains (lamp should be off) and then try to insert a seperate load into a socket again and see what that does. (I expect there will be a flickr again).<br>What I tried to do is to seperate between a trigger pulse coming in through your arduino or a trigger pulse coming directly onto your gate.<br>pending outcome of the recent test i just proposed, I presume it is most likely your circuit suffers from so called spontaneous triggering.<br>This is usually caused by noise on the mains line, causing a sharo dV/dt<br><br>So I would like you to check the following:<br>does the problem persist if you plug in your circuit the other way around? (basically switching phase and neutral)<br>Can you double check that it is M! of your Triac that is connected to the gate via the MOC and not M2?<br>It is hard to see from your video, but how long are the connections to your gate?<br>Which Triac type are you using?<br><br>There are some ways to combat sponteneous triggering (though one can wonder if it is really spontaneous).<br>The easiest would be a 1 k resistor between the gate and M2 and adding a snubber circuit. The latter though is kinda picky with regard to its values and if wrong may not do anything ta all, but a 39nf capacitor in series with a 100 ohm resistor over M1-M2 are often used. But... I hae known cases in which spontaneous triggering disappered wheb a present snubber circuit was actually removed.<br>Using less sensitive TRIACS is also an option. The TIC 206 I have in the circuit has a rather sensitive gate. The BT139-600H e.g. is relatively resistant to noise on the gate (it has to be an H-type)</p>
<p>well that proves it is not any glitches on yr arduino's powerline. Can you try one more thing? just disconnect your arduino and connect yr circut to the mains (lamp should be off) and then insert another load?</p>
<p>127? D&aacute; pra usar o mesmo circuito de potencia?</p>
<p>Zelfde kring maar andere waarden, gewoon halveren is het beste</p>
<p>Em 127 V, consigo usar o mesmo circuito de potencia ou tem que alterar o valor dos componentes?</p>
<p>Dat staat allemaal beschreven in het artikel. Bij 127 Volt kun je de weerstanden halveren</p>
AC Light Dimmers from KRIDA Electronics:<br><br>http://www.ebay.com/itm/111764492631<br>http://www.ebay.com/itm/111764507278<br>http://www.ebay.com/itm/121752461158
<p>kinda looks similar to mine, Difference is the price: ebay modules 21 USD + 5 USD shipping, my design: about 3 euro's and some elbow grease.<br>As the seller is in Latvia, like you, I presume you are the maker, so in fact this is a bit of spam, but I'll allow it.</p>
one more question sir. can i use moc3041 place of 4n25 . thanks
<p>no you cannot. It is a thyrister optocoupler with zerocrossing detection</p>
thanks for your help
<p>by the way, if you havent built this yet: I am working on a circuit that can use PWM to dim a lamp. The software will be much simpler with that</p>
i watch the video , when you post PWM to dim lamp? . Can that circuit dim ac fan or motor
<p>oh ad that PWM circuit is NOT suitable for an AC fan or motor</p>
<p>hello sir</p><p>because this circuit does not can dimming a AC motor</p>
<p>correct, or at least it isnt really suitable for that.<br>AC motors cannor be 'dimmed'<br>One can regulate them by feeding less voltage, but as they are an inductive load, there is a phase difference between current and voltage which makes this circuit less suitable</p>
<p>Very thanks for your reply,</p><p>then, what circuit you recommend for regule an AC motor</p>
<p>it largely depends on th emotor you have, check here for some options: </p><p><a href="http://www.instructables.com/community/Speed-Control-for-AC-Electric-Motors/">http://www.instructables.com/community/Speed-Contr...</a></p>
<p>Very thanks,</p><p>I have other question</p><p>I can use the circuit for dimming a lamp , for regule the speed of an universal motor, or a bifasic motor?</p>
<p>I have a working circuit already, just need to make some pictures before posting on insructables. However, I published it here already: </p><p>https://arduinodiy.wordpress.com/2015/07/25/1498/</p>
<p>my pleasure</p>
Can I use 4 diode for bridge rectifier , if yes then how i connect the 4 diode with 4n25 or mct2e.
<p>A big thank you for the explanation!</p>

About This Instructable


299 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: Simple EEPROM Module for Arduino or other microcontroller 433 MHz Coil loaded antenna Using pin 1 on Attiny13 without messing with fuse bits
Add instructable to: