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

The only reason I can think off why the moc died is if the TRIAC burned and a full 220V came over the moc. that is why i asked to test the circuit without the moc.
I will try as soon as I arrive at home on Tuesday. By the way, I changed both components due to availability and using Liteon's moc3023 and STM's Z0405MF0AA2. Looking at the specs, both should be also OK, what do you think?
Just to be sure I was clear enough before, i am using both components since the beginning and not yours. But I think it shouldn't be the issue since the whole circuit was working over a month
<p>I build a prototype circuit on perfboard to check how the circuit performs. 4N25, MOC3021 &amp; BTE137 used. Load is 230V 70W halogen lamp with a 6A circuit breaker for basic safety. Arduino pro mini and circuits runs flawlessly on 3.3V or 5V.</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/6xaxQnqF-AI" width="500"></iframe></p><p>I'm going to etch a PCB as soon as possible.</p>
<p>looks great. Thanks for sharing</p>
Good evening, <br><br>First of all, congrats for the great Instructable. It was running for days perfectly but suddenly led is now always ON. The only thing I made different was to connect a non dimmable load (a old laptop) in the output which consumpts about 300mA to see what's going on. Now, I checked the circuit and in the moc input I have 0 but in the output (triac gate), I have a 1, so it's clear MOC is now damaged. Do you think it was casualty or trying to dim a non dimmable load can damage the MOC? <br><br>Thanks in advance, <br>Peter
Peter, It is a bit too much of a coincidence, not te be related to plugging in yr laptop.<br><br>I just hope it was just your moc that got killed. Can i suggest to remove the moc, attach a lamp, then plug the device into the mains again and see if the lamp lights or not?<br>just to check if yr TRIAC is ok
Ok, I will do that. If it was a coincidence, is there any possibility to reduce chances to kill the MOC? I mean, a preventive circuit or something similar? I didn't use it so much to kill it.
<p>i am not 100% confident, where could i buy one ? i really nead it</p>
<p>checj inmojo.com </p><p><a href="http://www.inmojo.com/search/?type=products&s=dimmer">http://www.inmojo.com/search/?type=products&amp;s=dimm...</a></p>
<p>Hello, First of all I am very thankful to you...</p><p>Excellent Document and each concept is explained very well...</p><p>I still want to make this compact in size. So instead of using Arduino, can I use directly atTiny 85 Controller? Will you just provide the AVR code for the same which is normally done in AVR Studio.. </p><p>Hope for positive response....</p>
<p>As the attiny 85 has an external interrupt, an output pin and a correct timer, I see no reason why that should not be possible, or, in other words, yes i think you can use an attiny 85</p>
<p>Thanks a lot....</p>
Hi<br>Can we convert AC current to DC current and use fading sketch like led<br>And remove zero crossing part??<br>Thanks for your effort
<p>something like this? </p><p><a href="https://www.instructables.com/id/AC-PWM-Dimmer-for-Arduino/">https://www.instructables.com/id/AC-PWM-Dimmer-for-...</a></p>
<p>Thanks a lot! </p><p>With your tutorial I was able to do this dragon ball lamp :)</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/sMglBAEWoIo" width="500"></iframe></p>
<p>I am happy it helped you. Thanks for sharing your pics and video</p>
<p>This is better and more safe way to control AC light dimmer</p><p><a href="https://www.instructables.com/id/Control-AC-Dimmer-Lamp-Using-Arduino/" rel="nofollow">https://www.instructables.com/id/Control-AC-Dimmer-...</a></p>
<p>This instructible is by far the most comprehensive piece of work I have ever read! AND it does not use this platform as a free advertising opportunity...</p>
Thank you gsmhack :-) I appreciate it.
<p>Well, &quot;better&quot; is up for debate and 'safer' is ofcourse nonsense, but i do understand you are trying to promote a product that is sold by you for 49USD, whereas mine will be a few dollars. <br>Seems you have a month left to generate the remaining 99% of your target. I wish you a well meant success<br> </p>
<p>as a maker i tried many way to control AC voltage devices and i did the same circuit before,first better when you want to integrate it with more advance application and add more sensors or motors or control it from wifi or BT or even from computer using serial communication,with your way it's only good for this application and with interrupt routine occupied by zero cross and triac driver trigger action you can't build a many functions project.<br>all i want to do is to let the people see that there are different way to do this and also control it with different platform&quot;not just arduino&quot; and it's not for the campaign,even if the campaign fail we will continue support and launch this product again because we believe it will be useful or many people.</p><p>thank you so much and I apologize for any inconvenience caused by these two comments</p>
<p>Hello , excelent Post, maybe you can solve one doubt that I have. To turn on the lamp for example (in the common way), you have to connect one side of the lamp to the power supply (Neutral) either (220V or 110V) and the other side the lamp to the phase, but the wire connected to the Phase you have to cut ir and connect to a switch. Every thing ok until here.</p><p>My question is: I just bought a commercial dimmer (Lutron) and is easy to connect you only disconnect and remove the normal switch and connect the dimmer. but inside the wall you have only one wire cut and this wire is the Phase, how the commercial dimmer works only with one wire (Phase) if in this post to dimerize the lamp we used the Phase and the neutral connected in one terminal block, and on the other teminal block the lamp.</p><p>Is possible to use the circuit board of this post in the way of a commercial dimmer, only with the phase?</p><p>I really appreciate your answers.</p>
<p>I apologize that i overlooked your question for a few days.<br>Well the dimmer you describe works in series with the lamp, but in reality ofcourse it is still using two wires...that you get by cutting the onewire that goes to the lamp,just as a switch needs two wires, but yes, it is still one phase. So that dimmer is a typical series circuit: the neutral wire goes to the lamp, a return wire goesfrom lamp till dimmer and then the phase goes from dimmer to the grid again, so yes, by interrupting the one wire to a lamp and putting the dimmer in between, it dims in series with the lamp.</p><p>The dimmer here works exactly the same: it is in series with the lamp and thus the TRIA just interrupts one wire. S also here we have a wire from the grid going to the lamp and a return wire going to the triac and from the triac back to the grid.</p><p>So far so good. BUT!!!! the dimmer needs to measure the zerocrossing of the grid and for tht it needs both phases</p><p>So, to answer your question......no, you need two phases.<br><br>I presume you want this so you can replace a switch with this dimmer? but even then you would still need to have 3 wires coming from that switch to your microprocessor: the zero cross signal, the trigger signal and a ground.</p><p>I think that in most countriesit is against code to put a circuit that has a low voltage side inside a regular wallsocket and or have wires coming out of itso this circuit is more suitable as a separate dimmed socket<br><br></p>
<p>Thanks for your really good explanation, in fact I was thinking in replace my common switch with this dimmer because work great.</p><p>Maybe can help me with other question in the sketch of step 7 (Software to set level using up an down buttons) I can see a variable called dim2 and in the comment (led control) is necessary to change some code to dim to LED bulb ?, because with the code showed above the LED bulb has two states ON / OFF and doesn't dims.</p><p>Thanks again.</p>
<p>When I wrote the instructable, I thought it was a good idea to gather some example codes from internet that i thought would benefit people. I was in the presumption that these codes were tried and tested, but apparently they have not been. So if that particular code does not work it is probably best to take my demo code as a basis and just ad or subtract from the dim value when the uo or down button is pressed</p>
<p>Excelent post, thanks a lot for share your knowledge with Us. People like you help make knowledge available for all.</p>
<p>hey thanks for sharing. i try to pwm the ac lamp with opto triac circuit. in my country the voltage is 220v 50Hz. when i pwm the lamp, the lamp is blinking. the higher pwm i gave the faster lamp blink. when the pwm is 255 the lamp is fully on. is that how it works? or i have to use spesific code not only just analogWrite ? thank you</p>
<p>Well this light dimmer doesnt work correctly with PWM. May I suggest you read the article as that explains and shows exactly what program(s) to use</p>
<p>Thank you for your reply. I actually want to controll the water temperature using heating element. Is your PWM program(s) that you used for dimming the lamp can used for PWM the heating element too? I used triac in my final project.</p>
<p>can i just start by again emphasizing that this is NOT PWM</p><p>yes it can be used for an AC heating element but not with pwm, it works with leading edge phase cutting</p>
<p>okey thank you for your explanation sir. it's very helping me to finish my project</p>
<p>my pleasure</p>
Does it Matter Where you connect live And neutral ? <br>Thank you. <br>Marc.
<p>For the workings not really</p>
Hallo, can i use this circuit to dim a High power LED, or i need additional stuff or there is differen circuit, please answer
<p>well it depends what you mean with high powered led. Is that the 3Volt 3Watt LED or an 36 Volt 100Watt LED array, or is it an LED u can screw into the mains grid??</p><p>If it is the latter the answer is 'most likely not' if it is the former my answer would be 'yes with some additional hardware'</p>
<p>Hello! Im a student and I have basic knowledge of electronics and the arduino. I was thinking if it would be possible if I add sensors while using this circuit for dimming. Particularly, pir sensor and photodiode. Moreover, i wanted to also add bluetooth module for additional manual control of the led bulb. Is it possible or is it too much for the arduino to handle? I have lots or questions for this project because its very nice and i want to add more features. thank you. kudos also to your very detailed instructable</p>
<p>it is not too much to handle for the arduino, but just know that the first program is a demo program that spends most of its time in a delay loop. If you want to do something else you need to replace he delays with some sort of timer function. Also in writing yr program keep in mind that there is an interrupt 100 times a second</p>
<p>will a 1A bridge rectifier work out fine?</p>
<p>if your load is not more than 220 Watt that should be fine. Make sure to put a heatsink on the TRIAC as well</p>
<p>for the triac, the only available in our area is the TICP206. will that work out also? http://www.alldatasheet.com/datasheet-pdf/pdf/20147/POINN/TICP206.html</p>
<p>yes, most likely. it has an RMS of 1.5 amps but it may be hard to put a hetsink on it so best not push it to its max</p>
<p>how many amperes is the 400 V bridge rectifier?</p>
<p>depends on your load</p>
<p>Hello,</p><p>I want to dim a 12v halogen lamp. I have a dimmable halogen psu, but I have to drive it with a TRIAC, from what I understood.</p><p>Is this circuit suitable for my needs?</p>
<p>You mean that it is a 12 Volt halogen lamp that is attached to a PSU that goes into the grid? and that PSU is labeleld as 'dimmable'.<br>If it is for a traditional TRIAC dimmer then you will have a good chance that this circuit will do the job </p>
<p>thanks for your reply,</p><p>I tried it with my configuration, but seems that my electronic psu prefers trailing-edge dimming. I get a strange behaviour of the halogen lamp while a normal lamp works great when attached directly to your circuit.</p>
<p>I have try the code for &quot;AC Voltage dimmer with Zero cross detection&quot; , I only can see the light glow and dim again and repeat again. Is this the result ? </p><p>for the code by using push button, I only see the light is flickering.</p>
<p>then most likely a hardware problem. Please check and recheck all yr connections</p>

About This Instructable




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:Connect to Carriot with ESP8266 BruteForce codefinding for Infrared RGB LEDstrip Using the 4 pins of the ESP8266-01 
Add instructable to: