Go Down

Topic: AC dimmer 220V 50Hz (Read 6707 times) previous topic - next topic


I have schematic:

and code:

// AC dimmer with zero-crossing detection
// First edition by Niels Oestergaard
// !!!!!!!! not verified !!!!!!!!!!!!!
// based on design Andrew Kilpatricks tutorial: http://www.andrewkilpatrick.org/blog/?page_id=445

//strategy for dimming part of the sketch:
// set counting interval to reach 255 in one half-period (50 hz = 10 millis pr. half-period / 60 Hz 8.3333333333333 pr. halft periode )
// set output to 0 - 255
// detect zero crossing (on interrupt pin)
// in interrupt rutine calculate time for turning on
// when millis reaching time, turn on for a short while
// turn output off to be sure not to trigger to early in next halfperiod

// interrupt 0 - (pin 2)

int dimmer1Pin = 10;         //output to opto triac (MOC 3010/3020)
int ledPin = 13;             //reserved for sanity check and visual feedback
volatile int freqAdjTime = 20;   //set to 10 for 50 Hz, and 8 for 60 Hz
volatile long nextOnTime = 0;    // variable for storing a time (in millis) for when to fire the output next
volatile int outputValue;      // Varialble for storing the output value 0-255
//unsigned int inputValue;     // Variable for storing input value (ie. a potentiometer)
long lastMillis;             // for timing purposes

void setup()
  pinMode(ledPin, OUTPUT);    // sets the digital pin as output
  // sanity check
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin, LOW);
  // end of sanity check
  attachInterrupt(0, zero, RISING); //Attachment of zero crossing detection, which means the interrupt rutine "zero" is called each 20 millis

void loop()
  if (nextOnTime < millis()) {       //is it time to set pin on?
    digitalWrite(dimmer1Pin, HIGH);    // then set it
    delay(2);                  // wait a bit to be sure the triac is on
    digitalWrite(dimmer1Pin, LOW);     // turn of the trigger to be sure it is not trigger in the very beginning of next halfperiod

// A loop that slowly increases outputvalue
if (millis()-lastMillis > 250 ) {     //increase output each fourth of a second
    outputValue++;                //increase outputvalue by one

    //Check if max is reached
    if (outputValue  > 255) {         // output = maximum
      outputValue = 0;            // set output to zero

void zero()
// set a volatile variable with the millis value + (the value of the desired output (0-255) multiplied by the freq adj time)
nextOnTime = millis() + (freqAdjTime * outputValue); //how long will this calculation take? too long to enable low output??

But it's not working :(

Please help...


What do you mean by "it's not working" 
What type of load do you have connected to the lower pair of terminals (on the right of the drawing)


not working = does not change the brightness

load - is lamp 60Watt

p.s. sorry for my english


What debugging have you tried?
Are you even getting interrupts?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


The 4N35 is probably damaged.  You need a regular diode in the reverse direction across the 4N35 LED to handle the reverse voltage.

The 82K  resistors need to be 1 watt rated; total dissipation is 2.2W

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info


why damaged?

can you give me schematic?


why damaged? can you give me schematic?

Look at your schematic.  The 220V AC Waveform goes both + and - .  When it is + the 4n35 LED conducts in the forward direction at about 10 ma.

BUT what about the opposite polarity? The reverse voltage across the 4N35 LED will be about 220 * (Square root of 2 = 1.414) or about 311 volts.  The 4N35 LED is only rated at about 10 volts reverse voltage, so it has almost definitely broken down electrically.

You need to put a diode across the 4N35 LED in the reverse direction. It will conduct and protect the 4N35..

Try it. Also, check the output of the 4N35. Does it ever change now??

OH: Other problem? The MOC3061 is, I believe, a "Zero Crossing" type, so it will not work in this application. Check the data sheet..
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info


I'd also suggest you replace the 82k mains resistors by type y capacitors rated for mains voltage.  They will offer suitable current limiting impedance with zero heat output (current is out of phase with voltage or cos phi = 0)

If you want 41kohms at 50Hz the  capacitor values should be a nominal 0.1uF  (uF meaning microfarad)


Nov 24, 2012, 07:11 pm Last Edit: Nov 25, 2012, 05:19 pm by Tesla_pow Reason: 1
Could you repost your schematic KNYAZ 2020  ? Can't see the schematic you've posted some time ago,
I've tried out some other circuits but this one I think is close. http://postimage.org/image/epi813wmx/
Thing is, in the simulation program there is just MOC3021 and I've tried out your code in my simulation. Looked at the
datasheets for 4N35, MOC3021 and the triac BT139. Tried adjusting the forward and holding currents on triac manually
but with no luck. The bridge rectifier is a 400V 1A,and triac is currently standing on 1mA forward and 5mA holding current. Someone who can help me on this ?


To be honest, I do not fully understand the code that you have written, but you may antto have a look here:
That has a working circuit+software

The circuit is probably not so much different from what you are using.


further to my post of just now, the MOC3061 will certainly not be working in this circuit coz it is a zero-crossing optocoupler and that is exactly what you NOT want, as you pick up your zeroXing from the 4n35.  Use a MOC3020 instead.

Again, have a look here: http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/

Go Up