Instructables

Arduino controlled light dimmer

Switching an AC load with an Arduino is rather simpel: either a mechanical relay or a solid state relay with an optically isolated Triac. (if you use an 8051 or PIC16F877A microcontroller, there is stuff for you too here.)

It becomes a bit more tricky if one wants to dim a mains AC lamp with an arduino: just limiting the current through e.g. a transistor is not really possible due to the large power the transistor then will need to dissipate, resulting in much heat and it is also not efficient from an energy use point of view.

One way of doing it is through phase control with a Triac: the Triac then is fully opened, but only during a part of the sinus AC wave. This is called leading edge cutting.
One could let an Arduino just open the Triac for a number of microseconds, but that has the problem that it is unpredictable during what part of the sinus wave the triac opens and therefore the dimming level is unpredictable. One needs a reference point in the sinus wave.
For that a zero crossing detector is necessary. This is a circuit that tells the Arduino (or another micro controller) when the sinus-wave goes through zero and therefore gives a defined point on that sinus wave.
Opening the Triac for a number of microseconds delay starting from the zero crossing therefore gives a predictable level of dimming.

Another way of doing this is by Pulse Skip Modulation. With PSM, one or more full cycles (sinuswaves) are transferred to the load and then one or more cycles are not. Though effective, it is not a good way to dim lights as there is a chance for flickering. Though it might be tempting, in PSM one should always allow a full sinuswave to be passed to the load, not a half sinus as in that case the load will be fed factually from DC which is not a good thing for most AC loads. The difference between leading edge cutting and PSM is mainly in the software: in both cases one will need a circuit that detects the zero crossing and that can control a triac.

Such a circuit is easy to build: The zero crossing is directly derived from the rectified mains AC lines – via an optocoupler ofcourse- and gives a signal every time the wave goes through zero. Because the sine wave first goes through double phased rectification, the zero-crossing signal is given regardless whether the sinus wave goes up through zero or down through zero. This signal then can be used to trigger an interrupt in the Arduino.

It goes without saying that there needs to be a galvanic separation between the Arduino side of things and anything connected to the mains. For those who do not understand 'galvanic separation' it means 'no metal connections' thus ---> opto-couplers. BUT, if you do not understand 'galvanic separation', maybe you should not build this.

The circuit pictured here does just that. The mains 220Volt voltage is led through two 30k resistors to a bridge rectifier that gives a double phased rectified signal to a 4N25 opto-coupler. The LED in this opto-coupler thus goes low with a frequency of 100Hz and the signal on the collector is going high with a frequency of 100Hz, in line with the sinusoid wave on the mains net. The signal of the 4N25 is fed to an interrupt pin in the Arduino (or other microprocessor). The interrupt routine feeds a signal of a specific length to one of the I/O pins. The I/O pin signal goes back to our circuit and opens the LED and a MOC3021, that triggers the Opto-Thyristor briefly. The LED in series with the MOC3021 indicates if there is any current going through the MOC3021. Mind you though that in dimming operation that light will not be very visible because it is very short lasting. Should you chose to use the triac switch for continuous use, the LED will light up clearly.

Mind you that only regular incandescent lamps are truly suitable for dimming. It will work with a halogen lamp as well, but it will shorten the life span of the halogen lamp. It will not work with any cfl lamps, unless they are specifically stated to be suited for a dimmer.

If you are interested in an AC dimmer such as this but you do not want to try building it yourself, there is a somewhat similar dimmer available at www.inmojo.com, however, that is a 110 Volt 60Hz version (but adaptable for 220 50Hz), that has been out of stock for a while. You will also find a schedule here.

NOTE! It is possible that depending on the LED that is used, the steering signal just does not cut it and you may end up with a lamp that just flickrs rather than being smoothly regulated. Replacing the LED with a wire bridge will cure that. The LED is not really necessary. increase the 220 ohm resistor to 470 then


STOP: This circuit is attached to a 110-220 Voltage. Do not build this if you are not confident about what you are doing. Unplug it before coming even close to the PCB. The cooling plate of the Triac is attached to the mains. Do not touch it while in operation. Put it in a proper enclosure/container.

WAIT: Let me just add a stronger warning here: This circuit is safe if it is built and implemented only by people who know what they are doing. If you have no clue or if you are doubting about what you do, chances are you are going to be DEAD!


Materials
Zerocrossing
4N25 €0.25 or H11AA1 or IL250, IL251, IL252, LTV814 (see text in the next step)
Resistor 10k €0.10
bridge rectifier 400 Volt €0.30
2x 30 k resistor 1/2 Watt (resistors will probably dissipate 400mW max each €0.30
1 connector €0.20
5.1 Volt zenerdiode (optional)



Lamp driver
LED (Note: you can replace the LED with a wire bridge as the LED may sometimes cause the lamp to flicker rather than to regulate smoothly)
MOC3021 If you chose another type, make sure it has NO zero-crossing detection)
Resistor 220 Ohm €0.10 (I actually used a 330 Ohm and that worked fine)
Resistor 470 Ohm-1k (I ended up using a 560 Ohm and that worked well)
TRIAC TIC206 €1.20 or BR136 €0.50
1 connector €0.20

Other
Piece of PCB 6x3cm
electric wiring

That is about €3 in parts

 
Remove these adsRemove these ads by Signing Up
1-40 of 321Next »
guttih made it!1 month ago

Thanks diy_bloke

https://www.youtube.com/watch?v=9NCnCmaYa_s

virkar1.JPG
diy_bloke (author)  guttih1 month ago

takk fyrir athugasemdir. Það lítur vel út

rathodmanoj1 month ago

Hi,

Many thanks for writing this article and explaining the functionality in simple steps yet providing all information.

I plan to use this in controlling the ceiling fan.

I plan to put the PCB in some kind of ABS plastic enclosure and fix it inside the switch board panel so that it is not visible to end user.

I have few queries.Does the TRIAC warm up and require ventilation or some kind of cooling? Is there a requirement of a Heat Sink?

Also if we do require a Heat Sink, can I connect multiple TRIACs in parallel (space constraints)?

Thanks,

Manoj.

diy_bloke (author)  rathodmanoj1 month ago

Thanks rathodmanoj
the heating up of the TRIAC is largely depend on the load it needs to drive. But if you tend to use it to regulate a fan of any considerable size I would add a heatsink
It is possible to mount various TRIAC's on one heatsink as long as you keep a few things in mind: With the TIC 206 the metal housing is connected to M2. If you therefore put several TIC206 TRIACs on one heatsink, they are all connected at M2.
That doesnt need to be a problem as long as the circuit allows for that.
In my circuit M2 is connected to the Mains, not to the load. That means that indeed, various TRIACS can be mounted on 1 plate.
However, if you use other TRIACS you have to make very sure that that is the case for them too.
Some airvents in the enclosure is a good idea.
If u use the TRIAC to drive a fan, it is best to add a snubber network as described in my article

diy_bloke (author)  diy_bloke1 month ago

Also, if you plan on mounting more than 1 Triac on 1 heatsink, make sure you dont make any mistakes in the wiring. Connect the wire from the mains to M2 only once.. they will be electrically connected throught the heatsink, or, if you have doubts on that.. connect them with a wire via the screws that mount them on the plate.

That way you avoid mixing up the 2 wires from the mains and get the best fireworks ever in yr ABS plastic case :-)

john2352 months ago

is this the same as pulse skipping modulation?

diy_bloke (author)  john2352 months ago

Let me add that PSM is in this case just a matter of software. The circuit here can be used for PSM. PSM however is not so suitable for incandescent lamps because of the inherent flickering.

Should you wish to use this circuit for PSM, then what you have to do is count the number of zerocrossings the cycle should be on and count the number of zerocrossings the cycle shld be off. So if you want to have a duty cycle of 50%, the software needs to switch on the triac at the first zerocrossing, kept open another after the 2nd zerocrossing, but switch off at the third zerocrossing, kept off at the 4th zerocrossing, until the 5th zerocrossing, when it should be switched on again.
The resulting output voltage is equal to:
Vrms x sqrt n
in which n is the duty cycle.
At a 50% duty cycle the output voltage thus is:
220 * sqrt 0.5 = 220 * 0.7 = 154 Volt
In determining the dutycycle, one should be careful to always send an even number of half cycles in order to avoid a dc component that could harm e.g. a motor.
a dutycycle of 50% therefure is done by conducting an entire cycle and stopping an entire cycle, not by conducting half a cycle and blocking the other half

Hi,

I completed my TRIAC switching circuit and am back with few more queries for you. :)

Thanks for great and crystal clear explanation about PSM. I also understood your point about TRIAC firing angles. What I am really not sure about is the efficiency about this circuit in controlling fan speed (inductive load). I have read at several place that speed control using phase control method creates some harmonics in the power line which makes the motor turn hot and similar stuff. If that is the case what would you suggest ? PSM or Phase control ? Which one should be used say in case of controlling a ceiling fan ?

Just want to know that can I use this circuit to control an inductive load like I mentioned a ceiling fan. I certainly will incorporate my FAVORITE Snubber circuits :). And to control it properly what is that I need to keep in mind. Any suggestion would be highly appreciated.

Thanks,

Abhishek.

diy_bloke (author)  egrabhishek2 months ago

I am not entirely shure abt the efficacy but I guess both methods may not differ that much in efficacy. I would guess though that the phase control would make for a better torque at low speeds.
You can use this circuit for a ceiling fan. I am surprised you havent tried it yet :-).
PSM or Pulse Skip Modulation, is sometimes also called cycle skip modulation. it does produce less rfi than phase cutting.
I am not sure if it has more harmonic interference.
You can always give bot methods a try and see what works best. The circuit can handle both as it is all in the software

Hi,

I have a small idea. Just need your thoughts on the same.

I understand the importance of ZC detector and its use. However, I am a bit worried about putting 30K - 1/2 watt resistor with direct AC. I understand it will work and its already tested. However I have a small idea.

I am anyways using a 6v/12v AC step down transformer. I intend to take my A.C signal from that and fed it to 4n25/4n35 OC. Since transformer is an inductive load there will be issues with phase shifting and due to this the zero crossing will also shift a bit. It might happen earlier or a bit later which will result in appropriate trigerring.

What I plan to do is to get the first zero cross detection. See the deviation on my scope and then accordingly adjust the delay for first time and then create 20 ms delays from there.

To tune my circuit, assume my first time the zero crossing happens 2 ms after the actual time due to the presence of the transformer.

Now I will see this on scope and then adjust the next zero crossing event to happen after 18 ms and then subsequent will happen after every 20 ms (1/50 = 20 ms ). I wont be taking the zero crossing event from OC anymore it will happen just for the first time to get a hold of it.

Let me know your thoughts on it.

The idea is to make firing more accurate and use a bit more of isolation.

Regards,

Abhishek.

diy_bloke (author)  egrabhishek2 months ago

Abishek, as a matter of fact if you scroll through the replies to
someone called 'Peter'(about a year ago), you will see that I already
gave that as a possibility :-)
I actually have tried it myself and
didn’t get that many problems with a phase shift, but I didn't put a
scope to it. I am no expert on AC but I had the impression there is no phase shift between a HV and LV side of a transformer, depending of course how you would connect the secundary side, meaning that there is either a 0 or a 180 degree shift.
Would
be interesting to see your results though.
I understand from your question that you want to sync only once with the zerocrossing signal and afterwards just use timing.
I would advise against that: the mains frequency may show slight variations and you would need to keep track of a lot of time.There is a problem though:
If there indeed is a phase delay (and I dont think so) then you would notice the zerocrossing a few micro or miliseconds late, i.e. AFTER the actual zerocrossing.
If you are on 50Hz you only have 10mSec to do all you need to do with the TRIAC. Obviously, if you would have a say 2 mSec delay in your phasing, you would not be able to ignite the TRIAC at the ZC anymore and thus would be unable to regulate to full power.
If you would say: "Yes, I know I am 2mS behind, I know that my next REAL ZC is actually 8mS AFTER I measured mine, So from that point on I will just use a timer to predict or guess my REAL ZC", you will run into some potential problems: There might be small variations in the grid frequency, that add up, and you would have to keep track of a lot of timing.

Again, I dont think there is a phase shift, but if there is I would advise to still have the measured ZC trigger a say 8mS timer (if the delay indeed would be 2mS) every cycle.

You talk about 20mS delays but remember that if you use a double phase rectification you will have a 100Hz signal=10mS

Still, I think there won't be a delay, but would be interested in your results

voeding2.JPG
diy_bloke (author)  diy_bloke2 months ago

you also may want to read this article:

http://opensourcemeter.com/CTtheory.html

Hi thanks for your input.

I tried detecting zero cross with a 12 vac transformer.

In the attached images,

Red signal = ZC via transformer with the same circuit as in this post. (Slight change here. Instead of 30K resistor as you have suggested, I am using 33K + 33K). This gives the higher time period signal (2nd image).

First image with red signal with comparatively lower time period is after I removed one 33k resitor so now there exists only one of it.

Yellow Signal = ZC via circuit suggested in this post.

I m not able to make any sense out of this. Also why does the ZC singal time period reduced after removing one 33k resistor ?

Kindly let me your thoughts .

I am not sure whether ZC is happening before or after actual zero crossing.

Please suggest.

Red_12_33K_0K.jpgRed_12_33K_33K.jpg
diy_bloke (author)  egrabhishek2 months ago

Those are very interesting pics Abishek.
regarding the first picture, I take it yr base is 25mSec? I presume that you have no capacitors on that secundary side of the transformer, just simply a bridge and an optocoupler.
The slight shift in time probably has all to do with the quality of your transformer. I am no expert on this, but have been reading up yesterday and this was one of the things that was suggested. Actually, if you look at the picture, there isn’t much difference between the start of the pulses, it is just that the secundary side has a wider peak that comes a bit later.
If your interrupt is set to be at the rising edge, this might well be possible to use.

regarding your second pic, that is actually not so surprising when you think of it. Now just to make sure I understand you correct: The 2nd pic is with 2x 33k and the first one is with 1x33k right?
That means that in the first picture the optocoupler will get all in all more current throughout the cycle and therefore is in saturation most of the time. Only when the wave comes very close to zero, the current wont be big enough anymore to open the transistor and the voltage on the collector will quickly rise till it peaks and quickly drop again when the wave is through its zero crossing because it soon will give enough voltage again to send the optocoupler in saturation, thanks to the smaller resistance.
In the 2nd picture where the resistance is much higher, relatively far before the actual zerocrossing the wave voltage is already low enough to prevent the optocoupler remain in saturation (coz of the higher resistance, and once it is through the zerocrossing, the voltage needs to rise higher (= further in the cycle) in order to saturate the optocoupler again.
What it teaches me is that maybe I should lower the resistance in my circuit.

It could well be that by tinkering a bit with the resistors on yr secundary side, you could move the rising flank a bit earlier.

Very very interesting graphs. I am very happy with them. Thanks

Hi Buddy,

Thanks for your wonderful feedback. You are absolutely right in all your understanding.

My base is 2.5 mSec (As you can see the ZC is ocurring after every 4 blocks / 10 mSec).

There is no smoothing capacitor and the one with smaller time period is 1 X 33k and the bigger time period is 2 X 33k.

One more thing, I do not think you need to decrease the resistance in your circuit. I decreased because I was working with 12VAC.

Also tell me one more thing. My resistances get a bit hot. Should I increase them to a bit more value say 100 K (1/2 watt). But I think that will make them dissipate more energy as heat. What do you say ?

Also I can make use of these ZC that I am getting but to make it more accurate I need to compare it against actual signal and see how much deviation is there from the waveform. I am not feeling confident although I know it will work. What do you say ?

I have a scope : OWON PDS5022T.

Here's the link to the manual:

http://akizukidenshi.com/download/ds/owon/PDS%20Se...

Sorry for bothering you much but in case you have some time to spare, please let me know if I can see the AC waveform on it. Although I do not think so.

What I want to achieve is see the actual waveform and zero crossing and the ZC event that I am getting and sync them together.

I feel after adding some heavy resistances (100k x 4) I can add my probe to the stepped down AC voltage and that should give me the actual voltage waveform.

Please know your thoughts. I intend to test it now.

And I am based at India. (Frequency: 50Hz / 240 VAC)

diy_bloke (author)  egrabhishek2 months ago

Don't worry Anishek, you are not bothering me. Somehow I already figured you were somewhere in India :-)
You can increase the resistors and they will not dissipate more heat, but less. After all P=I²xR but if R goes up, I goes down.
In order to synchronise you need one channel of yr scope on the 240 and one channel on yr secondary site. However, you can only do that if you have completely seperated channels, so not with a common earth and I am not sure if yr scope has that. But ofcourse you can always measure the waveform if you add resistors, maybe even without as yr maximum input voltage is 300 Volt, but I am sure there is a range you need to set with a knob or something. Take into account though that yr iinput impedance is 1 Mohm so even with 1MOhm resistors you will only have halved the signal

Thanks for your time buddy.

Well I do not want to mess up my new scope with A.C.:-).There is so much of complexity involved I feel.

However, to verify the zero crossing detector efficiency I am going to compare the ZC spikes against the actual zero cross point in time, but not with A.C voltage. Rather than using A.C, I will rectify it and make it DC certainly with no smoothing capacitor. I thing the zero crossing point in A.C will certainly be in phase with the zero touching point of D.C. If this works then I will try to see how much is the deviation in case of ZC circuit with transformer.

Let me know your thoughts.

diy_bloke (author)  egrabhishek2 months ago

That seems the best solution. The ZC is just as visible in a rectified (but not smoothed) wave.
I suggest you experiment a bit with the resistor I presume you are using to feed yr OC from the secundary side

Hi.

I will try to see if the ZC points matches the actual DC zero touching points I am not clear about what should I try with the resistors value ?

Currently I am getting around < 50 volts as rectified DC. I think I can see that wave form directly on the scope. Any comments ?

Also I will try this first your circuit with 220 v as input and rectified DC as output and then with 12 v AC as input.

diy_bloke (author)  egrabhishek2 months ago

I meant that it is best to choose the resistance such that u have a narrow peak

Hi,

I compared the rectified dc zero touching points and the circuit's ZC detection signals against time. The one without transformer is quite accurate as compared to the one with the transformer. I think its better to stay away from the transformer for now. However, I have 2 small queries here:

1. With transformer why does zero crossing occur before actual zero crossing.

2. I am using 2 watt resistor instead of 1/2 watt since I have to run the system 24 X 7. Even they get a bit hot. Should I try increasing the value a bit to reduce power dissipation but in that case the ZC curve will get a bit extended. What do you suggest ?

-Abhishek.

1_No_Trans.jpg2_No_Trans.jpg3_No_Trans.jpg4_No_Trans.jpg5_Trans.jpg6_Trans.jpg
diy_bloke (author)  egrabhishek2 months ago

Abishek, before i can answer yr first question i would need to get a bit more info on yr graphs because I am not entirely sure what I see there/

with regard to yr second question, I would keep the resistance as it is. are you now using 2x33k? then 2 watt should be definitely enough.
If power consumption is a problem, there are some zerocrossing detectors that supposedly use minimal power

Yeah those are 2 X 33 K resistors , 2 watt each.

Can you give me an idea how much power will get wasted if I got your way with 2 x 33K resistors ?

Sorry for not providing enuf for graph

The yellow waveform is rectifier output and the red one is ZC out.

The last 2 pics are with transformer in place and there u see some lag. Others are without transformer.

diy_bloke (author)  egrabhishek2 months ago

well then is seems that when using the transformer your resistors are too high in value, so the optocoupler is already closing at a low voltage.
The dissipated power with 240V 2x33k will be U²/R=240*240/66.000=870mW. There are ways of getting this down, e.g. by using a more sensitive optocoupler and increase your resistors. You may want to experiment with the 6N138

diy_bloke (author)  egrabhishek2 months ago

well then is seems that when using the transformer your resistors are too high in value, so the optocoupler is already closing at a low voltage.
The dissipated power with 240V 2x33k will be U²/R=240*240/66.000=870mW. There are ways of getting this down, e.g. by using a more sensitive optocoupler and increase your resistors. You may want to experiment with the 6N138

Yeah. 20 ms was a typo. It should be corrected. Thanks for pointing that out.

I will see what I get on the Scope. Will post my results soon.

diy_bloke (author)  john2352 months ago

no, Not really. In PSM aTriac will connect a number of cycles but disconnect another number of cycles. What happens here however is phase control in each cycle

john235 diy_bloke2 months ago

Thanks for the explanation .

What I'm trying to do right now is right in the picture below

I want the lamp to act light a camera flash

can you help me regarding the code for arduino & if possible

can you also show me on how to do it using mikroC

10322687_10202740587730323_7482639038477746324_n.jpg
diy_bloke (author)  john2352 months ago

I am not sure what acting like a camera flash relates to the picture below.
With a camera flash there is a short burst of high intensity. This circuit is really not suitable for that. The maximum output is if the Triac is open during the entire cycle, so you would need to switch between 0 and 128 to basvally make the lamp go on and off again

john235 diy_bloke2 months ago

This is the output waveform of camera flash. How to code so that I can achieve the output waveform like this?

Untitled.png
diy_bloke (author)  john2352 months ago

I really think you are going the wrong way here, and you wouldn't even need this circuit for what you want to do, but in code you need to fully open the lamp (dimming=0) and then shut it off (dimming =128)

john235 diy_bloke2 months ago

What should i change on the code?..sorry for asking because i'm still new in electronic

int AC_LOAD = 3; // Output to Opto Triac pin

int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup()

{

pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output

attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above

}

//the interrupt function must take no parameters and return nothing

void zero_crosss_int() //function to be fired at the zero crossing to dim the light

{

// Firing angle calculation : 1 full 50Hz wave =1/50=20ms

// Every zerocrossing thus: (50Hz)-> 10ms (1/2 Cycle)

// For 60Hz => 8.33ms (10.000/120)

// 10ms=10000us

// (10000us - 10us) / 128 = 75 (Approx) For 60Hz =>65

int dimtime = (75*dimming); // For 60Hz =>65

delayMicroseconds(dimtime); // Wait till firing the TRIAC

digitalWrite(AC_LOAD, HIGH); // Fire the TRIAC

delayMicroseconds(10); // triac On propogation delay (for 60Hz use 8.33)

digitalWrite(AC_LOAD, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC

}

void loop() {

for (int i=5; i <= 128; i++){

dimming=i;

delay(10);

}

}

diy_bloke (author)  john2352 months ago

Mattard, I am not sure how much clearer i can tell you: you have to switch between on and off which is dimming 0 and 128

change the void loop() into
void loop() {
dimming =0;

delay(5);

dimming=128;

delay(500);

}
But you really do not need a circuit like this for it. You dont even need the zero cross detection because all you do is switch a lamp on and off

diy_bloke (author)  diy_bloke2 months ago

John, I hope I have not sounded brief in my reply. If so that was not my intention. Please feel free to ask as much as you want

john235 diy_bloke2 months ago

Thanks mate. But I want to adjust the input like i don't want it to rise in full power but start it in low then sudden rise in full then dim it slowly . Anyway thanks for your help. Really appreciate it

diy_bloke (author)  john2352 months ago

In that case, decrease 'dimming' from 128 till desired level, then bring it to 0 and then slowly increase up again.

for (int i=128; i >= 75; i--){

dimming=i;

delay(10);

}

dimming=0;

for (int i=5; i <= 128; i++){

dimming=i;

delay(10);

}

john235 diy_bloke2 months ago

The high intensity i will make it by connecting the lamp parallel with another 9 lamps. But i don't have the idea on how to code it so that it will act like a camera flash. It does not necessary to act 100% like camera flash but as long as it is almost like one. thanks

john2352 months ago

I already tried this code
& the lamp is continuously on
what are you suggesting to make it function like the one when using arduino?

diy_bloke (author)  john2352 months ago

John, Ias I have no PIC system and no expert on PIC's, I gave the code as a start but I have no way of testing it.
The code with the example 'dimming=20' just sends ONE value to the lamp and that is an 'almost fully on' value so it is understandable if yr lamp is on.

Try the following: Change 'int dimming=20; ' into 'int dimming=120;'
Is your lamp almost off then? then everything is functioning OK.

What you need to do is to get a value from whatever program you are writing and using that for the value 'dimming'

As I do not know what it is that you want to do, I can only point you in a general direction: Suppose you want a variable resistor to determine the level, then you read the value off yr variable resistor through an analogue port, map that value to 128 and use that as 'dimming'

if you just want to have your light level go up and down then make a for next loop in which you vary the value of 'dimming'

john235 diy_bloke2 months ago
(removed by author or community request)
1-40 of 321Next »