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>Hi, thanks for you great explanations, sorry for my english but my mother tongue is spanish, I have a question please, I implement the code above and only have two states, ON and OFF although the variables dim, dim2 and dim2 change when i press de buttons, in dim = 128 de lamp is off and and any other value of dim diferentes to 128, turns ON the lamp. Finally when the lamp is OFF whit dim=128, after a while the lamp starts blink, I apreciate a lot youy help, I hope you can answeer me question, thanks.</p>
<p>Seems you are using something else than the demoprogram :-) OK first things first: what happens if you load my demoprogram. Does that work properly? </p>
First, thanks for answer my question, second Yes, when I use the demo program works properly, makes the loop from 0 to 128. Whit the demo program I try to send individual values makes a multiplication between 65 (because in my country works wirh 110V 60Hz) and dimming values but doesn't work, because the lamp turn on totally with each value. Finally I used another program that works with the buttons to up or down the dimming level, which caused my doubt.
<p>ok so the hardware works. That means the problem is in your program. I cant say anything on that without seeing your program</p>
<p>Hi, now the program works, but I did a lot of tests and I have the following results:</p><p>1) With dimming=5; -&gt; the lamp is totalilly ON max poer</p><p>2) from dimming=10 to dimming=109 tthe lamp is lowering its brightness</p><p>3) In dimming=110, the lap starts flashing</p><p>4) from dimming=111 to dimming=118 the lap is on its minimum power</p><p>5) in dimming= 119 the lap increase its power again </p><p>6) in dimming=128 the lamps starts a buccle form max power to low power </p><p>7) in dimming=127, dimming=126, dimming=125 the lamp is on its max power again</p><p>In conclusion:</p><p>1) For values over dimming=110 the performance of the dimmer I don't understand</p><p>2) with dimming= 60 and over the zero cross circuit emits a beep, i think that is normally because i have in my room a normal dimmer that i bought in a store</p><p> Thanks very much, I appreciate your help</p><p>The code I am using:</p><p>int AC_LOAD = 3; // Output to Opto Triac pin</p><p>int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF</p><p>void setup()</p><p>{</p><p> pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output</p><p> attachInterrupt(0, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above</p><p>}</p><p>//the interrupt function must take no parameters and return nothing</p><p>void zero_crosss_int() //function to be fired at the zero crossing to dim the light</p><p>{</p><p> // Firing angle calculation : 1 full 50Hz wave =1/50=20ms </p><p> // Every zerocrossing thus: (50Hz)-&gt; 10ms (1/2 Cycle) </p><p> // For 60Hz =&gt; 8.33ms (10.000/120)</p><p> // 10ms=10000us</p><p> // (10000us - 10us) / 128 = 75 (Approx) For 60Hz =&gt;65</p><p> int dimtime = (65*dimming); // For 60Hz =&gt;65 </p><p> delayMicroseconds(dimtime); // Wait till firing the TRIAC </p><p> digitalWrite(AC_LOAD, HIGH); // Fire the TRIAC</p><p> delayMicroseconds(10); // triac On propogation delay </p><p> // (for 60Hz use 8.33) Some Triacs need a longer period</p><p> digitalWrite(AC_LOAD, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC</p><p>}</p><p>void loop() {</p><p> dimming=30;</p><p> delay(10);</p><p>}</p>
<p>It all suggest that somewhere your timing is off, but before I go any deeper, explain this to me:<br>&quot;2) with dimming= 60 and over the zero cross circuit emits a beep, i <br>think that is normally because i have in my room a normal dimmer that i<br> bought in a store&quot;<br><br>What element produces the beep and what does a store bought dimmer have to do with your problem?</p>
I'm using the zero cross circuit that you propouse in this blog, the dimmer that I have in my room is analog, of those standing on the wall, I think that the triac emits the beep or the lamp I'm not really shure.
<p>TRIACS dont emit beeps</p><p>Not sure what you mean with analog dimmer, but if you are using a store bought dimmer in this circuit I am ot surprised you have problems. <br>1 how do you control it from yr arduino? (i.e. where do the wires go?)<br>2 how do you need to control it? (the dimtime values may mean nothing to yr dimmer)<br>3 can you control it? (maybe it isnt designed to be controlled from a microcontroller)</p>
<p>Sorry, As I said my English is not very good, forget about the analog dimmers, I'm not using that dimmer in the circuit, I think I caused confusion. I'm using de zero cross circuit and de dimmer circuit propouse of you in this blog, sorry again for the confusion</p>
<p>is ok. I was already wondering what a commercial dimmer had to do with it. So that beep that you brought up... from which circuit is that coming?</p>
The beep comes from the light bulb
<p>that surprises me coz lightbulbs generally dont beep.</p><p>Anyway, I might have become sidetracked from your original problem. Is that solved now?</p>
The problem persists, you told me: &quot;It all suggest that somewhere your timing is off...&quot;. I appreciate your help.
<p>OK. I get many questions so sometimes if iot is a bit prolonged I may lose track what the initial problem was.<br>So we can state that the demo program is working, which means that your hardware is working. However when you send an individual value you run into problems.<br>The program you are using is exactly the same as the demo program that is working for you, except for that you replaced <br></p><p>for (int i=5; i &lt;= 128; i++){<br> dimming=i;<br> delay(10);<br> }</p><p> with </p><p> dimming=30;</p><p> delay(10);<br><br>As you can appreciate that is extremely odd as the value 30 and in fact any of the other values that you tried all come along in the loop that actually IS working for you.<br>Additionally, you have a beeping lamp<br>I must say this is all very unlogical. Must oversee something<br>So here is what I like you to do:<br>Load the original demo program again and make sure that indeed works and that the lamp doesnt beep</p><p>then change </p><p>for (int i=5; i &lt;= 128; i++){<br>into</p><p>for (int i=20; i &lt;= 100; i++){<br>and see what that does.<br>If that works narrow the gap to say 30-40 and tell me what that does</p>
<p>OK, my last question: how do you calculate the 30 Ohm? Motorola's 4n25 takes 60mW / 3V and I have the impression that 47 Ohm is better suited for 230V. Am I wrong?</p>
<p>I am not sure what 30 ohm you are referring to</p>
<p>Sorry. On the mains input you have two 30 *Kilo*Ohm resistors. How do you calculate this value? </p>
<p>pls dont overlook my prior reply to this question, but I just wanted to add a bit more. Ideally the resistor value should be as small as possible so the width of the zerocross signal is as small as possible. However, that would lead to a rather high powerdissipation in the resistors.<br>The 4n25 doesnt have 60mW dissipation as you write but max 120mW. However that number is of secundary relevance as we really dont need 40mA to open the 4n25.<br>the rectified voltage of 230 V AC =207 Volt.<br>divided by 60k that gives abt 3.5mA which is enough. Going to 2x47k (not sure where you got that value) would give 2.2mA. That is pushing it a bit. Plus your zerocrosssignal would get wider, giving you timing problems</p>
<p>I think you made a calculation error here. </p><p>The peak to peak voltage after rectification will be 220*sqrt(2) = 311 Volts (not 207V).</p><p>For the purpose of calculating the current, however, you should use the 220V RMS value (because the resistors are on the AC side of the rectifier), so 220/60k = 3.67 mA which is still close to what you calculated, so in the end it does not matter very much.</p>
<p>Thanks. I always used to calculate with 1.4 and it is well possible that 3-4 years ago I used that value to calculate the resistor value based on 1.4x230V.But since then I was told (rectifieing basics on internet) that it should be the average voltage, which was supposed to be 0.9*230. I am not sure if that is true, but as you correctly state: In the end it doesnt matter very much.</p><p>Thanks :-)</p>
<p>by dividing the voltage by the current I wanted to flow.<br>This is a <br>compromise between wanting a decent current to open the phototransistor<br> and a current that wouldnt be too big with regard to the power <br>dissipation</p>
<p>Thank you all for such great projects. I have developed this project and it works all fine. But when I use a sensor with arduino, it gives no response. I think arduino is too much busy in tackling zero-crossings. Can anyone suggest anything about using multiple things with this power control using same arduino board.</p>
<p>seems like instructables is at it again. I answered yesterday, but I cant see my reply anymore. Also I saw you reacted again but cant find that reaction.<br>Anyway I will try again: you probably used the firts program, which is a demo program. It doesnt do much else but wait in a delay.<br>If you want your CPU to do something else, take the 2nd program that uses a timer interrupt to determine that phase cut and leaves the CPU free to do other things</p>
<p>Yet another question. As you write, the zero-crossing detection consumes ~800mW.</p><p>For a 365 days / 24h device that is a lot (thinking at the coal or uranium burnt for that). </p><p>So lets save energy: for the important dim-position &quot;switched off&quot; it might be reasonable to operate mains input by an optocoupler-driven relais that cuts this down to 0mW. Does this seem reasonable to you?</p>
<p>indeed it is a lot. A relay however also draws power, though probably not that much, but it is a needlessly complicated solution. Better try this then: </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>
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>

About This Instructable

474,563views

397favorites

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: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: