Go Down

Topic: AC Light Dimming (Read 10 times) previous topic - next topic


I am working on trying to get this whole dimmer thing going...  I got a Triac hooked up to control a light on one of the Digital IO pins. (and only killed one ATMEGA in the process!)

The problem I am having now is getting the Zero Cross detection working to be able to fire the Triac at the right time.  I have been looking at http://www.andrewkilpatrick.org/blog/?page_id=445 and http://www.hoelscher-hi.de/hendrik/english/dimmer.htm  which have been a great help.  Thanks Guys!

I have some H11AA1 and 4N25 optocouplers.  What was thinking was to have the HOT 120 V AC go through 25ohm 1 W resistor (right now 4 100ohm 1/8w in parallel to test it out) to pin 1 of the H11AA1 and Pin 2 to AC neutral.  Then pin 4 to GND and Pin 5 as an input with a 10K pullup to 5V.

But...  its not working, and I think I might of fried the coupler.  Any ideas?  unfortunately I do not have a oscilloscope handy   :(

Thanks for any help you can give, I love learning this stuff.


I think you missed the "K" in the resistance stated in Andrew Kilpatrick's circuit. He has two 47K ohm resistors in parallel( 23.5Kohm equiv ) and not 25 ohm.  Try 4 100K ohm resistors in parallel.



Stupid me!  I just want to CrapShack and got some 1/2W 47K and it seems to be working now...  I will post the code when I am done to show how it is working.


Ok, so I was able to get the zero cross detection working.  Now I am trying to do the light dimming and I am stuck.  I know i need to fire the triac at a certain time past the zero cross.  There are 120 zero crosses per second (for 60Hz power) that gives 8333 micros between crosses. If I use 128 steps that is 65 micros per step.  So if I want 50% power I would need to fire the Triac at 65*64 micros after the zero cross??

That being said...  I would think this would work, but with no luck...

Code: [Select]

int dim = 0;

attachInterrupt(0, zero, CHANGE);

void loop()
 while(dim<128) {

void zero() {
 digitalWrite(dimmer1Pin, HIGH);
 digitalWrite(dimmer1Pin, LOW);


don't count on the freq being 60Hz and instead, keep track of the 0-xing period yourself either every 0-xing or for some reasonable period(every couple of minutes maybe ).  This way you're sure what your period is and then do the math to set what a percentage point is for that period.

I didn't look at the code but it seems your stated logic is right. For a 60Hz freq 1/60 is the full cycle period, so 1/2 that is the half cycle 0-xing period and it should be in the 8mS range. You divided your 0-xing period by the resolution(128) to get what part of the 0-xing period a 'tick' is. For a 50% cycle you'd want x number of ticks which when multiplied by the 'tick' period is closest to 50% of your 0-xing period.

Cool you got things close and closer to fully working.


Go Up