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-06 Watt (actually it dissipates abt 480mW max) 0.12 euro
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


m4biz (author)2017-05-18

do you think that this should be work ?

diy_bloke (author)m4biz2017-05-19

no, if any you should switch the collector and emitter :-)

m4biz (author)diy_bloke2017-05-20


Any idea to solve my issue?

Is there any (easy) way to modify the circuit so that when the PWM's Duty Cycle is 0% the lamp will be OFF and when the Duty Cycle is 100% the lamp will be fully ON?

In other words: is there any way so that the circuit will work in "reverse" mode?

Thanks in advance

oc. (author)m4biz2017-05-22


You need a Normal Open circuit(bassically a reversed logic): I've built and described below and also added the schematic using exactly the components in this article's build.



m4biz (author)oc.2017-05-23

Hi Oc, thanks for your reply.

It seems to me that you has added two - different - Normally Open Circuit.

Is it rigth?

And if yes wich is the right (and easiest) circuit for my needs?

Thanks in advance

oc. made it! (author)m4biz2017-05-23

This one creates a default opened circuit (lamp starts switched off) as in a normal electrical circuit or as some of us need bringing the needed behavior with no need of calculations inheriting them from the original design.

The previous implies modifications and extra parts ; and its logic is not sound as seen in comments.

bcastronomer (author)oc.2017-09-15

Just to confirm, this will work without modification with 120V AC, correct?

diy_bloke (author)m4biz2017-05-21

you could put the optocoupler in the upper part of the voltage divider, but you would need to make a few recalculations

m4biz (author)diy_bloke2017-05-23

This could be work?

diy_bloke (author)m4biz2017-05-29

No it will not, at least not properly. The optocoupler should go below where the capacitor is attached.

With regard to my remark on your first proposed circuit, That was followed by SirO2 with success. Look for the circuit in his post below

SirO2 (author)m4biz2017-05-26

I've modified the circuit for my self the way shown in attached picture.

Works as an open circuit thanks the pull down resistor R4. The capacitor is 25V. The IRF740 doesn't heat up during running almost all day. The only issue I've fight last two days was bulb flickering. After a lot of digging I've changed the Arduino PWM pin from 9 to 5 and now it all works very well. The only 5 and 6 Arduino pro mini's pins have 62kHz frequency as a default. Rest of them are 32kHz and hence bulb had flickered what was very annoying.

diy_bloke (author)SirO22017-05-29

Indeed. That is exactly what I meant with my remark to M4biz. Thanks for yr feedback. Especially good to knowit does not heat up.
If you are OK with it I will add yr circuit to the instructable itself

m4biz (author)SirO22017-05-26

Has you verified wich is the temperature of the 100 kOhm resistor? Is too hot?

SirO2 (author)m4biz2017-05-26

Right, I've forget it. So I've modified the voltage divider to be 2x100k in series and 10k and 1,2k in series.

Now they are berly worm.

diy_bloke (author)m4biz2017-05-24

probably not so good. Place the optocoupler below where the capacitor is charged. You may have to adapt the resistor va;ue

PouryaMec made it! (author)2017-08-30

Hello dear

First thanks for your instructables website. I fond of this website because it has really practical projects.

On the other hand I built this circuit :

It works perfect but there is a problem which I faced with. I used 250 Hz PWM with different duty cycles but at lower duty cycles ( 10%,20%,30%,40%) the Mosfets or IGBTs got very hot and it is much problems for my project.

Also I erected this Circuit too:

However,it didn't worked properly. it works continuously with or without PWM's stimulation.

diy_bloke (author)PouryaMec2017-08-31

regarding the 1st circuit, that is not my circuit. I used it as example of a circuit that I think does not work correctly and you just confirmed that.

As with the 2nd one, i am not sure which one that is as the link gets truncated, but if it is this one

then it means that either the 4n35 is not working or not connecting properly or there is no signal coming from yr microcontroller, or yr FET is broken

So please check the connections around the optocoupler and make sure your micro provides proper input.

If that is all ok suspicion falls on your FET

PouryaMec (author)diy_bloke2017-09-01

Thanks for your responsiveness Mr.Diy. I made these two projects with either MOC3021 or PC817 as optocoupler instead of 4n35. also about FET I used IRF830 or K10A60D or FGH40N60(IGBT).

By this Materials I got same result in two projects. It means at First project I faced with the problem as I mentioned that you agree with this fault and at Second project the lamp turn on continuously.

Is it possible the fault at second project return to the materials which I used instead of FET and Optocoupler?

diy_bloke (author)PouryaMec2017-09-01

the lamp should go off when the optocoupler shorts the gate to ground.

When the lamp does not go off there are in fact two possibilities:

1- the optocoupler does NOT short the gate to ground

2-your mosfet/igbt is burned through

with regard to the MOC3021... that is an optocoupler with a DIAC rather than a transistor, so it is not suitable. The PC817 should in principle be OK.

So if you used 2 different optocouplers and 3 different transistors, that is 6 combinations and is a recipe for wrong connections, so if you checked that all in all 10 times, that is 1.5 times per combo. If you checked the MOC3021 10 times or 100 times makes no difference as it is the wrong component.

So i suggest the following. Throw yr MOC3021 in a corner, you have no need for it.

Concentrate on the 817 and the IRF830 and try again.

If you then still have the same problem, disconnect from the grid, remove the PC870 from its socket and connect a wire between pin 3 and 4 of the socket and plug in again. Is the lamp off now?

PouryaMec (author)diy_bloke2017-09-01

I got appropriate response with these components at 11 times from first circuit ( . thanks for sharing your knowledge with others.

diy_bloke (author)PouryaMec2017-09-01

ok so you are saying it works now?

PouryaMec (author)diy_bloke2017-09-02

Yes, it works like a clock without Heat losses.

diy_bloke (author)PouryaMec2017-09-01

when in my reply i say PC870, i mean the PC817 optocoupler

RonakJ10 (author)2017-08-24

Hello Sir

I like your project

I want to make this project

sir can you send me correct circuit diagram because I can not understand which one is correct and component list

diy_bloke (author)RonakJ102017-08-24

there are in fact several possibilities, I apologize if that was not clear. I suggest this circuit :

RonakJ10 (author)diy_bloke2017-08-24

sir is not your fault it's my fault because I am studied in mechanical engineer so I can't understand a circuit

thanks, sir

sir i need one more help can you give me component list

diy_bloke (author)RonakJ102017-08-27

it is ok.

the component list is in the circuit: for the diode use a 1N4001 or something,the bridge depends on the load u want to regulate

usane (author)2017-07-14

Hi! Wonderful post. I recently plan to make a PWM dimmmer. First i
thougt a phase cutting one, but if i can avoid the extra programming
then i make a PWM dimmer, and this one is great. I think i will create
the SirO2 version. One question though. You wrote 280Hz and 500Hz PWM is
work, and SirO2 wrote 32kHz is flickering, so what about the PWM
frequency? I will use a PIC micro, so i can change wide range but i
could appreciate where should i try.

diy_bloke (author)usane2017-07-15

I can only state that I didnt see any flickering with the standardarduino that uses that uses the mentioned frequencies.
the flickering is very much depending on the time it takes to open the MOSFET and that again depends on the available voltage for the gate.
I ahve constructed my circuit such that there is a fast rise and drop of the available gate voltage.
Sir O's circuit is different, you might have seen the discussion on the two antiparrallel diodes that nor I nor acolomichi understood and that SirO could not explain other than 'it works and if repalced by a wirelink it doesnot'
In general a higher frequency would probably give more trouble in Switching the FET. In that aspect it is surprising that SirO finds that 62kHz didnt cause any flickering and 32kHz did.
As he changed his circuit a few times and he himself says he does not fully understand how it works, I feel a bit hesitant to make any further comment on his 32kHz vs 62kHz experience

usane (author)diy_bloke2017-07-17

Not that one he mentioned at acolomitchi's post with 2 FET and atnipar diodes.. The one here with one FET but he wrote doesnt satisfied with it, he didnt write why. Never mind i will experiment it with a little. Although i can reverse the PWM polaritiy in PIC i want to build an active high logic version to sake of consistency.

diy_bloke (author)usane2017-07-18

I understand.
The reversed logic was something I did because I aimed for optimal timing around the Mosfet so it would switch fast and the reversed logic suited me.
If you want it reversed can use the Optocoupler in series with the 100k

EdM100 (author)2017-07-07

Thank you! This is one EXCELLENT instructable. Although it's not a light I need to control like this you have described exactly what I needed to know and more - I suspected this was the right way to achieve what I want and you have confirmed it but you have also cleared up my knowledge of *why* it's the right way to do it.

You say to hold off building it because of an alternative method - Is there any movement on that you know of?

diy_bloke (author)EdM1002017-07-09

thank you for yr kind words.

You may want to check here before you decide on what to build:

eddiegr (author)2017-06-22

Hi, amazing instructable! Do you think this circuit will be fine for a 400W floodlight? Do I need to add heatsink perhaps?


diy_bloke (author)eddiegr2017-06-23

yes, bt indeed a heatsink is always advisable

PiyushS87 (author)2017-06-10

Can i use this circuit in hho generator. Can i connect it to the alternator of motorcycle.

diy_bloke (author)PiyushS872017-06-14

it depends what you want it to do

diy_bloke (author)acolomitchi2017-06-04

Well done! Looks prett neat

dzulpanjul (author)2017-05-16

What should I modify if I want to add 2 output channels? thanks

diy_bloke (author)dzulpanjul2017-05-17

everything after the 100uF capacitor
Thus: voltage divider, mosfet, optocoupler, zener... if you use that

dzulpanjul made it! (author)diy_bloke2017-05-20

this is correct?

diy_bloke (author)dzulpanjul2017-05-22

this would be what you need

diy_bloke (author)dzulpanjul2017-05-21

your circuit is a bit unclear on where the 100uf is connected to. Your circuit makes no difference between crossing lines and connected lines. It seems needlesly complicated. if you want two channels, just duplicate the fet, the lamp, the optocoupler and the voltage divider

m4biz (author)2017-05-18


A simple question.

Is there any (easy) way to modify the circuit so that when the PWM's Duty Cycle is 0% the lamp will be OFF and when the Duty Cycle is 100% the lamp will be fully ON?

In other words: is there any way so that the circuit will work in "reverse" mode?

Thanks in advance

oc. made it! (author)2017-05-12

Following the informed comment earlier, and without adding any parts; ; simply inverting the logic would give us a Normally open circuit easy to use in any application. from 10 to 350V AC or DC (for lower voltages ; the 100K resistor must be adjusted to allow the IRF's gate to open).

Thanks again for this article and with added flexibility will be easier to enjoy the use this circuit to control lighting via pwm or as a gpio from any embedded or PIR or anything you need to make use of it as a NO electronic cheap switch.

diy_bloke (author)oc.2017-05-13

Thank you for your input :-)
have you already built this?

oc. (author)diy_bloke2017-05-13


Yes of course, the i built one button is pressed for that reason; if anyone needs a layout i can share mine; but being so many connector choices ; dimensions and wide usage; this schematic is more valuable as a concept based on what one the needs : a normally open(this one) or inverted: normally closed circuit(the initial one).

Thanks again ; and happy DiY-ing to all.

diy_bloke (author)oc.2017-05-14

great, thanks for sharing the circuit. Just was interested to know if it was a theoretical addition or an implemented one as the latter always gives people more confidence o build it.
Ah yes, i know that 'problem' my circuits usually look simpler than my eventual PCB as i tend to add jumpers/connectors etcetera. :-)

mcgrand1 (author)2017-05-14

Looking for help, I need to create something that will automatically fade in 1000w of incandescent lights over about 10-15 seconds once the circuit is energized. After fade in, lights will remain at 100%. Would appreciate any assistance on how to build something.

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: