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

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>
<p>Thanks Garret. Always glad to see what other people made of it :-) Looks pretty neat</p>
<p>hello sir</p><p>i am student of 7th sem i need your help.</p>
<p>well, what can i do for you?</p>
Can I use 4 diode for bridge rectifier , if yes then how i connect the 4 diode with 4n25 or mct2e.
<p>yes you can. You connect it the same way as you connect the bridge rectifier as a bridge rectifier IS 4 diodes in a small package</p>
can I replace 4n25 with mct2e ? IF YES, Dose mct2e need bridge rectifier ? thanks a lot for this post.
<p>yes you can and yes it still needs a bridge rectifier</p>
<p>Can I use any of the Arduino's PWM PIN to control the dimming. Basically I need to do other things at the same time and leave a free CPU resource.</p>
<p>PinakiG, I think you misunderstand the circuit. It doesnt work with PWM<br>It works by delaying the ignition of the TRIAC a set time from the zerocrossing..<br>If your CPU needs to do other things at the same time, there are couple of options: use a timer interrupt for the delays. You will find such a program in the program section of my instructable, or wait for my new instructable in which I show how to dim an AC lamp with PWM. That makes the software a lot easier</p>
<p>i have a problem my circuit is not sitching off the bulb..</p><p>any one who can help me out</p>
<p>could you give a bit more details? you mean it is dimming the bulb but it doesnt dim down all the way to OFF ?</p>
<p>Hi, could anyone tell me, which capacitor i really need for inductive loads (Which type) ? and is anybody having some eagle schematic and board?<br>cheerz</p>
<p>ceramic or film</p>
<p>thx for this quick answer, could you tell me a Type who is working?</p>
<p>they both are but since ceramic capacitors get very pricy for higher voltages, I'd recommend film</p>
I am not sure how thick yr traces are so I cannot comment on whether your pcb will hold 1 amp, but soldering a copper wire over the traces to the T1 and T2 of the TRIAC is always a good idea if you use a sizeable load
<p>Awesome instructable, thanks for all the extra detail and theory you put into it, I've learned quite a lot :)</p><p>I built one today and it works great! Used an IL250 and BT136 and etched your board design. </p><p>I plan to run a few bulbs in parallel, which I think should work fine so long as I stay under the 4A limit of the triac? I'm planning to write code for a pulsing/flickering effect, is there anything you think I need to consider?</p><p>Thanks again for putting this together :)</p>
<p>I am happy you like it and thanks for inclusing pics. I always enjou seeing what people made out of an ibble.<br>in spite of some people experiencing problems, it is a simple design that really cant go wrong. I have built 5 or 6 that all worked first time. <br>Running bulbs in parallel should be fine, indeed as long as you stay under your Triacs specs.<br>Code for pulsating should not be too difficult.<br>Write an alternating High and low value to the circuit and that should do it.<br>If you want the arduino to do anything else than regulate your light, I advise to use the timer rather than the delays in yr software<br></p>
<p>You're welcome :) One quick question regarding the track thickness - you mention that the load/mains tracks won't take 4 amps, is it worth just reinforcing them with wire anyway? I'm going to run maybe 200W total at 240V, will they cope with ~1A? Thanks!</p>
<p>what is yr question Rathore? :-)</p>
<p>The InMojo AC dimmer mentioned above is back in stock and can be found here:<br><a href="http://www.inmojo.com/store/inmojo-market/item/digital-ac-dimmer-module-lite-v.2/" rel="nofollow">http://www.inmojo.com/store/inmojo-market/item/dig...</a></p>
<p>I have a question, if i want to use this circuit with 120V 60Hz Which elements I should change</p>
<p>that is ad extenso decribed in the instructable :-) and even answered inthe question right below :-)</p><p>Basically it comes down to adapting the resister values to the optocoupler and using 8.3 usec steps instead of 10 us</p>
<p>Hello again diy_bloke! Thanks for this wonderful instructable! I do have a question, if I may. Since I am in the US (120 VAC/60 Hz), what are the main things I should be concerned when building this? My intuition tells me that this should be completely fine and your circuit should still work... what do you think?</p><p>Thanks in advance!</p>
<p>the text mentions what changes you need to make. basically it comes down to adapting the resister values to the optocoupler and using 8.3 usec steps instead of 10 us</p>
My apologies! I skimmed through the text and neglected that part. I will be more careful next time. Thank you very much!
<p>no problem. I have been known to overlook whats in front of me sometimes as well</p>
<p>Thank you ... :)</p><p>but i used (30Kohm 1/4W) x4 (&quot;+&quot; x2 and &quot;-&quot; x2)</p>
<p>if that works for you,great. Thanks for the picture. I Always like seeing what people made of it :-)</p><p>looks great </p>
<p>DIY_Bloke</p><p>This works well I am making my self a 4 ch dimmer and your code has helped me immensely, </p><p>My only suggestions is a resister and Capacitor should be added to your triac circuit to reduce noise emitted to mains and to limit triac misfire due to noise on the mains.</p><p>What my struggle at the moment is I want this to operate of a single button, I will post once I work it out. effectively One press will trigger light off (if light on) or Light on (if light off) and if held will dim up (if at 0 or last dimed down) or dim down (if at full or last dimed up).</p><p>This will mean you can us momentary buttons in single mech wallplates</p>
<p>I am happy you like it. With regard to the snubber network you suggest, I think I mentioned that somewhere in the article and yes that is a choice. I myself havent really felt the need for it as i havent experienced triac misfire. Audio is another reason why you could consider a snubber, but I hardly operate the two at the same time. But if you decide to regulate inductive loads it is a must.<br>Snubber netwoks can however also introduce problems.<br><br>It is certainly possible to use a button as you describe, but you may wonder if you want to sacrifice an arduino on that, as there are build in dimmers who do what you describe.<br>Good luck</p>

About This Instructable


277 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: 433 MHz Coil loaded antenna Using pin 1 on Attiny13 without messing with fuse bits 'Home Alone' burglar deterrent/ Use Attiny13 to drive a Steppermotor and a Servo
Add instructable to: