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>Em 127 V, consigo usar o mesmo circuito de potencia ou tem que alterar o valor dos componentes?</p>
<p>127? D&aacute; pra usar o mesmo circuito de potencia?</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>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>
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>
<p>Hi, could anyone tell me, which capacitor i really need for inductive loads (Which type) ? and is anybody having some eagle schematic and board?<br>cheerz</p>
<p>ceramic or film</p>
<p>thx for this quick answer, could you tell me a Type who is working?</p>
<p>they both are but since ceramic capacitors get very pricy for higher voltages, I'd recommend film</p>
I am not sure how thick yr traces are so I cannot comment on whether your pcb will hold 1 amp, but soldering a copper wire over the traces to the T1 and T2 of the TRIAC is always a good idea if you use a sizeable load
<p>Awesome instructable, thanks for all the extra detail and theory you put into it, I've learned quite a lot :)</p><p>I built one today and it works great! Used an IL250 and BT136 and etched your board design. </p><p>I plan to run a few bulbs in parallel, which I think should work fine so long as I stay under the 4A limit of the triac? I'm planning to write code for a pulsing/flickering effect, is there anything you think I need to consider?</p><p>Thanks again for putting this together :)</p>
<p>I am happy you like it and thanks for inclusing pics. I always enjou seeing what people made out of an ibble.<br>in spite of some people experiencing problems, it is a simple design that really cant go wrong. I have built 5 or 6 that all worked first time. <br>Running bulbs in parallel should be fine, indeed as long as you stay under your Triacs specs.<br>Code for pulsating should not be too difficult.<br>Write an alternating High and low value to the circuit and that should do it.<br>If you want the arduino to do anything else than regulate your light, I advise to use the timer rather than the delays in yr software<br></p>
<p>You're welcome :) One quick question regarding the track thickness - you mention that the load/mains tracks won't take 4 amps, is it worth just reinforcing them with wire anyway? I'm going to run maybe 200W total at 240V, will they cope with ~1A? Thanks!</p>
<p>what is yr question Rathore? :-)</p>
<p>The InMojo AC dimmer mentioned above is back in stock and can be found here:<br><a href="http://www.inmojo.com/store/inmojo-market/item/digital-ac-dimmer-module-lite-v.2/" rel="nofollow">http://www.inmojo.com/store/inmojo-market/item/dig...</a></p>
<p>I have a question, if i want to use this circuit with 120V 60Hz Which elements I should change</p>
<p>that is ad extenso decribed in the instructable :-) and even answered inthe question right below :-)</p><p>Basically it comes down to adapting the resister values to the optocoupler and using 8.3 usec steps instead of 10 us</p>

About This Instructable


277 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: 433 MHz Coil loaded antenna Using pin 1 on Attiny13 without messing with fuse bits 'Home Alone' burglar deterrent/ Use Attiny13 to drive a Steppermotor and a Servo
Add instructable to: