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>Hello Bloke,</p><p>Excelent post, thanks a lot for share your knowledge.</p><p>I want to download the PCB print file but the file seems no longer exist.</p><p>Pls. kindly check the downwload link or could you send it to my email genhitsuu@yahoo.com</p><p>Thank you very much</p>
<p>I get the impression my entire dropbox is gone. Try here </p><p><a href="https://drive.google.com/open?id=0B6c8mGbetkOUQlpyM0swRmowZEE">https://drive.google.com/open?id=0B6c8mGbetkOUQlpy...</a></p>
<p>I got it. Thank you Bloke!</p>
<p>Hey Bloke</p><p>Thank u for all this information is really help full.</p><p>BUT i want to control the fan speed.I used this circuit &amp; program &amp; its making humming sound. So is there any option to reduce that or any other circuit for that?</p><p>I already tried snubber circuit but no use of that.So please help me with this. </p><p>Thank You</p>
regulating a fan is one of the most frequent questions I get asked here.<br>A TRIAC is not the best to regulate an inductive load but as you have experienced it does work.<br>Anyway, humming can be a problem with TRIAC dimmers. Is it coming from the dimmer itself or is it coming from your fan? is it surely an electrical humm/buzz or perhaps a mechanical one.<br>You could consider a choke filter... though these sometimes give a bit of mechanical hum.<br>Do you have it on a breadboard or on a PCB?
<p>As i am still testing so it is on breadboard.The humming sound is coming from fan ( it's a table fan). The circuit is working quite better as compared to just PWM but some what humming is still there from fan.</p><p>Also the 30k 1/2W resistors are heating when fan is turned on.So should i use higher wattage resistors or any other option. </p>
<p>There are several questions (and answers) on the 30k resistors getting warm.<br>It is odd though that they get warm when the fan is on as their only task is to supply the zerocross pulse, so please check your wiring for any faults.<br>The 1/2 Watt should be OK, it is hard to say how warm or hot 'heating' is but if your wiring is ok and you find them too hot, yes a higher wattage should be OK</p>
<p>Hello bloke</p><p>Thank you for the help. Your reply is very help full.</p><p>Just one more question, I m just trying to use remote to control the fan, i used the IRremote library. The fan control &amp; IR communication is working well seperately. But when i merged the program IR sensor took garbage values. Its look like arduino got too much busy with Zero crossing detection loop.</p>
<p>The first program in the instructable is as I indicated in the tekst, a quick demo to test the circuit. And indeed most of its time is spent waiting and therefore it is not the most suitable program to do other things with. What you need is a program that doesnt wait, but checks a timer to know when to swith the TRIAC on. Such a Timer driven program you will find a bit further in my instructable.<br>I am not sure which interrupt you use for the IR receiver, but make sure it is not one that is used by the Timer you are bound to use or by the zerocross routine</p>
<p>Thank you bloke i will work on it.Thanks again for all your help.</p>
<p>My pleasure. Good luck</p>
<p>I used the fixed dimming values for 0,1,2,3 buttons of remote in program. For example for 3 i used dimming=15 which is the highest speed of fan. For 2 its 80. I got that values from trial &amp; error.</p>
<p>The humming coming from your fan is a bit of a hard nut to crack, it comes a bit with the use of a TRIAC. Sometimes a choke filter can help</p>
The only reason I can think off why the moc died is if the TRIAC burned and a full 220V came over the moc. that is why i asked to test the circuit without the moc.
Hello Bloke, <br><br>You were right. I changed the triac and now it is working. Do you know what are the reasons to burn a triac and some preventive actions? I would like to avoid to burn it again in the future. Thanks in advance! <br><br>Peter
<p>I am glad you got it working again.<br>I am 99.9% sure it died because of your laptop. I am not entirely sure what laptop you have but most have a switched PSU and that is really not a good load for a TRIAC, so I guess it was a spike or something.<br>I guess the best way to avoid it is not to plug in yr laptop anymore :-)<br>Now ofcourse that could have been a coincidence and there was another reason, but that would be far-fetched</p>
Hi Bloke, <br><br>Thanks for the comment. My triac should support at least 600 vrms and 4 amps. So it should be able to support the possible peak in the laptop, doesn't it? <br><br>Or maybe trying to dim a laptop causes a higher peak in current and/or voltage? <br><br>Thanks in advance for your time, <br>Peter
<p>I presume it is a voltage peak. Most switching psu's start with an induction in both lines and just as a relay in the collector line of a transistor can kill that transistor, in this case that might have happened to the TRIAC. Though I would expect a laptop psu have a MOV or something as overvoltage protection, maybe that wasnt fast enough for the spikes. Mind you that with an induction the current and voltage are out of phase and that is something TRIACs dont handle very well</p>
Hi Bloke, <br><br>I understand your point. Is it any preventive circuit to add? I mean like snubbing or something? <br><br>Peter
I will try as soon as I arrive at home on Tuesday. By the way, I changed both components due to availability and using Liteon's moc3023 and STM's Z0405MF0AA2. Looking at the specs, both should be also OK, what do you think?
Just to be sure I was clear enough before, i am using both components since the beginning and not yours. But I think it shouldn't be the issue since the whole circuit was working over a month
<p>yes was clear :-)</p>
<p>I build a prototype circuit on perfboard to check how the circuit performs. 4N25, MOC3021 &amp; BTE137 used. Load is 230V 70W halogen lamp with a 6A circuit breaker for basic safety. Arduino pro mini and circuits runs flawlessly on 3.3V or 5V.</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/6xaxQnqF-AI" width="500"></iframe></p><p>I'm going to etch a PCB as soon as possible.</p>
<p>looks great. Thanks for sharing</p>
Good evening, <br><br>First of all, congrats for the great Instructable. It was running for days perfectly but suddenly led is now always ON. The only thing I made different was to connect a non dimmable load (a old laptop) in the output which consumpts about 300mA to see what's going on. Now, I checked the circuit and in the moc input I have 0 but in the output (triac gate), I have a 1, so it's clear MOC is now damaged. Do you think it was casualty or trying to dim a non dimmable load can damage the MOC? <br><br>Thanks in advance, <br>Peter
Peter, It is a bit too much of a coincidence, not te be related to plugging in yr laptop.<br><br>I just hope it was just your moc that got killed. Can i suggest to remove the moc, attach a lamp, then plug the device into the mains again and see if the lamp lights or not?<br>just to check if yr TRIAC is ok
Ok, I will do that. If it was a coincidence, is there any possibility to reduce chances to kill the MOC? I mean, a preventive circuit or something similar? I didn't use it so much to kill it.
<p>i am not 100% confident, where could i buy one ? i really nead it</p>
<p>checj inmojo.com </p><p><a href="http://www.inmojo.com/search/?type=products&s=dimmer">http://www.inmojo.com/search/?type=products&amp;s=dimm...</a></p>
<p>Hello, First of all I am very thankful to you...</p><p>Excellent Document and each concept is explained very well...</p><p>I still want to make this compact in size. So instead of using Arduino, can I use directly atTiny 85 Controller? Will you just provide the AVR code for the same which is normally done in AVR Studio.. </p><p>Hope for positive response....</p>
<p>As the attiny 85 has an external interrupt, an output pin and a correct timer, I see no reason why that should not be possible, or, in other words, yes i think you can use an attiny 85</p>
<p>Thanks a lot....</p>
Hi<br>Can we convert AC current to DC current and use fading sketch like led<br>And remove zero crossing part??<br>Thanks for your effort
<p>something like this? </p><p><a href="https://www.instructables.com/id/AC-PWM-Dimmer-for-Arduino/">https://www.instructables.com/id/AC-PWM-Dimmer-for-...</a></p>
<p>Thanks a lot! </p><p>With your tutorial I was able to do this dragon ball lamp :)</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/sMglBAEWoIo" width="500"></iframe></p>
<p>I am happy it helped you. Thanks for sharing your pics and video</p>
<p>This is better and more safe way to control AC light dimmer</p><p><a href="https://www.instructables.com/id/Control-AC-Dimmer-Lamp-Using-Arduino/" rel="nofollow">https://www.instructables.com/id/Control-AC-Dimmer-...</a></p>
<p>This instructible is by far the most comprehensive piece of work I have ever read! AND it does not use this platform as a free advertising opportunity...</p>
Thank you gsmhack :-) I appreciate it.
<p>Well, &quot;better&quot; is up for debate and 'safer' is ofcourse nonsense, but i do understand you are trying to promote a product that is sold by you for 49USD, whereas mine will be a few dollars. <br>Seems you have a month left to generate the remaining 99% of your target. I wish you a well meant success<br> </p>
<p>as a maker i tried many way to control AC voltage devices and i did the same circuit before,first better when you want to integrate it with more advance application and add more sensors or motors or control it from wifi or BT or even from computer using serial communication,with your way it's only good for this application and with interrupt routine occupied by zero cross and triac driver trigger action you can't build a many functions project.<br>all i want to do is to let the people see that there are different way to do this and also control it with different platform&quot;not just arduino&quot; and it's not for the campaign,even if the campaign fail we will continue support and launch this product again because we believe it will be useful or many people.</p><p>thank you so much and I apologize for any inconvenience caused by these two comments</p>
<p>Hello , excelent Post, maybe you can solve one doubt that I have. To turn on the lamp for example (in the common way), you have to connect one side of the lamp to the power supply (Neutral) either (220V or 110V) and the other side the lamp to the phase, but the wire connected to the Phase you have to cut ir and connect to a switch. Every thing ok until here.</p><p>My question is: I just bought a commercial dimmer (Lutron) and is easy to connect you only disconnect and remove the normal switch and connect the dimmer. but inside the wall you have only one wire cut and this wire is the Phase, how the commercial dimmer works only with one wire (Phase) if in this post to dimerize the lamp we used the Phase and the neutral connected in one terminal block, and on the other teminal block the lamp.</p><p>Is possible to use the circuit board of this post in the way of a commercial dimmer, only with the phase?</p><p>I really appreciate your answers.</p>
<p>I apologize that i overlooked your question for a few days.<br>Well the dimmer you describe works in series with the lamp, but in reality ofcourse it is still using two wires...that you get by cutting the onewire that goes to the lamp,just as a switch needs two wires, but yes, it is still one phase. So that dimmer is a typical series circuit: the neutral wire goes to the lamp, a return wire goesfrom lamp till dimmer and then the phase goes from dimmer to the grid again, so yes, by interrupting the one wire to a lamp and putting the dimmer in between, it dims in series with the lamp.</p><p>The dimmer here works exactly the same: it is in series with the lamp and thus the TRIA just interrupts one wire. S also here we have a wire from the grid going to the lamp and a return wire going to the triac and from the triac back to the grid.</p><p>So far so good. BUT!!!! the dimmer needs to measure the zerocrossing of the grid and for tht it needs both phases</p><p>So, to answer your question......no, you need two phases.<br><br>I presume you want this so you can replace a switch with this dimmer? but even then you would still need to have 3 wires coming from that switch to your microprocessor: the zero cross signal, the trigger signal and a ground.</p><p>I think that in most countriesit is against code to put a circuit that has a low voltage side inside a regular wallsocket and or have wires coming out of itso this circuit is more suitable as a separate dimmed socket<br><br></p>
<p>Thanks for your really good explanation, in fact I was thinking in replace my common switch with this dimmer because work great.</p><p>Maybe can help me with other question in the sketch of step 7 (Software to set level using up an down buttons) I can see a variable called dim2 and in the comment (led control) is necessary to change some code to dim to LED bulb ?, because with the code showed above the LED bulb has two states ON / OFF and doesn't dims.</p><p>Thanks again.</p>
<p>When I wrote the instructable, I thought it was a good idea to gather some example codes from internet that i thought would benefit people. I was in the presumption that these codes were tried and tested, but apparently they have not been. So if that particular code does not work it is probably best to take my demo code as a basis and just ad or subtract from the dim value when the uo or down button is pressed</p>
<p>Excelent post, thanks a lot for share your knowledge with Us. People like you help make knowledge available for all.</p>
<p>hey thanks for sharing. i try to pwm the ac lamp with opto triac circuit. in my country the voltage is 220v 50Hz. when i pwm the lamp, the lamp is blinking. the higher pwm i gave the faster lamp blink. when the pwm is 255 the lamp is fully on. is that how it works? or i have to use spesific code not only just analogWrite ? thank you</p>
<p>Well this light dimmer doesnt work correctly with PWM. May I suggest you read the article as that explains and shows exactly what program(s) to use</p>
<p>Thank you for your reply. I actually want to controll the water temperature using heating element. Is your PWM program(s) that you used for dimming the lamp can used for PWM the heating element too? I used triac in my final project.</p>
<p>can i just start by again emphasizing that this is NOT PWM</p><p>yes it can be used for an AC heating element but not with pwm, it works with leading edge phase cutting</p>

About This Instructable

567,778views

460favorites

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:Adding an MCP23017 I/O Extender to Arduino or ESP8266 Calculating Sunset and Sunrise for a Microcontroller A Simple MQTT Pub/Sub Node With Arduino UNO and ENC28J60 Ethernetshield 
Add instructable to: