AC PWM Dimmer for Arduino

126,052

177

400

Introduction: AC PWM Dimmer for Arduino

About: 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 like. Other than my hobbies that involves grassroots medicine in S.E.&P Asia. I have buil…

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.

Gieberts

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.

ac-pwm

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

igbt-mosfet2

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:

mosfet-pwm

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

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

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

10 People Made This Project!

Recommendations

  • Potato Speed Challenge

    Potato Speed Challenge
  • Mason Jar Speed Challenge

    Mason Jar Speed Challenge
  • Bikes Challenge

    Bikes Challenge

400 Discussions

0
wolfkeeper
wolfkeeper

13 days ago

Looks good! I was thinking about using this circuit but uprating it to make an immersion heater controller for use with solar panels. I figure that together with a house import/export sensor, dimming a standard immersion heater down to 500 watts through a smoothing inductor should be about right for that application. I'll try to do an instructable if I get it to work.

0
diy_bloke
diy_bloke

Reply 13 days ago

Looking forward to that instructable. Was just wondering though that if you use solarenergy you do not need the AC part.
A FET with some PWM would suffice ( but ofcourse a large part of my circuit is just that)

0
BerryZ
BerryZ

1 year ago

1st off all I love this post, I made your old design once. but I think this is much better no more interupt pins ;-)

So I try to make the circuit 2 channels to drive 220v Cooling fans for a reef tank.
I'm bit new so I was wandering if someone could have a look if This will work.
most of the parts I used double because I'm not sure if 1 Cap and the 15v zenner and diode would work. Also I added 2 Relay switches just to take sure power is off when not needed.

Cheers

Screen Shot 2019-05-23 at 10.23.33 AM.png
0
diy_bloke
diy_bloke

Reply 13 days ago

Sorry for my late reply. Thank you for your kind words
Your circuit is a bit small on my phone so i can't say much about it but i hope you have the answer already

0
diy_bloke
diy_bloke

Reply 1 year ago

Thank you for your kind words. Your circuit is a bit hard to read, but it should be OK if you just copy the circuit behind D1

0
hassanfire14
hassanfire14

7 weeks ago

How can i alter the design so that it may work with LIGHT bulb and FAN

0
wolfkeeper
wolfkeeper

Reply 13 days ago

Fans are inductive, so you'd need to add a flyback diode across the load to the power rail or something like that otherwise you'll overvolt the semiconductor from the kickback during switching transients.

0
deva_rishi
deva_rishi

Question 4 months ago

I am having an issue with the 100uF (450v i have) Capacitor, maybe it is just broken, but then how did i break it ? I am gonna try another one, but it seems that the current it draws to first fill up exceeds the 1.0 A of the 1n4007 (i am also using 4x1n4007 as a rectifier, and half of that blew out earlier as well, i was doubting that they were actual 1n4007's but my magnifying glass confirms they are.) I am thinking it may be a good idea to add an extra current limiting resistor between the 100K resistor and the diode. I am using a p10nk60zfp mosfet, and really i am just intending to control filament led bulbs with it. I first went for the zero-cross dimmer first, got that to work, but adding more channels started to cause to much noise on the main input, and the filament leds should work fine if i go for a 100hz PWM signal. But first of course i want it to work with the Tap fully open.

0
deva_rishi
deva_rishi

Answer 3 months ago

Ok so now i made the 15v power supply inclusive. i built a small capacitor based power supply (technically speaking the diode -100uF -100K - 15.1v zener is one but expressed my objection) I decided to go for a symetrical design on the AC - side, and since i didn't have any 1uF capacitors that are rated for 400v, but did have a couple of 100nF - 600v ones i decided to go for those.Those power supply circuits claim they will provide about 30-50mA, and i figured i hardly need any. The 2 resistor across the capacitors are there to drain the energy out of the capacitors when you unplug the power (or you may get a jolt from it when you touch the terminals) and in the original design they were 1M already, i could up them to 10M and still have the same drain time, but i didn't have any. So left them as they are. then i tested it. No ripple on the on the 15v rail, so i guess 47uF (25v i have ) is enough. And when i put an LED and a resistor, i got to a 3mA maximum which i had at about 1K, I used the 15.1v zener from the other project, i don't know what the rating should be, but i recon 500mW should be ok, though when i walked into the corner shop they only had 1.3W, and that's what's in it. When i measured the voltage between the GND of the power supply and the GND of the High-Power DC rail it showed 75v, (DC) and then i was happy i went for the symetrical design, but just i case i decided put a fuse holder and a fuse in between first (200mA) just in case. Then i connected it all and all is good. I am keeping the fuse on the AC line, but i clearly don't need the one on the GND line. I still have the power supply on the breadboard, and i don't like high power on a breadboard, the leads can come loose, there is a lot exposed and a momentary lapse of focus and you put your fingers on something before you unplug. Also the breadboard can have bad connections and particularly with AC a bad connection can cause sparks and crackle. fix that later. I've added the schematics, i left the High-Power Rail as it is, and really want to thank you for this learning experience, i think i finally have this capacitor- based- power supply thing down,

Capacitor-based power-supply..JPGMosfet-driver.JPG
0
diy_bloke
diy_bloke

Answer 3 months ago

Sorry you are having problems.
Let me take you through the numbers:
If we go by 220 volt after half wave rectifying then 220/100.000 = 2.2mA. That is hardly a current that is a problem for an 1N4007 so 'adding a current limiting resistor' is useless and will cause the circuit not to function.
If your diode blows up there are really only a few possibilities:
Faulty wiring
Wrong diode
Wrong resistor
I suggest you check those

0
deva_rishi
deva_rishi

Reply 3 months ago

I double checked those, the diodes i have a now confirmed 1N4007, the Resistor is a 1w 100K , and the wiring is correct. I checked the rectifier bridge using an oscilloscope (i have a small one that i can power with a battery pack, and a probe adapter that is not much more than Wheat-bridge to reduce the voltage that i am probing) I don't dispute that the current running through R1 is about 2.2mA, without C1 in the circuit. The Issue occurs when i add C1 (i have a big-ass 100uF 450v Electrolitic Cap) into the circuit.The charging up off C1 draws way more current than the 1A that the 1N4007's are rated for, and that blows the 16A fuse in my house. and damaged the first rectifier. (it may be relevant that the fusebox is just next to my workspace) Then i added a 1A glass fuse before the rectifier, which also went repeatedly. (but now my rectifier bridge survived) To confirm that the Capacitor was nit in some way faulty (well first i just grabbed another one, i have 20 of them) i charged it up slowly using a few extra resistors (see image) and i actually got to a reasonable voltage to drive the mosfet, but some measurements revealed that the voltage in the Capacitor actually ran up to 380v RMS (according to my multimeter, which may be a tad off, but not a lot, and i may have removed the drain (30K) resistor when i measured, can't quite remember) while the voltage at the terminals was still 207v RMS, and of the top of my head the voltage between the 200k & 30K resistors was about 14v (edit: no actually i was about 20.5v, then i removed 1 of the 10K resistors and i came to 13.8v)Then i decided that i didn't really want stored energy in my circuit of that magnitude.
I tried using a 1uF capacitor, that i had still, between the terminals just to take the ripple out, and that was quite satisfactory, but it was rated only up to 250v (and really 350v should be the minimum) and just the spark that creates when i short it out to discharge is quite impressive.Hence my decision. I've been looking at circuits like https://www.google.com/search?q=capacitor+based+power+supply&sxsrf=ALeKk02O_bU3PSuyizNVGbYPf-GicfYySw:1591195311498&tbm=isch&source=iu&ictx=1&fir=w8oLjTR7n3NiTM%253A%252CDrdxFln4S8lskM%252C_&vet=1&usg=AI4_-kRJVNiCP8r28thZMTWtH0Psi1EuJw&sa=X&ved=2ahUKEwi5jr268OXpAhVQzqYKHV4pDA8Q9QEwAHoECAUQIQ#imgrc=w8oLjTR7n3NiTM: these, since only very small current is required to flick the gate (actually no current at all, just voltage, but during the grounding out, current will flow) but i think it probably requires modification to prevent current from flowing back into the power supply..

Mosfet-driver.JPG
0
deva_rishi
deva_rishi

Answer 3 months ago

So i got it to work, but using a 12v power supply which actually provides 16.3v when measured, instead of the 100uF cap and the 100K and the zener diode.
I used a wheat bridge of (+) 10K - 100K (GND) and tapped the opto-coupler and the gate of the mosfet halfway.
Now i am actually wondering if it anyway wouldn't be better to use a small polypropyleen-capacitor based power supply circuit (in a way that's what mine is though the 0.9A that it can put out is serious overkill) , since it would only need to provide a few mA's. price wise it probably doesn't make much difference, but it will be quite a lot safer. Not only does the 100uF capacitor blow the fuses in my house but having something with that much voltage (and power) on a breadboard seems a bit unwise. Still quite a few people got it to work, and there is a discharging circuit for the capacitor, mind you if you feel the discharge (and see the spark) of a 1uF capacitor and imagine that 100uF is 100x times that, well.. Not to mention that the voltage in the capacitor can run up to 350v DC rms.

0
deva_rishi
deva_rishi

Answer 3 months ago

Anyway after some experimenting, re-building the Rectifier-bridge, adding a fuse holder and putting a 1A fuse on the AC line (i was running out of 16A fuses for my house) I seriously doubt that anyone could use an actual 100uF Capacitor without chocking the power into it. (or mine are even bigger and just say 100uF on them. But with the use of a few more resistors i did manage to get about 15v (Haven't put the zener yet or powered the mosfet) but since i want to make more channels, and i don't really see a way how to drive several mosfets from the same capacitor without them influencing each other. I am now planning to just use a 16v power supply i have. Simpler, safer and smaller.

0
Dguerard
Dguerard

5 months ago

Hi!
I had just a part of the circuit and already had a problem. I had the bridge, the light (placed in the ac part just for test) and the control element (I tried TIP150 at first and then fqp19n20) and just by turning on and off the AC source a cpuple time, the protection diode in the control element blows. Does anyone know why?

Thanks

0
diy_bloke
diy_bloke

Reply 4 months ago

Sorry to hear that. I am a bit confused though by your question. What 'part' was it that you built? When you say 'protection diode' do you mean the zener?
Also a TIP150 is a darlington BJT a d is totally unsuitable for this project

0
Dguerard
Dguerard

Reply 4 months ago

Thanks for your reply! Yes I am in a 110volt country. What I had was the AC source, diode bridge, the light and the mosfet. Nothing was pluggued to the gate pin. The protection diode I talked about is in the mosfet, between Source pin and Drain pin.

0
diy_bloke
diy_bloke

Reply 4 months ago

Furthermore the fqp19n20 is a 200volt mosfet, so unless you are in a 110volt country that might not be a suitable FET

0
shmelev723
shmelev723

5 months ago

After second try and soldering all together, it works! All electric parts were taken from old PC power supply, then i go to parts store and buy 1W 100k resistor.
Related to topic question: any way to reduce PWM noise in heating element? Filter capacitor can help, or i need LC?
Anyway, thank you! Works better than dimmer with zero detection, 10A mosfet stay cold.

0
108m05003
108m05003

6 months ago

Hello, I would like to ask why the light does not change when I use PIC to let it specify the PWM output

0
MarkvdLee
MarkvdLee

Question 1 year ago

Awesome circuit! Build and tested it, and it works perfectly! Love it.

Now I want to take it a step further. I want to build the PWM dimmer into an old 'plug-in' light switch, to make a smart dimmer! Unfortunately, the neutral in and outputs are linked already thus I cannot use a DC dimmer (in the schematic the available wires are shown).

Now my plan is to make a PWM dimmer that produces a AC signal as output using the schematic below (using two mosfets back-to-back), but for some reason I get a short at IN1_F and IN2. Can you spot the mistake?

Schermafbeelding 2019-09-14 om 20.31.19.png