Introduction: AC PWM Dimmer for Arduino

Picture of AC PWM Dimmer for Arduino

Before you decide to build this, you may want to have a look at acolomichi's design as well

Warning: this project uses deadly voltages, as well as a sizeable capacitor to store these deadly voltages. Even after you disconnect the circuit from the grid, the capacitor still can give you a jolt to remember!!

Over 3 years ago, I published a simpel TRIAC AC dimmer for the arduino. That proved to be a very popular design. Yet in spite of the simplicity of the circuit the software needed was a bit complicated as it needed to keep track of the zero crossing of the AC signal, then keep track of the time and then finally open the TRIAC. So to avoid letting the arduino just wait for most of the time, an interrupt and a timer were necessary.

So why can't we just use PWM, like with LED's? Well, I explained that in that instructable, but there are possibilities to do that. Someone looking for that would no doubt end up at design by Ton Giesberts/Elektor Magazine that can do PWM of an AC source. This design is on Instructables as well.

That will work, but in spite of my admiration for Giesberts and Elektor, there is something fundamentally wrong with that circuit. I think it is necessary that I explain what is wrong before I come up with improvements. If you are not interested in the technical details, just skip to the next step.


At first glance, the Giesberts circuit seems like a complicated circuit, but we can bring it back to 2 or 3 components: A lamp, and a switch, but as in fact the switching is done in DC rather than AC, it becomes a lamp, a bridge rectifier and a switch. That switch, which is in fact the MOSFET and the components around it is controlled by the Arduino (or PIC or whatever). So, switching that on and off in a certain duty cycle will switch the lamp on and off and if done fast enough the lamp won't be seen anymore as flickering, but as being dimmed, similar as we do with LED's and PWM.

So far so good. The theory behind the circuit is sound. However, the MOSFET needs a voltage on its gate to be switched on and as we cannot get that from an arduino for obvious reasons (it is only 5 Volt, which isnt enough AND you don't want your arduino to be connected to the mains grid), Giesberts uses an optocoupler. That optocoupler still needs a DC voltage and Giesberts is using the to DC rectified AC voltage for that.

giesberts2And that is where the problems start, because he is feeding the gate from the MOSFET, with a voltage that is shorted by that same MOSFET. In other words, if the MOSFET is fully opened the DC voltage coming from the rectifier is completely shorted. Therefore there will be no voltage anymore to put on the gate and the MOSFET will block again. This effect might not be so outspoken by a low dutycycle (= lamp on a low intensity), because of the presence of C1, that will retain its charge for a while and will be receiving new charge thanks to the low dutycycle, but at 25-80% dutycycle the voltage on C1 just cannot be sustained anymore and the lamp may start to flicker. What's worse is that at moments that the voltage on the gate drops, for a while the MOSFET will be still conducting, but not be fully saturized: it will slowly go from its nominal 0.04 Ohm resistance to infinite resistance and the slower this goes, the higher the power that needs to be dissipated in the MOSFET. That means a lot of heat. MOSFETS are good switches but bad resistors. They need to be switched ON and OFF fast. Currently the circuit heavily relies on D1 to keep the voltage on the gate of T1 at acceptable limits while the voltage is swinging between 0 Volt and Full peak At peak the rectified voltage is 230x1.4=330V The average rectified voltage is 230x0.9=207V

If we forget about the smoothing effect of the capacitor for a while and presume the optocoupler to be fully open the average voltage on the capacitor would be 22/88 * 207 =52 Volts and in peak 22/88 * 330= 83 Volts. That it is not is because of D1 and the fact that the MOSFET will short the Voltage.

If the optocoupler is not in saturation and its impedance therefore infinite, the capacitor C1 would charge up to full rectified voltage if not for D1. On average 3mA will flow through R3,R4 and R5 (207-10)/66k which equals a power consumption of 0.6 Watt in the resistors R3,R4, R5

Improvements giesberts3

The problems mentioned with the Giesberts circuit can be remedied by putting the lamp somewhere else: remove it from the AC line and put it in the Drain of the MOSFET. For the lamp it doesnt really matter if it is receiving DC or AC. You could make more improvements, as now there is no need to cater for a a voltage swinging between 0 and 330 Volt

Step 1: AC PWM Dimmer: IGBT or MOSFET

Picture of AC PWM Dimmer: IGBT or MOSFET

But as I was changing the design, I might as well take it a step further and use an IGBT (Insulated Gate Bipolar Transistor) Simply put, an IGBT is a device that is a MOSFET at its gate and a bipolar transistor at its Collector and Emitter, making it an ideal switch. Thus we can come to the following circuit: igbt-11

The IRG4PC30 acts as a fast switch that either switches the lamp on or off. It needs about 12 Volt on its gate to do that. The voltage divider R1/R2 should put about 13-15 volts* max on the Gate of the IGBT, switching the lamp fully ON. As there might be some fluctuations on the grid 4k7 is a safe value. If you want to be safe, make sure you have an IGBT with a Base Emmitter breakdown voltage of >= 20 Volt and put a zenerdiode of 15 V parrallel to R2. Possible IGBT's are IRGPC40W or IRG4PC30, but basically any will do provided they have a Base emmitter voltage rating of at least 20 Volts When the optocoupler receives a signal, it opens and pulls the voltage on R1/R2 to zero, effectively closing the IGBT. The PWM signal of an Arduino is faster than the 50Hz Frequency so you will basically see the PWM signal modulated on the 50Hz rectified sine wave, making the effective voltage lower. This circuit is ONLY for incandescant bulbs. It is NOT for any inductive load as it is DC biased. With regard to the capacitor C1, I have tested it with 100uF but will probably work with lower capacity as well.


  • Although the average voltage will be 230*0.9, C1 may eventually charge to 310-330 hence 4k7 is a safe value.


Reader acolimitchi pointed out to me that if you add the zener, the 6k8 resistor isnt really necessary anymore, which is true, so the circuit becomes like this:


With regard to MOSFETs vs IGBTs both have their pro's and con's and the voltage and switching rate this circuit is operating under may just be in an overlap of both spectra. Using a MOSFET rather than an IGBT is therefore possible. MOSFETS are generally also cheaper than IGBT's. A tried and tested MOSFET is the STP10NK60Z (Thanks Pavel). This MOSFET has a gate-source breakdown voltage of 30 Volt and has clamping diodes protecting the the gate. Another one that worked quite well is the relatively cheap IRF 730. MOSFETs usually need a bit of a higher voltage than IGBTs to switch so a 6k8 resistor should be fine. If you use a MOSFET without clamping diodes a zener of 15 Volt is adviseable.

I couldnt embed a video, but you will find it here. It is a 60 Watt lamp. The slight flicker you may see when the lamp is turned up, is an artefact. It is caused by my cellphone camera trying to adapt to the changing light intensity

Conclusion: stick to the MOSFET, they are on average 4 times cheaper than IGBT's

Bridge rectifier. I used a 1 amp 400 Volt DIL pack 0.25 euro
Diode 1n4001 or 1N4007 0.10 euro
Capacitor 100uF 350-400 Volt 1.36 euro
resistor 100k 0.5-0.6 Watt (actually it dissipates abt 480mW max) 0.12 euro (play it safe with a 1 Watt resistor)
resistor 6k8 1/4 Watt 0.10 euro
Zener 15 Volt 0.5 Watt 0.08 euro
MOSFET IRF730 or STP10NK60 0.58 euro
Optocoupler 4n35 0.25 euro
Resistor between 330ohm-470ohm (possibly even 1k dpending on the Optocoupler) 0.10 euro

So, alltogether at single piece retail prices the cost is 2.94 euro

Step 2: AC PWM Dimmer for Arduino: Thoughts

Picture of AC PWM Dimmer for Arduino: Thoughts

Without any input signal the lamp will burn at max.

If you notice that When writing a zero PWM value to the circuit the lamp burns a bit less than when you switch off the Arduino, that means that the 4n35 is still opened a bit. Play around with t470 Ohm serial resistor, maybe you need a higher value.
The 100uF 400 Volt can be quite big. A 50 uF may work as well and maybe even a smaller value. However, you do not want the value to be too small thus as not to be able to deliver enough voltage for the Gate of the MosFet in fast switching.

Should you wonder if perhaps you can use a capacitor of a lower voltage -say 25 Volt- and connect that over the zener/6k8 resistor in order to replace the 400 Volt one... maybe that will work, but the 4N35 will make it lose its charge almost instantaneously and due to the large RC time (remember, there is 100k resistor) it might not have time to fully recharge again. At least it will make keep the MOSFET in its resistive phase longer, leading to extra heat development.

The Arduino has a PWM frequency of about 500 Hz. That is ample enough. My guess is that around 100 would be more than enough. The Attiny85 delivers I think 280 Hz on its PWM pins. Software ofcourse can always elevate that if necessary.

This dimmer is not suitable for inductive loads

This dimmer does NOT need zero cross detection.

Heat development:
I tested this with a 60 Watt lamp at full brightness, without any heatsink: the temperature rose with 9 degrees above ambient after half an hour and an hour.
Then I tried with continous dimming from zero to full and back again.: The temperature rose with 10 degrees above ambient after about 10-15 minutes and stayed like that for the hour I tried.

With a 150 Watt halogen spot the temperature went up 15 degrees. It reached max temperature after about 10 minutes and then stayed the same for the hour tested.

This was measured with a DHT11 sensor directly clamped to the MOSFET


FurkanY14 (author)2017-12-26

@diy_bloke thaks for that.

can we control 0-220VAC with pwm in this electronic design?

diy_bloke (author)FurkanY142017-12-27

yes, that is exactly the purpose

FurkanY14 (author)diy_bloke2017-12-27

i will test it with rpi but my load will be transformator. (max. 100A.)

as soon as i will give the feedback.

it made me very very happy to your answer. I appreciate for that.

diy_bloke (author)FurkanY142017-12-27

wait wait wait. NOT a transformor, absolutely not. It is for lamps

FurkanY14 (author)diy_bloke2017-12-27

why not @diy_bloke.

I have a transformor. primer voltage is 220VAC and seconder voltage is 48VAC. I just wanna affix the your designed with my primer pole.

I didn't understand the your anxiety

diy_bloke (author)FurkanY142018-01-10

apologies for my late reply. I have been ill.

the fact is that the load is fed with pwm DC. transformers cannot take that very well. Nobody is gonna die but DC may magnetize the core

DickM14 (author)2017-12-23

Hi, diy_bloke, thanks for the great work.

I have used this principle to build a circuit that dims approx. 1000w.

Problem that i have now is that when the arduino is powered off or initialising the load is on. How would one convert this so that is always is off when no external input is present? Any suggestions on this?

Regards, Dick

diy_bloke (author)DickM142017-12-27

you could put the optocoupler in series with the 100k resistor.
the design i made was mainly aimed at quick switching, thus creating minimal heat

DickM14 (author)diy_bloke2017-12-27

Thanks again, that would require a resistor to pull the voltage down across the zener wouldn't it? I still have a fait bit of heat especially in the rectifier bridge at 1000W...

diy_bloke (author)DickM142017-12-27

That would not be really necessary

AlexanderG143 (author)2017-11-13

Nice circuit @diy_bloke! I would build it with a TRIAC based circuit but my input signal is PWM and I cannot change that, so I ended up on this page. I need to build 4 of them, each controlling a 40W incandescent bulb.

I have 2 questions: Since my bulbs have a rather low power requirement, is there anything you would change to make it more safe?

Also for safety reasons, I modified it to be a normally open circuit (no Arduino signal => lamp off) since the circuit will be powered 24/7.

Would you add some fuses or thermal cutoffs to make the circuit safer and if so, where?

diy_bloke (author)AlexanderG1432017-11-14

Alexander, the power of th lamps isnt really anything that determines safety, so i cant think of any changes on that aspext.

A fuse can be put anywhere in the supply line, either in the AC line or t the DC line.

I have not tried myself but supposedly people have put the optocoupler in series with the 100k resistor to reverse the action

AlexanderG143 (author)diy_bloke2017-11-21

Thanks @diy_bloke!

As I said, I'm building 4 of them, each controlling a 40-60W incandescent bulb from 4 different PWM signals. Am I correct in that I can "share" the bridge rectifier between the 4 circuits?

By the way, I added a thermal fuse and a normal slow fuse.

diy_bloke (author)AlexanderG1432017-11-22

yes you can

AlexanderG143 (author)diy_bloke2017-11-27

Thanks again @diy_bloke!

Anything else you would recommend I could simplify for 4 of these? Is it better to have one capacitor (as opposed to 4) and if so, which size? Or would that cause it to drain too fast?

D1, D2 and R1 could also be reused if I'm seeing that correctly? Pretty much only R2 the optocoupler and the MOSFET need to be replicated, yes?

diy_bloke (author)AlexanderG1432017-11-27

I think 1 capacitor is ok as it is basically only there to provide power to the gate triggering.
If you want too trigger 4 channels maybe, just maybe the 100 would be enough, but I think I would go for at least 220.
There is a discussion going that it would be better to put the capacitor between gate and ground so it could be a lower voltage (and thus cheaper) and you might even get away with lower capacity as well, but in that case you would need 4.
However, the current circuit, with the 100 uF capacitor in the High voltage line is designed to allow fast switching of the FET and thus minimal power dissipation.
Anyway, the asnwer is 'yes', but consider using a 220uF

AlexanderG143 (author)diy_bloke2017-11-28

Hm, apparently my earlier comment wasn't posted.

Anyway, thank you once again for your time! I built it today (for one bulb), and it's working great, except, I must have done something wrong because my signal seems to be inverted. If I write a PWM value of 255, the bulb is off. If I write 0, the bulb is fully on (and of course the gradient between 0 and 255 is also inverted). That was a bit unexpected and I'm not really sure what I could have done wrong.

It's not a faulty Arduino (I tried with 2 plus the final PWM signal that I will have to use). Any idea what could cause this?

BastianJ2 (author)AlexanderG1432017-12-20


Inverting is quite easy. Only modify Pins 1 and 2 from the Optocoupler. Connect pin 1 to you Arduino Vcc and pin 2 to your PWM signal. Done. I ran it myself.

AlexanderG143 (author)BastianJ22017-12-21

Smart, and I see why that would work. But doesn't it damage the Arduino in the long run? How much current does the PWM pin have to sink in that configuration?

diy_bloke (author)AlexanderG1432017-12-02

you didnt do anything wrong, that is how it is supposed to work.
when you write 255 the output signal is fully HIGH, turning off the FET

HugoS85 (author)2017-11-29

Also, is there any problem if I change the 4n35 for a 4n25 ?

diy_bloke (author)HugoS852017-12-02

I think that shld not give much pruoblems

HugoS85 (author)2017-11-29

Hey diy_bloke :D
Nice job on explanations, this is the second lesson that I get from you.

I have one question, what would you change on this circuit to make it for 127V 60Hz ?

diy_bloke (author)HugoS852017-12-02

I would change the 100k voltage divider to make sure you would get enough voltage on the gate

PouryaMec (author)2017-11-05

Sorry,diy_bloke regarding comments,which circuit you and love_evidence writing about because I still need a circuit that work with 500w lamp.

diy_bloke (author)PouryaMec2017-11-27

Apparently I overlooked your comment. I am not really sure what circuit you are now referring to that we talkked abt as we spoke abt several circuits: The one in this instructable and teh Giesberts circuit that I touch upon in the begiing of this instructabe.
As for a dimmer with a TRIAC and a MOC3021, yes that is certainly possible as these form the basis of most AC dimmers. Look here.

PouryaMec (author)PouryaMec2017-11-06

And Is there any way to instruct a dimmer with triac and moc3021?

GuelW (author)2017-11-14

Hello @diy_bloke, first of all thanks for sharing your knowledge, this is a great circuit, built it in my board and it worked just fine, however what I really wanted is to control my ac ceiling fan, and you've mentioned in the article that this circuit is not for inductive loads, I'd like to ask you if there's anything in this circuit I could change so that I can control an inductive motor? I see your circuit is based on DC, I've thought about placing the load in the AC line rather than the DC one, but I don't know what this can cause as I'm not an expert, also my house runs over 110-127ac instead of 220ac, I'd appreciate if you could point me out to any direction which would help me in accomplishing my goal, in the meantime I'll keep looking for others ac dimmer circuits, yours was what most grabbed my attention as it's very simple in comparison to others I've seen, sorry if my English was not clear enough, I'm not a native speaker, cheers from Brazil.

diy_bloke (author)GuelW2017-11-18

if you want to use this for inductive loads then factually your only option is to place it in the AC line.
It still might not be the best circuit as it is not switching the motor on the zerocrossing so you may induce a lot of noise on the powerline.
The other problem that you will have is that with placing it in the AC line, you are in fact reducing the power to the trigger circuit of the FET so on occasion it may not get into full saturation and have a high RDS. In that case you basically have a resistor in series with your fan, leading to quick overheating.
In fact you would have all the problems that I described with the circuit of Giesberts
127 Volt in it self should not be a huge problem, though you may want to reduce the 100k resistor a bit
Nothing wrong with your english, no worries

love_edevice (author)2017-10-16

I used a regular incandescent.

diy_bloke (author)love_edevice2017-10-17

OK, just to make sure I tried again with the STP10NK60 and a 500 Watt lamp, works well, yes gets fairly warm after say 20 min but nothing serious.
Then I tried again with a 730, works well too, gets hotter but nothing to worry about

diy_bloke (author)love_edevice2017-10-16

then i must say i am getting at the end of my rope, did you measure if indeed you are feeding the lamp with DC?, not presume but actually measure?

love_edevice (author)2017-10-16

Thanks for your help.

I already checked my circuit several times and my breadboard circuit is same to the circuit I sent you before. It is different from just using KBU810 and SF56 instead of DB107 and 1N4007. I used the version with Zener diode and checked that the resistors of the voltage divider worked properly. The voltage divider and Zener diode provided correctly 15V to the gate of IRF730.

When put in a 500W lamp, the IRF730 is so hot that it can not be touched by hand. I also know that the IRF730 can guarantee enough current for a 500W lamp, but strangely it continues to fail.

So I bought the STP10NK60ZFP (35W) and connected it to the circuit instead of IRF730. At this time, the dimmer circuit worked properly. But likewise, the heat is great. Even though it provides 10A current.

It is really strange. Is there a problem with the 500w lamp?

I know that you tested your breadboard with 500w lamp and your circuit worked without a problem.

Could you send me the circuit you tested with 500w lamp. I can't really find any problems in my circuit.

diy_bloke (author)love_edevice2017-10-16

it is the same circuit, albeit that I have the series resistor in the anode rather than the cathode.
I tested with a 500 Watt halogen lamp (didnt have an incandescent lamp) and yes the 730 gets warm but not extremely hot.
What kind of 500 Watt lamp are you using? regular incandescent?

love_edevice (author)2017-10-13


I am very sorry to take your time often. But I have something to inform you.

I removed the 4n35 from the circuit and tested with a 500W lamp. The lamp was fully on, but IRF730 was burned immediately. At that time the gate voltage is 0V.

In the previous experiment, 4n35 was removed and the 100w lamp was connected. So the IRF730 did not fail.

I think there is another problem in my circuit except for 4n35.

I removed only 4n35 from my circuit and connected 500w lamp.

Help me again.

diy_bloke (author)love_edevice2017-10-14

dont worry, thats fine.
Sorry to hear that.
But something I do not understand. How can the gate voltage be 0Volt when you remove the 4n35, it should be at full voltage, completely opening your mosfet. At that moment it is able to deliver 4.5 amps.
I suggest you recheck your circuit

love_edevice (author)diy_bloke2017-10-14

That's problem.

Before connecting the lamp, the gate voltage is 15V.

First I connected 100W lamp, then the gate voltage is 15V.

But after connecting the 500W lamp, immediately the gate voltage is 0V and IRF730 is broken.

diy_bloke (author)love_edevice2017-10-16

OK I see you used the zener. Please check something else as well, can you measure if your diode bridge is connected correctly and that indeed you have DC on your lamp!

love_edevice (author)2017-10-14

That's problem.

Before connecting the lamp, the gate voltage is 15V.

First I connected 100W lamp, then the gate voltage is 15V. At this time lamp is fully on.

But after connecting the 500W lamp, immediately the gate voltage is 0V and IRF730 is broken.(of course I removed 4n35 from the circuit)

diy_bloke (author)love_edevice2017-10-16

ok, so just to get it staright. When you removed the optocoupler and put in a 500 Watt lamp the IRF730 burned out? Even though the gate was at 15 Volt.
Honestly I cant really wrap my head around it The IRF 730 when fully open should be able to get 5.5 amps. Even if it is hot it still can deliver 3.5 amps and in short bursts even 22 amp.

When you use a 500 Watt lamp the current is around 2 amps (when on 230 Volt). There shouldnt be a problem at all.
When fully open the RDSon is 1 ohm so it would develop 2 Watt in warmth and that should not be a problem, definitely not instantaneous.
There are several (remote) possibilities:
There is a fault in yr circuit. Not the circuit as drawn, but as you built it, but I think you already checked an double checked it.
The IRF730 has a max Gate voltage of 20 Volt, so 15 Volt is getting close but still shld be room enough. Also, then I wouldnt expect only the 500 watt to be a problem.

Please do the following: check if the resistors of your voltage divider have the proper value and are connected properly.
Also, did you use the version with a zener diode?

love_edevice (author)2017-10-12

What do you mean "Did you actually fully read my previous 2 comments?"

diy_bloke (author)love_edevice2017-10-13

I meant exactly that, as from yr replies I sometimes got impression you hadnt read something

love_edevice (author)2017-10-12

Hi, Thanks for your help.

I tested my circuit removing 4n35 and lamp was fully on.

I always read carefully your instruction. But I have something to tell you.

I connected pin 1 of 4n35 to the output of NodeMCU without the series resistor, but I connected pin 2 of 4n35 to ground through a 330 ohms resistor.

I think this resistor takes a voltage of 2V that you worry about.

I know your circuit also is configured like this.

diy_bloke (author)love_edevice2017-10-13

ah ok, so you do have a series resistor.
Anyway if the Lamp is fully on with the 4n35 removed and your IRF730 does not show any signs of burning out, the problem is around the trigger circuit.
I suggest you get a new 4n35 and then try first with an arduino, just a simple analogWrite loop.

love_edevice (author)2017-10-12

Hi diy_block,

Did you find really the problem?

If you are right, I will appreciate to you.

I will send you my code.

But I am worried that my code will confuse you because there are many features in my code that I have not mentioned to you.

Please let me know when you finish the checking about my code.

diy_bloke (author)love_edevice2017-10-12

Please read this carefully. I get the feeling sometimes you are not fully or not accurately reading my replies and I find myself repeating a lot so please read this carefully.

For one thing, you didnt make the circuit according to instructions. It would have been helpfull if you had said that in the beginning: You left out the series resistor, you shouldnt.The 4N35 has a forward voltage of 1.3 Volt, the nodeMCU has an output voltage of 3.3V (ideally). Therefore the circuit had to dump 2 volts somewhere without a series resistor to do it in.

This can have several consequences: your optocoupler desperately tries to keep the voltage at 1.3, while yr nodeMCU desperately tries to keep it at 3.3V.

The result is a large current. The GPIO pin can only deliver 12mA. When the current rises above that, the voltage may droptill zero and the logical pinmight get damaged. If the GPIO pin cannot sustain the HIGH, the optocoupler might shut down again, or worse, only open a bit, sending the TRIAC in resistive mode.

In the meantime your NodeMCU could be damagedand even yr 4N35 could be damaged (dont think so though).

So here is what I want yo to do:

Remove your 4N35.
Replace the burned IRF730

Add the 500V lamp
Plug in to the grid

Your lamp should by fully on right now. Confirm it is.

Remove power. give the capacitor a bit of time to discharge

insert the 4N35 (preferably a new one)

if you have a spare arduino, please do the following:

add a 330 ohm seriesresistor as in my circuit.

Try dimming with a 5Volt arduino.

If that works.. replace the 330 ohm series resistor with a 270 ohm (330 will probably work but wanna make sure)

try with your nodeMCU, preferably using a simple analogwrite statement in a loop.

diy_bloke (author)love_edevice2017-10-12

checking yr code right now. Did you actually fully read my previous 2 comments?

love_edevice (author)2017-10-12

Did you receive the circuit?

I send it again.

diy_bloke (author)love_edevice2017-10-12

yes, just to make sure send me your program. But I think I already may have an idea where the problem is

diy_bloke (author)love_edevice2017-10-12

yes received it but I presumed indeed it was akin to the one in the instructable

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:Working With the Funduino W5100 Ethernet Module.Uploading Data to ThingSpeak With MQTTAdding an MCP23017 I/O Extender to Arduino or ESP8266
Add instructable to: