Introduction: 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

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

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


usane made it!(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 made it!(author)2017-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 made it!(author)2017-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 made it!(author)2017-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 made it!(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 made it!(author)2017-07-09

thank you for yr kind words.

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

eddiegr made it!(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 made it!(author)2017-06-23

yes, bt indeed a heatsink is always advisable

PiyushS87 made it!(author)2017-06-10

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

diy_bloke made it!(author)2017-06-14

it depends what you want it to do

acolomitchi made it!(author)2017-06-02
diy_bloke made it!(author)2017-06-04

Well done! Looks prett neat

m4biz made it!(author)2017-05-18

do you think that this should be work ?

diy_bloke made it!(author)2017-05-19

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

m4biz made it!(author)2017-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

diy_bloke made it!(author)2017-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 made it!(author)2017-05-23

This could be work?

diy_bloke made it!(author)2017-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 made it!(author)2017-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 made it!(author)2017-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 made it!(author)2017-05-26

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

SirO2 made it!(author)2017-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 made it!(author)2017-05-24

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

oc. made it!(author)2017-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 made it!(author)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)2017-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.

dzulpanjul made it!(author)2017-05-16

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

diy_bloke made it!(author)2017-05-17

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

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

this is correct?

pwm 4n35.png
diy_bloke made it!(author)2017-05-22

this would be what you need

diy_bloke made it!(author)2017-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 made it!(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 made it!(author)2017-05-13

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

oc. made it!(author)2017-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 made it!(author)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 made it!(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.

diy_bloke made it!(author)2017-05-14

you can do that with this circuit or with the one in my other dimmer instructable that works with a triac.
What you want you can do in hardware, but that is very complicated, or you can do it in software with say a promini that will cost you 1.60 USD

m4biz made it!(author)2017-05-11


I've successfully realized the AC PWM Dimmer.

It work like a charm with Simulink (Matlab).

Many thanks

diy_bloke made it!(author)2017-05-13

Thank you for adding the pictures

diy_bloke made it!(author)2017-05-11

My pleasure. Thanks for sharing. Looks great

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


If anyone ... and i will guess most... have the need of a normally open circuit based upon this schematic; that is capable of being driven by either a pir or a arduino pwm or simply by a low current positive 3.3-5V coming from a door switch or something; then .. there ya go ... needless to say capable of 10-350V input AC or DC , and load either led or noninductive load as kindly indicated by the guest.

diy_bloke made it!(author)2017-05-06

Are you sure your circuit is correct? It seems like your BSS123 will never get anygate voltage

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


I see your point and is very valid.
The circuit is working in this configuration with no visible stability issues.
I had a 10k resistor to the positive rail upon designing , to achieve just that: keep the gate suspended; but i guess foolishly removed since it made no visual difference in the stability of the circuit.

To make things short a 10 K to the positive rail or bss's drain was my original approach.

AmirA36 made it!(author)2017-05-05

What is the model of bridge rectifier that you used?

AmirA36 made it!(author)2017-05-06

thank you sir

m4biz made it!(author)2017-05-01

HI. I'm an Italian High School Teacher.

I'm attempting to use arduino to teach ON/Off and PID controllers.

I've no issue on ON/Off controller : a simple relay and a bulb lamp 60W are enough to explain this systems to my students in a very efficient way (I've succesfully followed this:

The big issue is when I try to prepare a lesson on PID controllers.

My idea is that I can dim the brightenss of the same AC bulb lamp (in Italy there are 230V/50 Hz) so that - consequently - will vary it superficial temperature .

At this point, by means the same temperature sensor - I'll can adjust the superficial bulb temperature by varying its brightenss - and minimize the error by means PID controller.

And - to do this - I thing that I should use a PWM control ,

The question are:

1) in your opinion is this the right way to better teach PID Controller?

2) is your circuit suitable and good for my needs

3) because strictly times requirements, may I find anything of already ready on the web? or , in alternative, may you build it for me?

I hope I was clear in my needs ad request.

Thanks in advance.

diy_bloke made it!(author)2017-05-02

PWM indeed seems the most suitable for PID. You are aware of the Arduino PIDlibrary I presume? (if an arduino is what you plan to use)

Whether my circuit is suitable ofcourse I cannot garantee but if you want to keep things simple, yes I think it is useable. Ofcourse you do still need a temperature sensor.

Thereare various pid controllers on the market, such as e.g. a sous vide, but I am not aware of any commercial product for the arduino or other microcontroller.

Technically I could build it for you but I am afraid that would become too expensive for what is in fact a fairly simple system that you could easily put together yourself.

Yes, you were very clear. Thank you for your interest and goodluck :-)

m4biz made it!(author)2017-05-04

Hi diy_bloke, thanks for your reply.

Reeding your post, it seems to me that the PCB you have posted, is not congruent with circuits showed.

Is it correct?

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: