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() {
  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() {

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

How conet to adrui no
<p>thats all in the text. The zerocross signal goes to one of th einterrupt pins that you define in the software (if INT 0 it is pin 2) The trigger input comes from a digital pin that you can freely chose in the software</p>
Please code to email cowboyxuan@yahoo.com thank you<br>
<p>Hi diy_bloke,</p><p>thank you for this very nice and understandable tutorial. In the circuit diagram it is not absoluetely clear to me: you do well control the phase and not null?</p><p>I would like to drice an AC motor (for house ventilation) with it. But I am afraid that when switching off, the motor induces voltage that destroys the triac. How can I protect it for such use?</p>
<p>thank you for your kind words. I am not really sure what you mean in your first paragraph as there is no such thing as controlling the null or the phase, or at least i do not know exactly what you mean with it. Do you mean if it makes a difference where the neutral and live phase go when connecting to the grid? No not at all.</p><p>driving a fan is one of the most asked questions on this tutorial :-) Yes you can. Though a triac is far from optimal to drive an inductive load, in most cases it will work, but you need to include a snubber (resistor +capacitor)</p>
<p>Thank you for the quick answer. &quot;Null&quot; was the new language frenglish (mix or french and english:) I meant neutral phase of course, as you guessed. Although electrically it is unimportant where you switch, one would (at least in Europe) always switch live phase to avoid tension on the machine parts when beeing switched off. I am happy to hear that your circuit allows this. Remains the snubber, I will try to learn this. Thank you!</p>
<p>well as I connect it with a regular plug, without marking that plug it would be impossible to know if the phase (brown) and neutral (blue) connected to the 'right' wires.<br>If it is build in with a permanent connection yes i would interrupt the 'live' phase (brown wire). To make it correct, the interrupted wire to the load should be black :-)</p>
<p>is it possible use zero crossing to dim LED dimmable bulb ?? </p>
<p>yes.... if you have the proper dimmable leds. not all are suitable</p>
do you have any reccomendations ? have you try it before ?
<p>I never tried leds in combination with this circuit. As it seems you still have to buy the leds and still have to build the circuit, I would recommend you dont use this dimmer but get a dedicated led dimmer. If you still insist on using this dimmer, get a retrofit LED labelled to work with a 'traditional' dimmer.</p>
<p>I restructured the circuit a bit and omitted the LED. <a href="http://www.instructables.com/id/Intel-Edison-Controlled-AC-Dimmer/" rel="nofollow">Works great</a> using 120VAC!</p>
<p>Great work</p><p>The circuit can do very well without the LED and in all honesty when i designed <br>as such, I had a red led in mind, not realising people might use other <br>color leds and thus not leave enough voltage for the optocoupler.<br>If I<br> have two small remark on your circuit, like me you use 2x 33k resistors. <br>However you you use them with 110 Volt. That is not a huge problem and <br>obviously it is working for you but it makes the zerocrossing puls a <br>tadd wider. That means that people could possibly experience a bit of <br>timing problems when using short delays on the rising flank or long <br>delays on the falling flank.<br>Not saying that WILL happen, but there is a chance<br>The second remark is that I am not really a fan of having a copper trace pass through optocouplers that carry a high voltage. It will probably be ok but it is best to avoid it. One wire bridge would have solved that on yr PCB.<br><br>Nevertheless good work</p>
Thanks diy_bloke. What would you suggest for the two resistor values for an ideal zero-cross pulse at 120VAC?
<p>I would leave it as it is since it is working fine for you.<br>generally in order to get a sharp pulse the resistors have to be as small as possible, but still big enough to limit the max current through your optocoupler and not to dissipate too much power.<br>your optocoupler has a max forward current of 60mA. The max Voltage it gets is 115 Volt, so theoretically a 2.3k resistor would be possible. However this would generate 5.75 Watt of heat. 2 resistors of 15 k would be fine, but again, in your case i would leave it as is</p>
<p>It seems that even with a wider pulse, as long as the pulse starts at the same time each interval, it should be ok, right?</p><p>The MCU code that is driving it polls the zero-cross pin instead of having an interrupt so I'd hope the pulse width wouldn't be so small as to be skipped or undetected though I'm unsure the approximate time it takes to cycle the loop in a worst case scenario.</p>
<p>yes it starts at the same time but i will illustrate the 'problem' by a bit of exaggeration: at 60 Hz your period for dimming is 8.3mS. Suppose you a have a zerocrossing pulse that is 4mS wide (it isnt ofcourse) and it begins 2mS before the actual zerocrossing and ends 2 mS afterthe actual zerocrossing. That means that you cannot dim during the full period anymore.<br><br>Suppose you want your lamp to be fully ON, so you switch on the TRIAC immediately at the zerocrossing pulse. However, that is 2mS before the actual zerocross that will suddenly switch off your TRIAC, so instead of Full ON it is dimmed to almost zero</p>
<p>Is there any reason you couldn't account for that in code? Shouldn't matter if you're behind as long as it's on time in reference to when the triac turns off at zero cross, right? I did test the circuit with a multimeter and I'm able to get a pretty full range from 0-119+VAC so it works well in practice. I also like the idea of less power consumption and heat.</p>
<p>sure that is possible. if you know the width of your pulse can account for that. Say it is 200uSec, then you know your real zerocross is 100uS after the interrupt.</p><p>As you have a full range there is no need to change the resistors.</p><p>Minimizing the powerconsumption is very well possible. the simplest is to use a transformer, but then you will be introducing a hoist of otherproblems. You may want to try this: </p><p><a href="http://www.3e-club.ru/files/2009-12-09/zero-cross/full1.jpg">http://www.3e-club.ru/files/2009-12-09/zero-cross/...</a></p>
<p>Regarding power consumption, I was referring to the difference between 2.3k where more power is consumed emitted as heat and 33k where less power is consumed. As you said, there's no reason to fix it now since it ain't broke.</p>
<p>ah OK, I misunderstood. I thought that was a separate idea :-)<br>Indeed, if it aint broken dont fix it</p>
<p>Thank you for your tutorial! Now I am about to implement 8 channel dimmer... ;-) </p>
<p>looks great. good luck with the 8 channels. Thanks for sharing pics</p>
Thank you for the tutorial. I am new to Arduino and this is the first 'bigger' project that I am doing with it. I made the circuit and it works perfectly with the code from step 4. However, I can't get the code from step 7 (with up and down button) to work. I connected the two buttons to pin D4 and D5 so that when the button is not pressed, the pin is connected to ground via a 10k pulldown resistor. When the button is pressed, the pin gets connected to 5V. When I press the buttons, nothing happens. What am I doing wrong? <br><br>My goal is to control multiple lights (dimmable led lights) independently via bluetooth using my phone. I still have to learn a lot about sketches and how to make/adjust them, but I am enjoying learning so I think that should not be a problem.<br><br>Thank you!
<p>well seems to me you didnt connect the buttons right. both buttons connect to ground, not to 5 volt and you dont use a 10k pulldown.</p><p>It is actually easy to see from the code as the loop checks for a LOW state of the buttons, not a HIGH state to alter the value of DIM.</p><p>Now having said that... when i made this device some time ago I collected some software from the web that i thought would benefit people and this was one of those. I then presumed other people to be more knowledgeable on the dimming software than me. Now i see some shortcomings in this code that i would certainly suggest you change in due course</p><p>In the loop you see the following lines:</p><p><br> digitalWrite(buton1, HIGH); </p><p><br> digitalWrite(buton2, HIGH);</p><p>What these do is to invoke the internal pull up resistor, after all, we use these as inputs.</p><p>However, it is unusual to keep doing that continuously in the loop, it is usually done in the setup. It is probably done this way so you can keep the button pressed, but it isnt very elegant.</p><p>Anyway, i trust that changing the buttons, so betewwn D4 and ground and D5 and ground will solve yr problem</p>
<p>Hello, it's a very nice tutorial!</p><p>I have made a circuit for 3 bulbs and it works well with them. However i would like to implement it with some LED lamps which you can find it here: </p><p><a href="http://www.bright-led.hk/a/Products/LED_Outdoor_Lighting/LED_Street_Light/262.html#.V2u2fZdzK9" rel="nofollow">http://www.bright-led.hk/a/Products/LED_Outdoor_Li...</a></p><p>with the AC input voltage. So, what is happening when I connect it with this circuit? Sometimes the lamp is turned on and then it turns off for a few second, sometimes the lamp flashing, and so on. </p><p>Does anyone have any suggestions? Should I change something in the circuit, or maybe these lamp aren't appropriate?</p>
<p>The lamps are not appropriate. As LEDs typically work on a low voltage, there are electronics in the lamp to bring it to that low voltage. Those are not compatible with dimming by phase-cutting</p>
<p>Ok, thank you. Do you know maybe for any other solution to dimm these lamps?</p>
<p>without opening them i dont think there is</p>
<p>it largely depends on the LED, some will respond to a traditional dimmer, some need a 1-10 v voltage for dimming signal. Your lamps, sorry do not really see a possibility for those</p>
hi friends!<br> I'm trying to implement AC dimmer circuit to dim LED bulb(which generally use in pop) but seams its not working...any solution?
<p>it only works with specially suited LED bulbs. What type of Bulb do you have?</p>
Diy Broker, can you put in Blue ac output And in Red load ??<br>Thank you it will greatly help me... Have a Nice day.<br>Marc.
<p>Also in the component lay out on the PCB (2nd pic) it actually says 'Mains' and 'Load'</p>
<p>The AC output goes to the load, but i guess you mean the input.</p><p>If you look at the 3rd picture of the pcb with the red text on it, at the right side it has two connectors. The top one, that has the two resistors pointing to it, is the ac input. The bottom one, that has the single jumper wire pointing to it, is the connector for the load</p>
<p>Hi,</p><p>I'm totally new to electronics but not new to programming.</p><p>I didn't pay attention to all those warnings about killing my self hahaha</p><p>how even I took some distance while turning it on ;-P</p><p>Just got one question, I have some light flicker, probably if a other device is drawing high power in the house, is there a way to stop this, maybe capacitors???? </p>
<p>Odd, I replied to your comment but I see it hasnt shown up. OK here we go again: Flickering is usually caused by one of a few things: The PSU, or the timing. A dirty PSU may cause flickering make sure it is well decoupled.<br>timing may also be an issue as the zerocrossing signal has a certain width: it starts a bit before the actual zerocrossing and ends a bit after. Usually the flickering then is either at th elowest setting or at the highest setting. Is this what is happening in your case?</p>
<p>I have read all the comments and done your suggestions but for some unknown reason my 2 resistors get burning hot. I started with 10K and went all the way up to 120K (1w and 2w).</p><p>Interestingly when I turn off the arduino, they don't get hot or even warm at all.!! but as soon as I supply power to my arduino, those two resistors become hot in matter of 2-3 minutes...</p><p>please look at my schematic. That red thing on top right is my 5v power supply. I also removed that and tried to power up the arduino from my ICSP but that did not change anything. </p><p>what do you think the problem is?</p>
<p>That is fairly simple. You didnt follow my circuit. You put the two 33 k resistors in parallel. I have them in the two seperate powerlines (neutral and phase) so they are in series.<br>I also dont understand why you started with 10k when it says 33k.<br>With the 10k in parallel you created a 5 k resistor that had to deal with about 10 Watt !!!!<br>the 2x 120 k in Parallel should have been OK.<br>Turning off the Arduino should make no difference according to your circuit</p>
<p>I tried 10K just to see what would happen. 2x 120K 1w got hot. I tried 2x 220K 1/2w and it seems to be ok. they become warm but not burning hot. I will try 220k 1w and will see what would happen.</p><p>My bridge rectifier used to also get hot when I tried 120k and lower values. However it did not get hot when I tried 220k.</p>
<p>no need to justify. Experimenting is the way to learning :-)</p><p>when you have 120 k in practice you have a 60 k resistor which is in the same region as the 2x33k in series that i have, they each shld dissipate 400mW that is enough to make them warm, not hot, Same for your rectifier. Are you sure you dont have a stray connection somewhere on your pcb?</p>
<p>thanks for teaching me everything to know about dimmer theory and providing examples and troubleshooting information as well. Will certainly have use for this instructable in the future.</p>
<p>My pleasure Rob. Thanks for your kind words. Glad you found it informative</p>
<p>I'm interested to know if you tried it with an inductive load, like an AC fan.</p><p>On the other hand, if one were to shoehorn this for a fan like that, what sort of snubber would you put in that doesn't mess up the firing? Something like a 45W AC fan.</p>
<p>I havent tried this circuit with a fan, but if i were to, i would start withsa a 100nF capacitot and maybe a 56 or 47 ohm resistor</p>
<p>I made a similar circuit(a little bit different) with Atmega8A/BTA16.The speed control works fine while on pure sine wave AC.I have an inverter which output square wave AC.The circuit malfunctions in square wave(The fan stops within first few minutes/great hum with very low speed/Great hum from inverter/No movement of fan).I am controlling speed of my ceiling fan.Is the mafunction normal at square wave ??</p>
<p>I am not quite sure what it is that you built. What is the inverter for? why do you use square wave if you have a functioning sinewave?<br>You need to give me a bit more info. preferably a circuit</p>
<p>Well...I am trying to control the speed in 5 different steps namely 0 to 4.</p><p>In 0,the fan stays off as the MOC3021 gets 0 from the Atmega8a's output pin 12.In 4,the MOC3021 is always on so the fan rotates in full speed.The speed control is done in 1,2 &amp; 3 steps by firing the triac in 3 different delay timings correspondingly.</p><p>my circuit works pretty well in the 220v electric supply(which have a pure sine wave).But whenever main fails and Inverter power takes place,the circuit starts to malfunction(The fan stops within first few minutes/great hum with very low speed/Great hum from inverter/No movement of fan).</p><p>I attached my DSO and found out that the Inverter generates pure square wave.I also made several changes in the triac driving section as listed below:</p><p>1.Replaced the BT136 and used BTA16(snubberless triac).</p><p>2.BTA16 with no external snubber attached.(omitted the external snubber).</p><p>3.Replacing the 470R+330R with a 1k and also attached another 1k from Pin4 of MOC3021 to Triac pin MT1.Also omitted the 47nF cap.</p><p>4.Add a 100uH choke between MT1 and LOAD.</p><p>5.Reattach snubber circuit with this position.</p><p>I also tried the stated configs with BT136 also. </p><p>I tested with all the above mentioned configs but each and every one failed to regulate fan speed on my Inverter output(Only 0 and full speed i.e-4 is functional).But all the above configs performs very well in Supply AC.</p><p>My question is what is necessary to control speed of my ceiling fans while on inverter power??</p><p>Please reply....I am attaching my circuit diagram</p>
<p>ok, it is clear.i thought somehow you had 'an inverter' in yr circuit, but basically your circuit is exactly the same. Your question is: It works on the sinewave of the grid, but it doesnt work on emergency power which is a square wave. So in hindsight the circuit wasnt necessary</p><p>You are not the only one with this problem, dimmers not working on DC-&gt;AC converters that supply square wave. The odd thing though is that the explanation for that, doesnt apply for this type of dimmer. However, a fan is not a pure resistive load as there is a phase shift. Although a Triac dimmer can be used for a fan it is not the most ideal and I think it is the square wave in combo withthe phase shift that makes the Triac not ignite. Can you do a small test? Instead of a fan, try dimming just an incandescent lamp when you have the dimmer work on your inverter and let me know what happens</p>
<p>Thanks a lot for the reply.....out of curiosity,i attached a small table fan with this circuit and the speed control was working for some time while on inverter.But i didn't conducted the test for a long time and also didn't pay much attention as my main goal was to control the ceiling fan speed .Of course i will conduct those tests with both the small table fan and a light and let you know..</p>

About This Instructable




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:Building a Simple and cheap egg incubator Comparison of capacitive soil probes Send sensor data (DHT11 & BMP180) to ThingSpeak with an Arduino, using ENC28J60 Ethercard 
Add instructable to: