# Simplest way to build a light dimmer using PWM

I need to build a light dimmer using the PWM on the microcontroller to dim an incandescent 60W bulb. What would be the simplest way to do it ?, any suggestions ?, would a optocoupler and triac suffice ?

berkeman
Mentor
I need to build a light dimmer using the PWM on the microcontroller to dim an incandescent 60W bulb. What would be the simplest way to do it ?, any suggestions ?, would a optocoupler and triac suffice ?
You will need two optocouplers. One to sense the zero-crossings of the AC Mains signal, and one in the other direction to trigger the triac at whatever angle is appropriate for the brightness level. Can you use Google Images to find some typical circuits like this with the isolated uC controlling the dimming, and post them here for discussion?

Simplest one's I could find where these,

What would be the need for the zero crosser if I know the AC mains frequency is 50Hz, so basically I know when the zero will occur. If I am using pwm to generate a square wave, I thought about something like this

I am using an atmega16 and so if I set the PWM frequency to 100Hz and say for example 80% brightness, that would be 80% duty cycle, which I read is all you need to generate a square wave using the atmega16

With a triac - it would be more of a phase delay vs what I would call PWM. To do a true PWM - you will need something that you can turn off, like a MOSFET - - you will need two to switch both the positive and negative AC wave, or you can rectify to DC and then just make a chopper.

NascentOxygen
Staff Emeritus
What would be the need for the zero crosser if I know the AC mains frequency
If your control pulses are not synchronised to be in step with the mains waveform, then the intensity of the light will show variations with time, which we perceive as annoying flicker.

Guys, I tried building the circuit, heres the schematic:

So bascially, the bottom optocoupler (U5) is the zero cross detector which will send a pulse to pin 3 when a zero is detected and fire an interrupt. This interrupt will trigger a timer which will fire the second optocoupler(U7) when the timer has elapsed (Brightness level will determine the time delay)

Now for my question, I want to know if I connected everything fine before I start the coding, especially the triac part, I am unsure about that

berkeman
Mentor
Guys, I tried building the circuit, heres the schematic:
View attachment 106043

So bascially, the bottom optocoupler (U5) is the zero cross detector which will send a pulse to pin 3 when a zero is detected and fire an interrupt. This interrupt will trigger a timer which will fire the second optocoupler(U7) when the timer has elapsed (Brightness level will determine the time delay)

Now for my question, I want to know if I connected everything fine before I start the coding, especially the triac part, I am unsure about that
The light needs to be in series with the triac...

As I read this, the triac is in series with the bulb.

I would point out that you could build this using a stepdown transformer first, and run this around 12 vac, to test it. Of course you need a low voltage bulb.

You can then test each section out easier, and less risk of blowing something up.

Fuse that source!

I am a little curious if the rectified portion of the zero crossing signal will go completely to 0v, also in this set up the zero cross signal to the uC will be normally high, and you will turn off at the 0 ceross. Probably will...

berkeman
Mentor
As I read this, the triac is in series with the bulb.
Ah, you're right. The "ground" symbol between the triac and the light confused me. The ground symbol needs to be removed, especially if it is meant to be the same as the ground symbol shown on the SELV side of the optos.

berkeman
Mentor
The ground symbol needs to be removed, especially if it is meant to be the same as the ground symbol shown on the SELV side of the optos.
Actually, what you need to do is be sure that the opto pullup voltage and the "ground" for the triac are different symbols from the SELV versions. I see now that you want a reference voltage for the triac trigger, which is fine. I think I would reference it to Neutral as a low-side switch, though... How are you making the opto pullup voltage for the triac?

TheRedDevil18
I modified the circuit to this,

also in this set up the zero cross signal to the uC will be normally high, and you will turn off at the 0 ceross
Wouldn't the MCU be measuring VCE on the transistor ?, so if their is a zero, the transistor would be off and VCE would be 5V and if its on, VCE would be 0V

I'm having trouble choosing R6 though, in the datasheet for the triac (L4004L3), this one
http://www.littelfuse.com/~/media/f...es/documents/data sheets/e1sensitivetriac.pdf
Igt = 3mA, so if I calculated the resistor value for peak voltage and 30mA current

R = Vpeak/I = 230/30mA = About 8k

But now lets say the triac is triggered when the AC voltage is 10V
I = V/R = 10/8k = 1.25mA

But since Igt is 3mA, the triac wont turn on

If I try choosing higher currents, the resistor power increases
Right now it's sitting at
P = I^2 * R = 0.03^2 * 8k = 7.2W peak power

NascentOxygen
Staff Emeritus
Pin 3 for most of the cycle will be low, but around the zero crossings it will go high.

For resistor power dissipation, don't overlook the fact that once the triac is ON the voltage across it (and across that gate resistor) drops to just a few volts.

In practice, these circuits incorporate RC filtering to reduce false triggering caused by electrical noise, e.g., http://www.bristolwatch.com/ele/triacs2.htm

TheRedDevil18 and jim hardy
jim hardy
Gold Member
Dearly Missed
Igt = 3mA, so if I calculated the resistor value for peak voltage and 30mA current

R = Vpeak/I = 230/30mA = About 8k

But now lets say the triac is triggered when the AC voltage is 10V
I = V/R = 10/8k = 1.25mA

But since Igt is 3mA, the triac wont turn on

If I try choosing higher currents, the resistor power increases
Right now it's sitting at
P = I^2 * R = 0.03^2 * 8k = 7.2W peak power

i've seen metal film resistors used around power circuits. They'll burn open like a fuse if needed. Pick one bigger than you need so it'll have some distance between its ends to quench an arc. Maybe an inch ?

Has nobody mentioned the hazards of working with housepower ? Windadct mentioned fusing this thing which you should do..
Additionally, i'd find a GFCI outlet or make one for my workbench. I screw an outlet box to a plank, install a GFCI outlet and wire a 3 conductor extension cord to it . Good way to salvage an extension cord that's got run over by the lawnmower.
Use it only in grounded household outlets, if unsure get one of these and test your wall outlet:

Here they're around six bucks at Walmart. That one's for US Nema 5 receptacles . Surely they make one for the outlets in your country.

Sorry to sound like a worried aunt but we don't know your skill level nor do we know who else might read this thread.
Keep things safe for tiny fingers in your household, they're naturally curious.

old jim

Last edited by a moderator:
TheRedDevil18 and Tom.G
Thanks guys, I now added some fuses and changed R6 to 220ohms, I now have this circuit

Need some help with the fuse current rating, this is how I calculated it

Since the PC817 can take a maximum current through the LED of 50mA, I used a 50mA fuse.

Then I checked the triac and it says "surge capability up to 55A" http://www.littelfuse.com/products/switching-thyristors/triac/lxx04xx-qxx04xx/q4004l3.aspx
The moc3021 can take max 1.2A

So my total current(peak) = Current drawn by bulb + current drawn by moc3021
= 60W/230V + 230V/220ohm
= 0.26A + 1.05A
= 1.31A

So maybe 1.4A, 1.5A fuse ?

And what type resistors should I be using ?, thick film ones ?

And, do I need a snubber between the triac ?

jim hardy
Gold Member
Dearly Missed
And what type resistors should I be using ?, thick film ones ?
i think my one inch suggestion was way overboard. Some of our equipment used resistors that big to protect against 480 volts....

http://www.vishay.com/docs/28737/nfr25.pdf
http://www.vishay.com/docs/31031/cmffuse.pdf

The fuses you picked should do admirably.
R2 protects U5, R6 protects U6.....

Incandescent lamps draw a lot of inrush current so either pick a slow-blow or test for a faster one that reliably lights the lamp when you start it at full brightness. Typical inrush is about 10X normal.
https://en.wikipedia.org/wiki/Incandescent_light_bulb
Current and resistance

The actual resistance of the filament is temperature dependent. The cold resistance of tungsten-filament lamps is about 1/15 the hot-filament resistance when the lamp is operating. For example, a 100-watt, 120-volt lamp has a resistance of 144 ohms when lit, but the cold resistance is much lower (about 9.5 ohms).[57][107] Since incandescent lamps are resistive loads, simple phase-control TRIAC dimmers can be used to control brightness. Electrical contacts may carry a "T" rating symbol indicating that they are designed to control circuits with the high inrush current characteristic of tungsten lamps. For a 100-watt, 120-volt general-service lamp, the current stabilizes in about 0.10 seconds, and the lamp reaches 90% of its full brightness after about 0.13 seconds.[108]
i've used MDL for some high inrush loads. You'd probably be fine with a 1 amp MDL.
http://www1.cooperbussmann.com/pdf/98a9c763-9040-4280-a255-151b3dbca2f3.pdf

Here's a common fast blow fuse
http://www1.cooperbussmann.com/pdf/b040436b-cbc7-495d-95a6-99fe85202786.pdf

and a tutorial on fuseology
http://www.littelfuse.com/~/media/e.../littelfuse_fuseology_selection_guide.pdf.pdf

Have fun !

old jim

TheRedDevil18
Thanks jim

I now basically put 3 fuses (those black boxes). I read about slow blow fuses and they basically allow a high inrush current for a small period of time before blowing. Since the bulbs normal peak current is I = P/V = 60W/230 = 260mA, I put a 300mA slow blow to allow the inrush current which is about 3.64A (14*normal)

The 1A fuse is for protecting U6

The 50mA is for U5, since 50mA is the LED's max current

Not too sure, what do you think ?, is it fine ?

Not too sure if they even have fuses with those current ratings, I googled a few and they did come up though

jim hardy
Gold Member
Dearly Missed
Not too sure, what do you think ?, is it fine ?

Not too sure if they even have fuses with those current ratings, I googled a few and they did come up though

It's thorough.

Myself i'd loosen up a little
use a single 1 amp to protect both U8 and the lamp socket, rely on R6 to fuse if it needs to save U6.
Old technician's saying: "Semiconductors are great fuse protectors. "

There's no inrush to your bridge so a good fast acting fuse will work.
I'd rely on R2 to protect U5 and pick a fuse that protects BR1. He looks to be a 2 amp bridge, and you need only a fraction of that
AGC is a common fast acting fuse available in 1/16 amp . I think that'd be sensible.

I do like that you placed your triac in the high side line.
When it's placed in the neutral,
that leaves this part of the socket and lamp "hot" when off and it's an invitation to get shocked changing a lightbulb that you think is switched off.

My two cents. Overpriced at that, probably.

old jim

NascentOxygen
Staff Emeritus