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

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>
<p>ok let me know</p>
Hi i cant figure out what is The load and what is The ac input can you help me plz Thanks
<p>the load is where it says 'load' and the input is where it says '220 V'</p><p>on the pcb it is same as in the circuit</p><p>; the top one is the AC, bottom one the load.</p><p>good luck</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>?</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>
<p>Hello diy_bloke. I finally found the answer to my frustrations. I found a post on the edaboard.com forum. Vital information - I haven't seen a single circuit description that tells you that &quot;PLEASE BE AWARE THAT...</p><p>the moc output MUST BE between the triac pins A2 and G<br>it is never written in any datasheets but it MUST be so<br>or your triac won't commute ...&quot;</p><p>...so that's the secret! The dark side of electronics is that these &quot;trade secrets&quot; take an age to discover. Once I understood that it was plain sailing.</p><p>Here is the thread on edaboard: <a href="http://www.edaboard.com/thread155997.html" rel="nofollow">http://www.edaboard.com/thread155997.html</a></p><p>Very useful in understanding what is going on in the circuit!</p><p>There's actually two types of triac on my 6-channel board (see photos) - BT136 and MAC228A10G, both working fine. Here they are just running a test pattern, fading up and then down, caught mid-fade.</p><p>Thanks for the circuit, but I suggest you mark out which is MT1 and which is MT2 on the triac, in case foolhardy hackers like me want to design their own boards.</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>Thanks !!!!! diy_bloke</p><p>i was searching for this circuitry for quite a time ..</p><p>this article is of great help ..., i am going to make this project ... </p><p>if ill run into the problem then i will pinch u for the possible reason .. :)</p>
<p>My pleasure. Dont hesitate to ask if u need help</p>
<p>Thanks !!!!! diy_bloke</p><p>i was searching for this circuitry for quite a time ..</p><p>this article is of great help ..., i am going to make this project ... </p><p>if ill run into the problem then i will pinch u for the possible reason .. :)</p>
<p>i didn't understand how did you get the &quot;pulse starting abt 200uS before the actual zerocrossing&quot;... in my calculations, i dont know where is my mistake, the pulse starts 972uS before the actual zerocrossing =/</p>
<p>oscilloscope</p>
<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>
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

About This Instructable

457,979views

387favorites

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:Send sensor data (DHT11 & BMP180) to ThingSpeak with an Arduino, using ENC28J60 Ethercard  Using an I2C LCD on Attiny85 AC PWM dimmer for Arduino 
Add instructable to: