loading

Step 5: Arduino Controlled Lightdimmer: The Software II

I found another piece of Software that allows controlling the lamp via the serial port.It triggers on the falling edge of the zero-crossing signal, so the timing is a bit different.

I have not tested it myself yet, but I see reasons why it should not work: as far as i can see it doesnt receive the number typed in the serial port but it receives the ascii value of each digit that is typed, so a '0' will be seen as 48

int AC_pin = 3;//Pin to OptoTriac
byte dim = 0; //Initial brightness level from 0 to 255, change as you like!

void setup() {
  Serial.begin(9600);
  pinMode(AC_pin, OUTPUT);
  attachInterrupt(0, light, FALLING);//When arduino Pin 2 is FALLING from HIGH to LOW, run light procedure!
}

void light() {
  if (Serial.available()) {
    dim = Serial.read();
    if (dim < 1) {
      //Turn TRIAC completely OFF if dim is 0
      digitalWrite(AC_pin, LOW);
    }

    if (dim > 254) { //Turn TRIAC completely ON if dim is 255
      digitalWrite(AC_pin, HIGH);
    }
  }

  if (dim > 0 && dim < 255) {
    //Dimming part, if dim is not 0 and not 255
    delayMicroseconds(34*(255-dim));
    digitalWrite(AC_pin, HIGH);
    delayMicroseconds(500);
    digitalWrite(AC_pin, LOW);
  }
}
void loop() {
}

Just a note: The above software is not mine. I think it is better to keep the check of the serial port out of the interrupt. Also the 500uS delay before the TRIAC is switched OFF is maybe a bit long.

Even more software here

<p>does this work with low consumption bulbs?</p>
<p>I am not sure what you mean with Low Consumption bulbs. Do you mean regular incandescent bulbs of low wattage? yes that works. Do you mean CFL lamps? no that doesnt work</p>
<p>Hello. I made a board for the circuit, although I couldn't get the TIC206 and found a replacement. In fact I've tried 2 TRIACs and have no clue why it's not working after studying the datasheets. I have some images of testing. The first is the output of the 4N25 zero-crossing detector (1V/div, x1 probe), the 2nd is the arduino output to the circuit, and the 3rd and 4th are the output (pin 4) of the MOC3021, 5v/div, x1 probe and x10 probe. Looks like a little less than 220V RMS to me, but this is feeding the gate of the TRIAC. This happened with both the MAC228A and BTA30-800 TRIACS.</p><p>The bulb briefly flashes when I touch the scope probe to the gate. What do you think I've got wrong?</p>
<p>unless the two dots in picture 2 mean anything rather than being reflections I would say that there is no output from the arduino.</p><p>Did you measure it directly on the port or maybe on the input side of the optocoupler?</p><p>please do the following:- put yr arduino aside and put 5V on the inout of the circuit.</p><p>is yr lamp on?</p><p>remove the 5v input</p><p>is yr lamp off?</p><p>if that doesnt work, check yr circuit.</p><p>if that works, please attach yr arduino and write a high to the pin connected to the circuit.</p><p>if that doesnt work, check the connection withyr arduino, make sure it is the proper pin.</p><p>let me know the outcome</p>
<p>Nupe.</p><p>Those two dots are from a test program I adapted from your code. They are from the arduino and go back and forth as I cycle from 5 to 128 and back.</p><p>I'll go through it with a multimeter tonight and check for shorts</p>
<p>And I tried a simple HIGH on the arduino with no joy</p>
<p>does that mean you wrote a HIGH to the pin and that had no effect?</p><p>in that case, write a high and measure the DC voltage on the low voltage/input pins of the optocoupler and subsequently the AC voltage over the secundary/high voltage side of the optocoupler</p>
<p>AC 239V (as expected - I am in the UK)</p><p>DC was 1.17 which I thought was a bit low, so I changed R2 from 470R to 270R. Now it's 2.21 which may be still a bit low according to the datasheet. I'll go lower still and see if it works.</p>
<p>The 239 Volts indicates your Optocoupler isnt triggered. 1.7 Volt is a bit low. What color LED did you use?</p>
<p>?</p><p>I changed it to 180R and the voltage dropped to 1.3V</p>
<p>I've checked my board layout and it's exactly the same circuit as yours. Could it be my choice of TRIAC? BAC30-800W</p>
And thnks bloke, you've been very helpful
<p>my pleasure</p>
Why do we get a humming noise when we controll ceiling fan through a triac. I have made a remote controled circuit for controlling light and fan. Used the above zerocrossing method. When the fan is on full speed there is no noise but when i reduce the fan speed the noise is prominent at low speed the fan makes noise. I guess it is due to the reason that the fan gets 220v form mains and when i reduce the speed there is less current flowing through the Triacs. I don't know the exact reason of the humming noise but im sure there is no problem with the fan :-P what can ne the possible reason behind it and what is the solution. Thanks
<p>well as I said a Triac is not ideal for an inductive load but it will work. Inductive loads are designed to use clean, smooth sine waves. They don't like it when their source of power is all chopped up. If you do use a dimmer -that chops the sine wave- with an inductive load, you will most likely notice that it (the motor or transformer) begins to hum or buzz.<br>is your ceiling fan one that already has a switch for say 3 speeds (usually a cord coming down from the fan)?</p>
And thnks bloke, you've been very helpful
I did try it with 33k 2watt and it worked! I tested it for the last 24hours..and they did not burn out they didnt even heat up a lot.. But they are geting moderately hot but I guess they are at constant temperature.. My last question is, will the constant temperature destroy the resistor over time in a period of 1-2 months? Thanks in advance for all your help sir.
<p>just an extra remark. If earlier your 2 Watt 30k burned out and now yr 2Wat 33k do well where in fact 1/2 Watt would be OK you have a problem in your circuit or messed up your resistor values. I strongly advise you to check yr circuit and do the test I suggested</p>
<p>it should not. Those 2 Watt resistors should be able to carry 366mW. Still, it is odd that you had those problems.Have you tried the tests that I mentioned earlier? especially running the circuit without a lam or arduino attached. If your resistors then suddenly dont get hot you know you have a circuit mistake somewhere</p>
Il definitely check my pcb as you said.. Do you have 110v ac or 220v ac.. At my place i have 220v. Does that matter.. How come your resistors do not get hot at all even if you use 1/2 watt. My zerocrossing circuit works very well no issues there.. Just the resistor heating problem is stopping me from manufacturing my product..
<p>I have 220 Volt<br>My resistors don't get hot because they are 33k so in total 66k thus the power is U&sup2;/R=733mW which is 366mW per resistor.<br>The fact that yours get hot is either because you dont have 33 k or because you have a mistake in yr circuit</p>
Sir thanks for your prompt reply.. But i did try 2watt 30k x2 resistors just like your circuit design but they burned out over night. Then i tried 2watt 15k x2 even they got too hot in an hour. Iv been struggling with this problem for too long.. Plz can you suggest an aleternative solution.. Thanks in advance.
<p>well as I said the 15k only increases the problem.<br>If even the 2watt 33k resistors burned out over night that means there is more than 4 watt being burned in the zerocross signal and that is definitely not right. I use I think 1/2 Watt each, have had it on for a long time and none of the problems you describe. <br>I start to think maybe there is an error in your circuit.<br>I suggest the following, please carefully check your PCB/connections.<br>if that doesnt give you anything and you still have 33 k resistors 1 Watt, please remove your Arduino, remove any load (such as a lamp) and plug the circuit ino the mains as usual. Please check if they get hot at the same rate<br>(Ofcourse only check after unplugging)</p>
Sir. I am making remote controled light and fan.. I used your circuit for the zerocrossing purpose.. It works great.. I used 2x 30k 1watt resistors we have 220v mains..it works fine but resistors heat up.. And in 4-5 hours they burn out.. What is the possible solution...how will use of 2x15k resistor affect? Sir im in an urjnt need of solution.. It will be great if you could assist me asap. Thankyou your doing a great job.
<p>the total power consumption in both resistors is a bit less than 1 Watt (u^2 / R)</p><p> so that is a bit less than a 1/2 Watt for each resistor so if each resistor is 1Watt it should be enough. Nevertheless, increase them to two watt.</p><p>15k is no use, that will be even hotter.</p>
<p>hello i've tried to make the circuit but when i plug it to my AC source, my plug burnt and my whole room filled with smoke :| . could you please tell me whats wrong ? i'm newbie in electronics. </p>
<p>Sir,Is there any instructable to controll ac fan spreed using arduino and android?I could not find.</p>
<p>The fan question must be the single most asked question i get on this instructable :-)<br>May I suggest you go through the comments and my reply.<br>In short, TRIACS or not the most optimal for inductive control but they can do it.<br>With regard to adding android:<br>what you need to do is have your arduino check the serial port for a value that controls the speed, connect a bluetooth module and then write an app that sends that number over bluetooth.<br>I have seen apps like that to regulate a light, but that is the same principle, but if you cant find an app, it is easy to write one yourself. Can I suggest 'appinventor2' to write an app.</p>
<p>hello grate work any idea how to control washing machine motor and how to use the tacho for preventing low speed on load thank you</p>
<p>TRIACs are not the most ideal component for inductive loads but they can do it. make sure that you have a TRIAC capable of the power needed and add a snubber network.<br>I am not sure what you mean with &quot;how to use the tacho for preventing low speed on load&quot;</p>
<p>I constructed the circuit, and the triac seems to be firing correctly. My light won't dim correctly thought. I put the basic dimming program you provided in step 4 on the arduino, but the light flickers rather than dimming. I'm guessing it is a timing issue. I am on a 120v 60Hz grid, and have changed the values in the program accordingly. Any suggestions?</p>
<p>yes it seems your circuit isnt doing at all what the program is supposed to do. Can you do the following for me:</p><p>keep your arduino connected but simply write a LOW to the output-what happens?</p><p>then simply write a HIGH to the output pin.-what happns then?</p><p>Then disconnect your Arduino totally, but leave your triac circuit connected to the grid-what happens then?</p>
<p>I wrote LOW to output, light stayed off.</p><p>I wrote HIGH to output, light stayed on, no flickering.</p><p>Disconnected arduino, light stayed off.</p><p>So it looks like the triac is working as expected. I wonder if it's an <br>issue with the timing on the interrupt. I have the 60Hz signal coming <br>from the mains passing through the 400v bridge rectifier, which produces<br> a 120Hz signal. This is passed to the 4n25, then to the interrupt pin. I<br> changed the values in the program from 75 to 65, and from 10 to 8.33 as<br> instructed. Oscilloscope showed a pretty clean 120Hz signal going to interrupt pin.</p>
<p>Well if you looked at it on a scope you should have been able to see the timing.</p><p>The reason for the test was to see if there might have been haphazard firing of the TRIAC, which is usually caused by a 'dirty' PSU<br>Nevertheless it proves teh TRIAC circuit is working well.<br>As you also have a clean 120 Hz signal going to the interrupt pin (did you indeed measure it on the collector of the 4n25??) I doubt the problem is there<br><br>Final question... did you also adapt the values of R4 and R5?<br>Did the scope show you the voltage of the 120 Hz signal on 4n25? and did it show you the width of the pulse?</p>
<p>I changed the 30k resistors to 15k, and have included a picture of the scope reading which shows some of the values you asked for. The width of the 120Hz signal is 8.310mS, Vpk-pk is 5.12v, Vrms is 1.47v. Still getting inconsistent dimming with basic dimming program.</p><p>One thing I tried was removing the for() statement in the void loop() of the basic dimming program, and after I did that it started dimming consistently. I will attach a video. With the value &quot;dimming&quot; not changing, I was expecting a fixed brightness level, but instead it started running through the dimming cycle.</p><p>Thanks for all your help!</p>
<p>That is odd to say the least. So you are left with<br>dimming=i;<br><br> delay(10);<br>and it is dimming?<br>Well, at least it works :-)</p>
<p>I removed the whole for() statement, so there is nothing in the void loop(), and it is dimming as before (see first screenshot).</p><p>I have tried manually setting dimming = 0 and delay(10), but I am still getting a quick fading cycle. I thought that setting dimming = 0 would leave the triac on for the whole wave cycle, giving me full brightness, but it is still going through a dimming cycle (see second screenshot).</p><p>Can you think of anything that would cause this?</p>
<p>Honestly... I have no idea. Your program is going through a loop that isnt programmed. If you disconnected the Arduino, there was no fading correct?</p>
<p>if you haven' t adapted the 30k resistors into 15 k you should do that. </p><p>If the value is too high (like 30k for 120 V is) then the zerocrossing pulse will be rather wide and start long before the real zerocrossing</p>
<p>great job, can i use in unstable frequency? i want to control my speed of syncronous generator. thx in advance.</p>
I think i can paralel 0-x ing output to INT1 and use pulseIn function to measure frequency, agree? Thx for your atenttion
<p>well i wouldnt directly parallel the two ports, but each give them a seperate signal by means of an extra optocoupler, you could also use 1 pin to measure frequency and keep track of the phase cutting timing, but software wise it might be easier to use two pins, that depends.</p><p>You can use PulseIn, thats is a convenient way but i am not sure if it is the best way as PulsIn will let the program wait, but maybe that is no problem for you</p><p>The best way might be to just use one pin for the zero interrupt, but to use a timer to measure the time between interrupts</p>
<p>my one last question, its possiblle to control 3 phase mode with 1 controller? (i use AVR atmega 32A). If yes tell me how to do it. Thx before.</p>
<p>I presume it is but I am not experienced enough with 3 phase power to give any advice on it</p>
<p>Not entirely sure what you mean with unstable frequencies. You mean on the grid?<br>Syncronous generators in my knowledge are used to generate a voltage from a movement, so I am not sure what speed you want to control. Or do you mean a syncronous motor?<br>TRIACs are not ideal to regulate inductive loads but they can. Just make sure you have a snubber network</p>
<p>yes syncronous generator as you mean, i use water turbine as prime mover. , as we know that consumer load is always changing and i want to use your project to control total generator loads in a way compensate it to dummy loads (incandescent bulb) so that frequency always stable at arround 50 hz. If frequencies more than 50.5 hz increase (dimm up) the bulb, if freq. Less than 49.5 Hz dim down bulb, else do nothing (last state dimm). Thx for your answer</p>
<p>obviously you need a bit more than just this circuit.</p><p>as i understand you want to use this to switch in aload, as ipresume to get the frequency down.</p><p>The problem however is that the period changes and therefore the timing for the triac is difficult to establish.</p><p>what u need to do is to measure the frequency andindeed based on that calculate a new dimfactor. I do not know enough about asyncronous generator to know if and to what extend a load will bring down the frequency but if yes, then yes this circuit can do it, but it might be easier to use my other circuit for that </p><p><a href="http://www.instructables.com/id/AC-PWM-Dimmer-for-Arduino/">http://www.instructables.com/id/AC-PWM-Dimmer-for-...</a></p><p>but u would still need to add some hardware to measure the frequency</p>
<p>i was read your project pwm ac dimmer, but IGBT that can handle about 8 kw load so very expensive, can i parallel some mosfet with 1 gate driver?</p>
<p>It is mi first Instructable proyect that I made!!! I am very happy!</p><p>I should changed some resistors because the 4n25 does not fire. Instead of using two resistors of 30Kohms, I used two of 15Kohms. I suspect that the bridge rectifier has its own resistance.</p><p>Thanks for all the info.</p><p>Bye!</p><p>PS: My home AC line is 220Volts 60Hertz</p><p>Este es mi primer proyecto que hago de Instructable!!! Estoy muy contento!</p><p>Tuve que hacer unos cambios en las resistencias porque no se disparaba el 4n25. En lugar de usar dos resistencias de 30KOmhs, us&eacute; dos de 15KOmhs. Sospecho que el puente rectificador que us&eacute; tiene resistencia.</p><p>Gracias por toda la informaci&oacute;n.</p><p>Saludos!</p><p>PD: En mi casa la electricidad es de 220Volts a 60 Hertz</p>
<p>Congrats and I am happy you were succesful in building it.<br>The bridge does not have its own resistance. It doesnt work that way. What does happen though with 2x15k, rather than 2x30k is that the zerocrossingssignal becomes narrower. It could have been that you needed that, but the 4n25 should have fired with the particular values.<br>Video looks great !</p>

About This Instructable

446,308views

373favorites

License:

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:AC PWM dimmer for ArduinoThe Arduino and daylight saving time (Europe)Using Reset pin / pin 1 on Attiny without bothering with Fuses
Add instructable to: