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>Hello,</p><p>I want to dim a 12v halogen lamp. I have a dimmable halogen psu, but I have to drive it with a TRIAC, from what I understood.</p><p>Is this circuit suitable for my needs?</p>
<p>You mean that it is a 12 Volt halogen lamp that is attached to a PSU that goes into the grid? and that PSU is labeleld as 'dimmable'.<br>If it is for a traditional TRIAC dimmer then you will have a good chance that this circuit will do the job </p>
<p>thanks for your reply,</p><p>I tried it with my configuration, but seems that my electronic psu prefers trailing-edge dimming. I get a strange behaviour of the halogen lamp while a normal lamp works great when attached directly to your circuit.</p>
<p>I have try the code for &quot;AC Voltage dimmer with Zero cross detection&quot; , I only can see the light glow and dim again and repeat again. Is this the result ? </p><p>for the code by using push button, I only see the light is flickering.</p>
<p>then most likely a hardware problem. Please check and recheck all yr connections</p>
<p>can I use just one &quot;bridge + 4N25 circuit part&quot; to get the zero cross for supply the arduino, and then use separate MOC3021+TIC206 circuit part for dimming each light? or its necesary the whole circuit for each light?</p>
<p>you only need one circuit for the zerocross interrupt.</p><p>if you want more lights you only need to copy the MOC3021+Triac part.<br>have a look at my <a href="http://www.instructables.com/id/3-channel-Dimmerfader-for-Arduino-or-other-microco/">3 lamp dimmer</a>.</p>
<p>thanks for reply, just 2 more questions, this circuit is better (dutty cycle/cost) than mosfet one you optimize, for a xmas light show? (I planned to run several hours)<br>wich kind of lights can be used? normal classic ones? rice type? led type?</p><p>note: I'll not put more than 2 strips together for each TIC206 part</p>
<p>I think that if you have those normal classic xmas lights in series on a string, then it doesnt make much difference which one you are using. As long as you keep the TIC cool it can take quite a load. If it are lights that work with a transformer than the PWM circuit is not suitable.<br>With a transformer the TRIAC circuit is not ideal but it will most likely do the job.<br>Merry xmas</p>
<p>I made it. </p><p>https://youtu.be/N_Zx1xARwtM</p>
<p>looks good sureshmali. Obviously you added a bit too it :-)</p>
<p>here is my schematic, after i double check the coding and circuit, the lamp still glow at 100%, i dont know what is happening here, my component is 4n26, moc3021, Triac bt136 and bridge rectifier. You know what is happening here? Please help me</p>
<p>circuit looks OK. I am not sure if I asked before but did you try the original dimmer demo from the instructable? did that work properly? That would be very helpful to see if the problem is in yr software or your hardware</p>
<p>I see , but I have try the original dimmer code already , I just saw the light flickering .</p>
<p>well that strongly suggest indeed that the problem is in your hardware. I cannot see anything wrong with yr circuit. When you tried the original program, did you include the LCD that I see in your schedule? Or was it just the program without any additions?<br> Could you recheck all connections and soldering joints?</p>
<p>Thanks for sharing! I have two questions related to your schematics.</p><p>Can I simply omit the bridge rectifier and still use a 4N25? The internal diode would block negative signal, and I would think software can handle it (especially as I plan to do only bursts of full-waves, not chopping them at a higher frequency).</p><p>Also why do you use two 30K resistors where only one 60K would seem to be enough? Thanks again :)</p>
<p>you would only have one phase and therefore you wouldnt have a zerocross, just a signal that lingers at zero for half of the cycle time.<br>You can use 1 60k but it would need to be of the double wattage that the 30k resistors are. That is one reason, The other is that by using 2 resistors the full 220 signal would be kept out of the PCB as much as possible<br></p>
Got it, thanks for your reply :)<br>The raising edge would trigger an interrupt at the beginning of each cycle then (and it is no more a zerocross indeed!).<br>
<p>Well ofcourse the signal doesnt need to go through zero ofcourse, just be low enough to open the 4n25 and if you trigger on the rising or falling flank you would still get an interrupt, but you would only get an interrupt for half of the cycle</p>
<p>What about false zero cross triggering due to other Triacs firing at different phases? </p>
<p>I do not understand what you are exactly asking</p>
<p>When using several channels of the same circuit to dim several loads and each load is at different value.</p><p>In this case when you fire the Triac it will introduce some noise on the mains.</p><p>This noise may be false detected as a Zero cross by the zero cross circuit, and case flicker .</p><p>Did you face such problem before?</p>
<p>no I didnt have any of those problems. It is important though to have a clean PSU as the grid is full of noise whether Triac induced or not.<br>Anyway, I never had any problem</p>
<p>Can i have a code for up down button control light dimmer ?</p>
<p>i have sent you already when i first received your email. check yr spam folder maybe. but will also send again</p>
<p>I receive it already..thanks :)</p>
<p>turns out i sent it to you on the 14th. 5 days ago. I just resent it</p>
<p>This is my email : joshuasamuel@klt.edu.my</p><p>I see, this is the problem i need to fix it, my final year project is to use the button to dim the light bulb, and i also use the LCD display to display the dim level. But after i power on the supply, the bulb only glow 100%, the circuit connection is right, so i guess is the code problem (T.T) . I hope you can help me.</p><p>I still waiting for you to send me the original code. Thanks :)</p>
<p>Hello! Im a student and I have basic knowledge of electronics and the arduino. I was thinking if it would be possible if I add sensors while using this circuit for dimming. Particularly, pir sensor and photodiode. Moreover, i wanted to also add bluetooth module for additional manual control of the led bulb. Is it possible or is it too much for the arduino to handle? I have lots or questions for this project because its very nice and i want to add more features. thank you. kudos also to your very detailed instructable</p>
<p>it is not too much to handle for the arduino, but just know that the first program is a demo program that spends most of its time in a delay loop. If you want to do something else you need to replace he delays with some sort of timer function. Also in writing yr program keep in mind that there is an interrupt 100 times a second</p>
<p>after i used the code for up down button, the light is constantly on 100%, what is happening here ?</p>
<p>johsim in the time I wrote this instructable I thought it would be beneficial to people if I added some codes besides the basic program. But it is a long time since I tested that specific program as I didnt want to use any buttons. Now it is very well possible that in putting the code on this webpage something went wrong. I took a quick look and it seemed OK, but If you p, me your email address (or leave it in the comments if ok with you. I will send you the original code. That however also has an LCD section in it but you can just ignore that.</p>
<p>This is my email : joshuasamuel@klt.edu.my</p><p>I see, this is the problem i need to fix it, my final year project is to use the button to dim the light bulb, and i also use the LCD display to display the dim level. But after i power on the supply, the bulb only glow 100%, the circuit connection is right, so i guess is the code problem (T.T) . I hope you can help me. Thanks </p>
<p>Hi to everyone,</p><p>I built this circuit because I was interested like many in controlling a 230 V AC Motor Fan.<br>First built for simple loads like a light bulb worked well and than for complex loads (inductive) like a motor. So I used the circuit with the snubber. Works well for i think 10 minutes than it starts to burn ;). Better said the 180 Ohm Resistor in the snubber circuit is to small and it will catch fire. I haven't done any calculations for the snubber yet but i will keep it up to date and try a different resistor. <br>Just wanted to warn people and I really recommend a heatsink for the Triac. <br>And I used an analogPin instead of the digital interrupt (got problems with it do not know why, but works well with analog Pin).</p><p><br>So I will make an update in the next few weeks for the snubber.</p><p>Anyway great circuit :)</p>
<p>thanks<br>Depending on the load yes a heatsink is advisable.<br>Snubber circuits indeed need to be able to deal with the spikes going through them.<br>I am surprised you had to resort to the analog pin, you must have changed the program then as there is no interrupt on the analog pin, but ofcourse polling is always possible</p>
<p>What is the minimum load on this dimmer ?<br>I have a parallel set of 4 dimmable led bulbs (5.5 Watts max each, 230V AC), will that work ?</p>
<p>the minimum load I tried was 40 watt and that was without problem. whether you can dim LED lamps largely depends on the LED lamp. Many dimmable lamps need dedicated dimmers, but if these are LED lamps that are for what is called a 'traditional' dimmer you have a good chance of it working</p>
<p>I made this for five bulbs . but there is a flickering. flicker rate is not constant, flicker rate change with the time and its looks periodic. its increase with time and then decrease. then flickering stop, after about 1 second its start flicker again. can you please tell me the problem. its not the number of bulbs i have used, even with one bulb there is flickering . i changed the resister values from 33k to 16.5k, detect zero cross using a 12v transformer, replace optocouplers. changed the 4n25 pull up resister from 10k to another value. nothing helps. flickering rate and pattern remaining same . can you please help me. thank you for this great instructable .</p>
<p><a href="http://www.instructables.com/member/%E0%B6%85%E0%B6%BA%E0%B6%B1%E0%B7%8A%E0%B6%B8">අයන්ම</a> I am not sure if you saw my 3 channel light dimmer, but that is just for your information.<br>There can be various reasons for flickering: software and hardware, so lets see where the problem is. I presume you have juist built the triac circuit 5 times and the zerocross one time.<br>What happens if you ignore the 4 channels and just use one and use my demo program? What happens then?</p><p>Flickering coz of hardware is usually because of a dirty psu. What happens if you feed your microcontroller from a battery?</p><p>I presume you used the 16.5k to get a smaller pulse, but apparently that was not the solution. Though you can use a transformer to get the pulse off but the quality of the transformer is of importance.</p><p>Do you have flickering over the entire range? Is there also flickering if you write one value to the optocoupler? say, 70.</p><p>Disconnect your microcontroller and put 5 volt on the entrance of your circuit. Do you have flickering too? </p><p>Anyway.. try that and try also feeding the microcontroller with a battery </p>
&quot;Disconnect your microcontroller and put 5 volt on the entrance of your circuit. Do you have flickering too?&quot; I didnt understand this. What do you mean by entrance
<p>the entrance is where you would normally put the output of the arduino</p>
<p>i tested it with your sample code ,which does not use the timer interrupts. it worked fine without flicker. flicker is only visible with the timer interrupt code. please help me to solve this problem. thank you. </p>
<p>Are you using an Arduino? which one?</p>
Yes. An Uno
<p>The Timer program as you can see in the legenda is not made by me, but it sounds as if somehow your timing is abit off.<br>Now there are two timer programs I think, in which step is the one you are using?<br>I am just going to make sure the timer is working correct</p>
Yes.i think the problem lies with intenal and external inturrupts. When we use both in same sketch it doesnt work well. Thats may be the reason when we use delay for dimming,the flickering doesnt apper. I have increased the number of brighness steps for 320. I called the ISR with 30microseconds delays and the flickering reduced to almot zero. To reduce the time concumed for ISR i used Direct port manipulation insted of digitalWrite and pinMode.
<p>ok so you did change rhe sketch, then not much use i check the one here. Direct port manipuation is good, makes it less portable but not really a problem for dedicated project. Make sure you dont make the interrupt routines too long</p>
I sloved the problem completely. Thanks for the help.
<p>great. good to hear</p>
Mr. Bloke. Did you run the circuit with the timer inturrupt code. And did it work smoothly? .

About This Instructable

520,751views

422favorites

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:BruteForce codefinding for Infrared RGB LEDstrip Using the 4 pins of the ESP8266-01 Adding the BMP180 to the ESP8266 
Add instructable to: