220 VAC dimmer, clarifying questions from a newbie

Dear fellow Arduino lovers,

I have been reading these forums with pleasure and I am so excited about how helpful you guys are! The threads on these forums are extremely helpful for a newbie like me. I have been reading much about AC dimming with the Arduino and I have come across some working examples. Though I am left with some questions that I hope you guys can help me with.

Just to clarify: I'm not an electrical engineer at all. The last physics class I had was 7-8 years ago and I only know the very basics (such as Ohm's Law, resistors, electromagnets, etc.). It would therefore be extremely helpful if you could really spell it out for me.

So the solution I have been looking at is this: Arduino Forum

I have a basic understanding how optocouplers and TRIACs work but just to make sure I will try to explain what's going on in that circuit and hopefully you can correct me where I'm wrong. I will be referring to specific parts of Fleckz' circuit so please check out this image where I have highlighted some areas:

Here is Fleckz' code:

int AC_pin = 10;           //Pin to MOC3020 - 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() {

}

Okay, here goes. So the AC power source (220v, 50hz) flows through the DC rectifier and creates a full-cycle direct current in the yellow part I have marked in the above image. That activates the optocoupler OK1 which sends an analog input signal to ARDUINO_PIN_2. This signal will interrupt on FALLING, which would only happen when I turn off the AC power source or if I pull out the AC power chord. Is that true?

The reason why we need to put in the 45K resistors (in the red rectangle) is because the 4N35 optocoupler only needs/accepts 5mA (220/45000), right?

On a FALLING signal from ARDUINO_PIN_2 we run our code (the light function). We control our light switch from the serial debugger window, so it will only react to new serial data input, right? We have 8 bits (256) degrees of resolution and can dim the light bulb so accordingly. The way the dimming then actually works is by sending either a HIGH or a LOW to the MOC3020 (OK2) and then insert a delay that is shorter when we set the dimmer to 255 (i.e., more light from the light bulb) and longer when we set the dimmer to 0 (i.e., less light from the light bulb). Is this correct?

Then finally I am puzzled about the BT139's function. My understanding of a TRIAC is the following: The more current that comes through the gate of the TRIAC, the more current is allowed to flow through the TRIAC (i.e., from A1 to A2). In that way the TRIAC works as a variable resistor that depends on the current flowing in from the gate. Correct? Well, wouldn't the flow of electrons not always just flow through the blue part in the schematics above. That is, directly from the MOC3020, through the 330 ohms resistor, to the load (the light bulb) and, this way, bypass the BT139?

I think that I need to work out the math to fully understand this. I would really appreciate it if you guys could help me out with that.

I know I have got a lot of questions but if you could just help me with some of them I would be extremely happy. Thank you so much!

sebastianbk:
Okay, here goes. So the AC power source (220v, 50hz) flows through the DC rectifier and creates a full-cycle direct current in the yellow part I have marked in the above image. That activates the optocoupler OK1 which sends an analog input signal to ARDUINO_PIN_2. This signal will interrupt on FALLING, which would only happen when I turn off the AC power source or if I pull out the AC power chord. Is that true?

Well, as you can see, there is no capacitor after bridge rectifier, so the DC will jump up and down at 100Hz

sebastianbk:
The reason why we need to put in the 45K resistors (in the red rectangle) is because the 4N35 optocoupler only needs/accepts 5mA (220/45000), right?

True!

sebastianbk:
On a FALLING signal from ARDUINO_PIN_2 we run our code (the light function). We control our light switch from the serial debugger window, so it will only react to new serial data input, right?

Yes, in this example - true!

sebastianbk:
We have 8 bits (256) degrees of resolution and can dim the light bulb so accordingly. The way the dimming then actually works is by sending either a HIGH or a LOW to the MOC3020 (OK2) and then insert a delay that is shorter when we set the dimmer to 255 (i.e., more light from the light bulb) and longer when we set the dimmer to 0 (i.e., less light from the light bulb). Is this correct?

Then finally I am puzzled about the BT139's function. My understanding of a TRIAC is the following: The more current that comes through the gate of the TRIAC, the more current is allowed to flow through the TRIAC (i.e., from A1 to A2). In that way the TRIAC works as a variable resistor that depends on the current flowing in from the gate. Correct? Well, wouldn't the flow of electrons not always just flow through the blue part in the schematics above. That is, directly from the MOC3020, through the 330 ohms resistor, to the load (the light bulb) and, this way, bypass the BT139?

No, the "problem" with triac's is, that you can only turn it ON or OFF

This signal will interrupt on FALLING, which would only happen when I turn off the AC power source or if I pull out the AC power chord. Is that true?

No, the signal will interrupt once very half cycle of the mains, just before the zero crossing (or when you turn off the AC power or pull out the cord). That is the purpose of the signal: to tell the Arduino when the zero crossing is about to happen.

The more current that comes through the gate of the TRIAC, the more current is allowed to flow through the TRIAC (i.e., from A1 to A2).

No, that description is for a BJT, not a triac. The triac will turn on when it receives gate drive, which will happen when the Arduino drives current through the MOC3020. It will turn off at the next zero-crossing of the mains.

Now perhaps you can work out why changing the delay between the zero crossing and generating the gate signal allows you to control the dimming?

You want to be very careful with mains electricity.

You don't want to be doing operations on the serial port during the interrupt service routine ( your function light() ).

Move the code which checks the serial input, into loop(). You then set a variable for the current requested
level. The interrupt service routine then just accesses that variable to do it's thing.