Step 5: Arduino Controlled Lightdimmer: The Software II

I found another piece of Software that allows controlling the lamp via the serial port. I have not tested it myself yet, but I see no reason why it should not work. It triggers on the falling edge of the zero-crossing signal, so the timing is a bit different.

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

void setup() {
  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
    digitalWrite(AC_pin, HIGH);
    digitalWrite(AC_pin, LOW);
void loop() {

Even more software <a href="http://wiki.dxarts.washington.edu/groups/general/wiki/4dd69/AC_Dimmer_Circuit.html" rel="nofollow">here</a>
<p>I have few stupid questions (I know it's basics but I can't find answers and I have not anyone who can help me with this) ...How does &quot;zero<br>crossing detector&quot; works? What happend if the Voltage is 0? What<br>happend if Voltage is more than 0? Where current flows in this two<br>cases (left side of 4N25) and what Voltage value is there?</p><p>I understand this in this way: when Voltage is 0<br>the current flows the green way and when it has some Voltage &ndash; red<br>way. But in that case why 10k resistor? Voltage will not be very low...? </p>
<p>if the voltage is zero, that is when the zero is detected. The optocoupler blocks and no or only a small current will flow through the transistor: Therefore the voltage on the collector will rise to close to the Vcc<br>If the AC voltage is not 0, the transistor in the optocoupler will be triggered and go into conduction therefore the collector will be pulled close to 0</p><p>With regard to yr question about the value of the 10k resistor and 'will the voltage not be very low' Well.. which voltage, the voltage when there is no conduction or the voltage when there is conduction? Maybe that already answers yr question: The value is somehat irrelevant. The voltage only needs to be high or low. <br>Theoretically that resistor could even be 100 MegaOhm because if there is no current flowing, the Voltage on the collector will still be 5Volt.<br>The reason why i dont make it 100MOhm is because in practice a small current will flow into the input pin, but even then 100MOhm might still give a HIGH when needed.<br></p>
<p>Thanks, that helped me, but now I have new questions :) Why there is 30K resistors? This is because the bridge or 4N25 can't stand 220V? And here my second question: what does '400V' under bridge mean? Is this max voltage of it? And the last one: why 1k resistor is conected to MOC3021? I tried understand this by reading datasheets, but I'm not so good at it...</p>
<p>It is the 4n25 whose current needs to be limited, hence the 30k resistors<br>400 Volt is the max rating of the bridge<br>The 1k is there to limit the Gate current</p>
<p>I need your help again. I build the circuit, I upload photos and schema. I use BT136 triac and MOC3021. Now the problem: the bulb is on only a short part of time when I plug or unplug: https://youtu.be/_EshGUidjB0 at 17s you can see that nothing happend, bulb is off. I connect wire direcly to triac and then bulb is on so triac is working fine. I change MOC3021 to another MOC3021 and that's change nothing. Zero crossing detection is working fine. What can be wrong here? I can show you my code, but i use STM32F0DISCOVERY instead of Arduino</p>
<p>I also try a differnet kind of bulb and another plug</p>
<p>Could you explain why using a optocoupler with zero crossing detection won't work (e.g having a MOC3042 instead of a MOC3021)? And what the zero crossing detection actually does in a optocoupler like MOC3042? </p>
<p>A MOC3041 or any other zerocrosing optocoupler when receiving an ON signal on the LED, switches at the moment there is a zerocrossing detected. But that is not what you want. You want to switch on the lamp at a moment of your choice and with that moment I mean a moment on the AC sinus curve. That is the entire principle of this circuit. So.. you detect a zeocrossing, wait a desired period and then switch on the lamp. The longer you wait, the less bright the lamp will be.<br>The Moc3041 doesnt give you that choice: it switches on the zerocrossing</p>
<p>I am still confused with the concept of zero crossing detector that I will apply to light 220v regulator circuit pakek moc3041, triac, together arduino. if for example of the pin out of my arduino give high voltage (5VDC) to the input pin moc3041 then the lights will be bright .. I ask if I give the voltage on pin out arduino 2,5vdc moc3041 input into whether the lights can be moderate? I've tried wearing moc3021 but to no avail only be on / off only .. please help .. I'm sorry if my language is not good ..</p>
<p>ef.ee there are a few things wrong with your thinking.<br>1. you cannot put a voltage of 2.5 volt on a pin. Yes you can put a pwm signal on a pin that may give an average of 2.5 Volt, but the optocoupler will still see that as a number of 5 and 0 volt signals.</p><p>2. Even if you could put a 2.5 volt signal on a pin, the optocoupler will either conduct or not<br><br>3. If you were to use a PWM signal and thus switch the triac on and off very fast, that has no relation to the amount of light coming from the lamp as you do not know when the lamp will be switched in relation to its phase.</p><p>As you are using an AC voltage the voltage is zero twice within the period of the sinus. and it is max twice within that period as well.<br>If you would briefly switch on the light when there is zero voltage and switch it off immediately again, yr lamp would not burn coz there is no voltage on it. If you would do the same at its peak, the lamp would burn at its brigthest. Hence a PWM signal is totally uncapable of regulating the brightness of an AC lamp in any predictable way.</p><p>The only way to do it in a predictable way is when you know where in the sinuscurve you are and then switch on the lamp.<br>If you would switch on the lamp when the voltage is zero and leave it on, it would get the full amount of power (Area under the curve). if you would switch it on halfway the sinus and leave it on, it would get only half the amount.<br>So that is how you regulate it... but ofcourse you then need to know when the AC sinus starts.... and that is why you need the zerocrossing signal.. to tell yr controller when the sinus starts<br></p>
<p>Hello. First of all thanks a ton for the amazing tutorial. Really helped me put together my own home automation system. </p><p>I am facing a problem though. The circuit works flawlessly under normal situations. But if i connect a load to some other source in the house there are momentary flickers in the load connected to the circuit. I am assuming this is caused due to noise in the circuit. </p><p>Here is the link to a video-<br><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/45WM_nNSN-o" width="500"></iframe></p><p>Would appreciate any help what so ever. </p>
<p>I actually have never experienced that but indeed when you plugin another load, especially repeatedly, you can get spikes on yr mains lines and that may either directly tigger your triac However, it may also put spikes on the psu of your Arduino (or other microcontroller).. To pinpoint the problem try doing the same with yr arduino disconnected or fed from a battery.</p>
Thanks for your prompt response. Really appreciate it.<br>I will give it a try today and let you know, if it works for me.<br>Thanks!
<p>Hello,</p><p>I have tried using battery as per your suggestion but it didn't work. Even with battery supply, if there is another load connected to any other switch in my home, bulb flickers when it is off.</p><p>The code doesn't even dim the light. Below is pseudo code -</p><p>while(1){<br>switch on the bulb<br>wait for 30 sec<br>switch off the bulb<br>wait for 30 sec<br>}</p><p>With this logic, it just turn on and off the bulb and stays in one state for 30 sec.<br>If I try to connect another bulb to some other switch (like shown in video), bulb connected to this circuit flickers when it is off.</p><p>Also, if I switch on the power supply (AC mains) to this circuit, there is an initial flicker (on and off just once). Here is the video.</p><p></p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/fLYwiOE-K0g" width="500"></iframe></p>
<p>first got to sauy that instructables acts weird. My earlier reponse to you seems to have disappeared. Good thing I check otherwise both of would be thinking: why no reply?<br>Anyway. Just like you to do one more thing. disconnect your arduino completely, connect yr circuit to the mains (lamp should be off) and then try to insert a seperate load into a socket again and see what that does. (I expect there will be a flickr again).<br>What I tried to do is to seperate between a trigger pulse coming in through your arduino or a trigger pulse coming directly onto your gate.<br>pending outcome of the recent test i just proposed, I presume it is most likely your circuit suffers from so called spontaneous triggering.<br>This is usually caused by noise on the mains line, causing a sharo dV/dt<br><br>So I would like you to check the following:<br>does the problem persist if you plug in your circuit the other way around? (basically switching phase and neutral)<br>Can you double check that it is M! of your Triac that is connected to the gate via the MOC and not M2?<br>It is hard to see from your video, but how long are the connections to your gate?<br>Which Triac type are you using?<br><br>There are some ways to combat sponteneous triggering (though one can wonder if it is really spontaneous).<br>The easiest would be a 1 k resistor between the gate and M2 and adding a snubber circuit. The latter though is kinda picky with regard to its values and if wrong may not do anything ta all, but a 39nf capacitor in series with a 100 ohm resistor over M1-M2 are often used. But... I hae known cases in which spontaneous triggering disappered wheb a present snubber circuit was actually removed.<br>Using less sensitive TRIACS is also an option. The TIC 206 I have in the circuit has a rather sensitive gate. The BT139-600H e.g. is relatively resistant to noise on the gate (it has to be an H-type)</p>
<p>well that proves it is not any glitches on yr arduino's powerline. Can you try one more thing? just disconnect your arduino and connect yr circut to the mains (lamp should be off) and then insert another load?</p>
<p>127? D&aacute; pra usar o mesmo circuito de potencia?</p>
<p>Zelfde kring maar andere waarden, gewoon halveren is het beste</p>
<p>Em 127 V, consigo usar o mesmo circuito de potencia ou tem que alterar o valor dos componentes?</p>
<p>Dat staat allemaal beschreven in het artikel. Bij 127 Volt kun je de weerstanden halveren</p>
AC Light Dimmers from KRIDA Electronics:<br><br>http://www.ebay.com/itm/111764492631<br>http://www.ebay.com/itm/111764507278<br>http://www.ebay.com/itm/121752461158
<p>kinda looks similar to mine, Difference is the price: ebay modules 21 USD + 5 USD shipping, my design: about 3 euro's and some elbow grease.<br>As the seller is in Latvia, like you, I presume you are the maker, so in fact this is a bit of spam, but I'll allow it.</p>
one more question sir. can i use moc3041 place of 4n25 . thanks
<p>no you cannot. It is a thyrister optocoupler with zerocrossing detection</p>
thanks for your help
<p>by the way, if you havent built this yet: I am working on a circuit that can use PWM to dim a lamp. The software will be much simpler with that</p>
i watch the video , when you post PWM to dim lamp? . Can that circuit dim ac fan or motor
<p>oh ad that PWM circuit is NOT suitable for an AC fan or motor</p>
<p>hello sir</p><p>because this circuit does not can dimming a AC motor</p>
<p>correct, or at least it isnt really suitable for that.<br>AC motors cannor be 'dimmed'<br>One can regulate them by feeding less voltage, but as they are an inductive load, there is a phase difference between current and voltage which makes this circuit less suitable</p>
<p>Very thanks for your reply,</p><p>then, what circuit you recommend for regule an AC motor</p>
<p>it largely depends on th emotor you have, check here for some options: </p><p><a href="http://www.instructables.com/community/Speed-Control-for-AC-Electric-Motors/">http://www.instructables.com/community/Speed-Contr...</a></p>
<p>Very thanks,</p><p>I have other question</p><p>I can use the circuit for dimming a lamp , for regule the speed of an universal motor, or a bifasic motor?</p>
<p>I have a working circuit already, just need to make some pictures before posting on insructables. However, I published it here already: </p><p>https://arduinodiy.wordpress.com/2015/07/25/1498/</p>
<p>my pleasure</p>
Can I use 4 diode for bridge rectifier , if yes then how i connect the 4 diode with 4n25 or mct2e.
<p>A big thank you for the explanation!</p>
<p>Thanks Garret. Always glad to see what other people made of it :-) Looks pretty neat</p>
<p>hello sir</p><p>i am student of 7th sem i need your help.</p>
<p>well, what can i do for you?</p>
Can I use 4 diode for bridge rectifier , if yes then how i connect the 4 diode with 4n25 or mct2e.
<p>yes you can. You connect it the same way as you connect the bridge rectifier as a bridge rectifier IS 4 diodes in a small package</p>
can I replace 4n25 with mct2e ? IF YES, Dose mct2e need bridge rectifier ? thanks a lot for this post.
<p>yes you can and yes it still needs a bridge rectifier</p>
<p>Can I use any of the Arduino's PWM PIN to control the dimming. Basically I need to do other things at the same time and leave a free CPU resource.</p>
<p>PinakiG, I think you misunderstand the circuit. It doesnt work with PWM<br>It works by delaying the ignition of the TRIAC a set time from the zerocrossing..<br>If your CPU needs to do other things at the same time, there are couple of options: use a timer interrupt for the delays. You will find such a program in the program section of my instructable, or wait for my new instructable in which I show how to dim an AC lamp with PWM. That makes the software a lot easier</p>
<p>i have a problem my circuit is not sitching off the bulb..</p><p>any one who can help me out</p>
<p>could you give a bit more details? you mean it is dimming the bulb but it doesnt dim down all the way to OFF ?</p>

About This Instructable


299 favorites


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: Simple EEPROM Module for Arduino or other microcontroller 433 MHz Coil loaded antenna Using pin 1 on Attiny13 without messing with fuse bits
Add instructable to: