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

Thank you for the tutorial. I am new to Arduino and this is the first 'bigger' project that I am doing with it. I made the circuit and it works perfectly with the code from step 4. However, I can't get the code from step 7 (with up and down button) to work. I connected the two buttons to pin D4 and D5 so that when the button is not pressed, the pin is connected to ground via a 10k pulldown resistor. When the button is pressed, the pin gets connected to 5V. When I press the buttons, nothing happens. What am I doing wrong? <br><br>My goal is to control multiple lights (dimmable led lights) independently via bluetooth using my phone. I still have to learn a lot about sketches and how to make/adjust them, but I am enjoying learning so I think that should not be a problem.<br><br>Thank you!
<p>well seems to me you didnt connect the buttons right. both buttons connect to ground, not to 5 volt and you dont use a 10k pulldown.</p><p>It is actually easy to see from the code as the loop checks for a LOW state of the buttons, not a HIGH state to alter the value of DIM.</p><p>Now having said that... when i made this device some time ago I collected some software from the web that i thought would benefit people and this was one of those. I then presumed other people to be more knowledgeable on the dimming software than me. Now i see some shortcomings in this code that i would certainly suggest you change in due course</p><p>In the loop you see the following lines:</p><p><br> digitalWrite(buton1, HIGH); </p><p><br> digitalWrite(buton2, HIGH);</p><p>What these do is to invoke the internal pull up resistor, after all, we use these as inputs.</p><p>However, it is unusual to keep doing that continuously in the loop, it is usually done in the setup. It is probably done this way so you can keep the button pressed, but it isnt very elegant.</p><p>Anyway, i trust that changing the buttons, so betewwn D4 and ground and D5 and ground will solve yr problem</p>
<p>Hello, it's a very nice tutorial!</p><p>I have made a circuit for 3 bulbs and it works well with them. However i would like to implement it with some LED lamps which you can find it here: </p><p><a href="http://www.bright-led.hk/a/Products/LED_Outdoor_Lighting/LED_Street_Light/262.html#.V2u2fZdzK9" rel="nofollow">http://www.bright-led.hk/a/Products/LED_Outdoor_Li...</a></p><p>with the AC input voltage. So, what is happening when I connect it with this circuit? Sometimes the lamp is turned on and then it turns off for a few second, sometimes the lamp flashing, and so on. </p><p>Does anyone have any suggestions? Should I change something in the circuit, or maybe these lamp aren't appropriate?</p>
<p>The lamps are not appropriate. As LEDs typically work on a low voltage, there are electronics in the lamp to bring it to that low voltage. Those are not compatible with dimming by phase-cutting</p>
<p>Ok, thank you. Do you know maybe for any other solution to dimm these lamps?</p>
<p>without opening them i dont think there is</p>
<p>it largely depends on the LED, some will respond to a traditional dimmer, some need a 1-10 v voltage for dimming signal. Your lamps, sorry do not really see a possibility for those</p>
hi friends!<br> I'm trying to implement AC dimmer circuit to dim LED bulb(which generally use in pop) but seams its not working...any solution?
<p>it only works with specially suited LED bulbs. What type of Bulb do you have?</p>
Diy Broker, can you put in Blue ac output And in Red load ??<br>Thank you it will greatly help me... Have a Nice day.<br>Marc.
<p>Also in the component lay out on the PCB (2nd pic) it actually says 'Mains' and 'Load'</p>
<p>The AC output goes to the load, but i guess you mean the input.</p><p>If you look at the 3rd picture of the pcb with the red text on it, at the right side it has two connectors. The top one, that has the two resistors pointing to it, is the ac input. The bottom one, that has the single jumper wire pointing to it, is the connector for the load</p>
<p>Hi,</p><p>I'm totally new to electronics but not new to programming.</p><p>I didn't pay attention to all those warnings about killing my self hahaha</p><p>how even I took some distance while turning it on ;-P</p><p>Just got one question, I have some light flicker, probably if a other device is drawing high power in the house, is there a way to stop this, maybe capacitors???? </p>
<p>Odd, I replied to your comment but I see it hasnt shown up. OK here we go again: Flickering is usually caused by one of a few things: The PSU, or the timing. A dirty PSU may cause flickering make sure it is well decoupled.<br>timing may also be an issue as the zerocrossing signal has a certain width: it starts a bit before the actual zerocrossing and ends a bit after. Usually the flickering then is either at th elowest setting or at the highest setting. Is this what is happening in your case?</p>
<p>I have read all the comments and done your suggestions but for some unknown reason my 2 resistors get burning hot. I started with 10K and went all the way up to 120K (1w and 2w).</p><p>Interestingly when I turn off the arduino, they don't get hot or even warm at all.!! but as soon as I supply power to my arduino, those two resistors become hot in matter of 2-3 minutes...</p><p>please look at my schematic. That red thing on top right is my 5v power supply. I also removed that and tried to power up the arduino from my ICSP but that did not change anything. </p><p>what do you think the problem is?</p>
<p>That is fairly simple. You didnt follow my circuit. You put the two 33 k resistors in parallel. I have them in the two seperate powerlines (neutral and phase) so they are in series.<br>I also dont understand why you started with 10k when it says 33k.<br>With the 10k in parallel you created a 5 k resistor that had to deal with about 10 Watt !!!!<br>the 2x 120 k in Parallel should have been OK.<br>Turning off the Arduino should make no difference according to your circuit</p>
<p>I tried 10K just to see what would happen. 2x 120K 1w got hot. I tried 2x 220K 1/2w and it seems to be ok. they become warm but not burning hot. I will try 220k 1w and will see what would happen.</p><p>My bridge rectifier used to also get hot when I tried 120k and lower values. However it did not get hot when I tried 220k.</p>
<p>no need to justify. Experimenting is the way to learning :-)</p><p>when you have 120 k in practice you have a 60 k resistor which is in the same region as the 2x33k in series that i have, they each shld dissipate 400mW that is enough to make them warm, not hot, Same for your rectifier. Are you sure you dont have a stray connection somewhere on your pcb?</p>
<p>thanks for teaching me everything to know about dimmer theory and providing examples and troubleshooting information as well. Will certainly have use for this instructable in the future.</p>
<p>My pleasure Rob. Thanks for your kind words. Glad you found it informative</p>
<p>I'm interested to know if you tried it with an inductive load, like an AC fan.</p><p>On the other hand, if one were to shoehorn this for a fan like that, what sort of snubber would you put in that doesn't mess up the firing? Something like a 45W AC fan.</p>
<p>I havent tried this circuit with a fan, but if i were to, i would start withsa a 100nF capacitot and maybe a 56 or 47 ohm resistor</p>
<p>I made a similar circuit(a little bit different) with Atmega8A/BTA16.The speed control works fine while on pure sine wave AC.I have an inverter which output square wave AC.The circuit malfunctions in square wave(The fan stops within first few minutes/great hum with very low speed/Great hum from inverter/No movement of fan).I am controlling speed of my ceiling fan.Is the mafunction normal at square wave ??</p>
<p>I am not quite sure what it is that you built. What is the inverter for? why do you use square wave if you have a functioning sinewave?<br>You need to give me a bit more info. preferably a circuit</p>
<p>Well...I am trying to control the speed in 5 different steps namely 0 to 4.</p><p>In 0,the fan stays off as the MOC3021 gets 0 from the Atmega8a's output pin 12.In 4,the MOC3021 is always on so the fan rotates in full speed.The speed control is done in 1,2 &amp; 3 steps by firing the triac in 3 different delay timings correspondingly.</p><p>my circuit works pretty well in the 220v electric supply(which have a pure sine wave).But whenever main fails and Inverter power takes place,the circuit starts to malfunction(The fan stops within first few minutes/great hum with very low speed/Great hum from inverter/No movement of fan).</p><p>I attached my DSO and found out that the Inverter generates pure square wave.I also made several changes in the triac driving section as listed below:</p><p>1.Replaced the BT136 and used BTA16(snubberless triac).</p><p>2.BTA16 with no external snubber attached.(omitted the external snubber).</p><p>3.Replacing the 470R+330R with a 1k and also attached another 1k from Pin4 of MOC3021 to Triac pin MT1.Also omitted the 47nF cap.</p><p>4.Add a 100uH choke between MT1 and LOAD.</p><p>5.Reattach snubber circuit with this position.</p><p>I also tried the stated configs with BT136 also. </p><p>I tested with all the above mentioned configs but each and every one failed to regulate fan speed on my Inverter output(Only 0 and full speed i.e-4 is functional).But all the above configs performs very well in Supply AC.</p><p>My question is what is necessary to control speed of my ceiling fans while on inverter power??</p><p>Please reply....I am attaching my circuit diagram</p>
<p>ok, it is clear.i thought somehow you had 'an inverter' in yr circuit, but basically your circuit is exactly the same. Your question is: It works on the sinewave of the grid, but it doesnt work on emergency power which is a square wave. So in hindsight the circuit wasnt necessary</p><p>You are not the only one with this problem, dimmers not working on DC-&gt;AC converters that supply square wave. The odd thing though is that the explanation for that, doesnt apply for this type of dimmer. However, a fan is not a pure resistive load as there is a phase shift. Although a Triac dimmer can be used for a fan it is not the most ideal and I think it is the square wave in combo withthe phase shift that makes the Triac not ignite. Can you do a small test? Instead of a fan, try dimming just an incandescent lamp when you have the dimmer work on your inverter and let me know what happens</p>
<p>Thanks a lot for the reply.....out of curiosity,i attached a small table fan with this circuit and the speed control was working for some time while on inverter.But i didn't conducted the test for a long time and also didn't pay much attention as my main goal was to control the ceiling fan speed .Of course i will conduct those tests with both the small table fan and a light and let you know..</p>
<p>ok let me know</p>
Hi i cant figure out what is The load and what is The ac input can you help me plz Thanks
<p>the load is where it says 'load' and the input is where it says '220 V'</p><p>on the pcb it is same as in the circuit</p><p>; the top one is the AC, bottom one the load.</p><p>good luck</p>
<p>Hello. I made a board for the circuit, although I couldn't get the TIC206 and found a replacement. In fact I've tried 2 TRIACs and have no clue why it's not working after studying the datasheets. I have some images of testing. The first is the output of the 4N25 zero-crossing detector (1V/div, x1 probe), the 2nd is the arduino output to the circuit, and the 3rd and 4th are the output (pin 4) of the MOC3021, 5v/div, x1 probe and x10 probe. Looks like a little less than 220V RMS to me, but this is feeding the gate of the TRIAC. This happened with both the MAC228A and BTA30-800 TRIACS.</p><p>The bulb briefly flashes when I touch the scope probe to the gate. What do you think I've got wrong?</p>
<p>unless the two dots in picture 2 mean anything rather than being reflections I would say that there is no output from the arduino.</p><p>Did you measure it directly on the port or maybe on the input side of the optocoupler?</p><p>please do the following:- put yr arduino aside and put 5V on the inout of the circuit.</p><p>is yr lamp on?</p><p>remove the 5v input</p><p>is yr lamp off?</p><p>if that doesnt work, check yr circuit.</p><p>if that works, please attach yr arduino and write a high to the pin connected to the circuit.</p><p>if that doesnt work, check the connection withyr arduino, make sure it is the proper pin.</p><p>let me know the outcome</p>
<p>Nupe.</p><p>Those two dots are from a test program I adapted from your code. They are from the arduino and go back and forth as I cycle from 5 to 128 and back.</p><p>I'll go through it with a multimeter tonight and check for shorts</p>
<p>And I tried a simple HIGH on the arduino with no joy</p>
<p>does that mean you wrote a HIGH to the pin and that had no effect?</p><p>in that case, write a high and measure the DC voltage on the low voltage/input pins of the optocoupler and subsequently the AC voltage over the secundary/high voltage side of the optocoupler</p>
<p>AC 239V (as expected - I am in the UK)</p><p>DC was 1.17 which I thought was a bit low, so I changed R2 from 470R to 270R. Now it's 2.21 which may be still a bit low according to the datasheet. I'll go lower still and see if it works.</p>
<p>?</p><p>I changed it to 180R and the voltage dropped to 1.3V</p>
<p>I've checked my board layout and it's exactly the same circuit as yours. Could it be my choice of TRIAC? BAC30-800W</p>
<p>Hello diy_bloke. I finally found the answer to my frustrations. I found a post on the edaboard.com forum. Vital information - I haven't seen a single circuit description that tells you that &quot;PLEASE BE AWARE THAT...</p><p>the moc output MUST BE between the triac pins A2 and G<br>it is never written in any datasheets but it MUST be so<br>or your triac won't commute ...&quot;</p><p>...so that's the secret! The dark side of electronics is that these &quot;trade secrets&quot; take an age to discover. Once I understood that it was plain sailing.</p><p>Here is the thread on edaboard: <a href="http://www.edaboard.com/thread155997.html" rel="nofollow">http://www.edaboard.com/thread155997.html</a></p><p>Very useful in understanding what is going on in the circuit!</p><p>There's actually two types of triac on my 6-channel board (see photos) - BT136 and MAC228A10G, both working fine. Here they are just running a test pattern, fading up and then down, caught mid-fade.</p><p>Thanks for the circuit, but I suggest you mark out which is MT1 and which is MT2 on the triac, in case foolhardy hackers like me want to design their own boards.</p>
<p>The 239 Volts indicates your Optocoupler isnt triggered. 1.7 Volt is a bit low. What color LED did you use?</p>
<p>Thanks !!!!! diy_bloke</p><p>i was searching for this circuitry for quite a time ..</p><p>this article is of great help ..., i am going to make this project ... </p><p>if ill run into the problem then i will pinch u for the possible reason .. :)</p>
<p>My pleasure. Dont hesitate to ask if u need help</p>
<p>Thanks !!!!! diy_bloke</p><p>i was searching for this circuitry for quite a time ..</p><p>this article is of great help ..., i am going to make this project ... </p><p>if ill run into the problem then i will pinch u for the possible reason .. :)</p>
<p>i didn't understand how did you get the &quot;pulse starting abt 200uS before the actual zerocrossing&quot;... in my calculations, i dont know where is my mistake, the pulse starts 972uS before the actual zerocrossing =/</p>
<p>does this work with low consumption bulbs?</p>
<p>I am not sure what you mean with Low Consumption bulbs. Do you mean regular incandescent bulbs of low wattage? yes that works. Do you mean CFL lamps? no that doesnt work</p>
And thnks bloke, you've been very helpful
<p>my pleasure</p>
Why do we get a humming noise when we controll ceiling fan through a triac. I have made a remote controled circuit for controlling light and fan. Used the above zerocrossing method. When the fan is on full speed there is no noise but when i reduce the fan speed the noise is prominent at low speed the fan makes noise. I guess it is due to the reason that the fan gets 220v form mains and when i reduce the speed there is less current flowing through the Triacs. I don't know the exact reason of the humming noise but im sure there is no problem with the fan :-P what can ne the possible reason behind it and what is the solution. Thanks

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:Send sensor data (DHT11 & BMP180) to ThingSpeak with an Arduino, using ENC28J60 Ethercard  Using an I2C LCD on Attiny85 AC PWM dimmer for Arduino 
Add instructable to: