AC dimmer wont work

Im trying to create AC dimmer (230V motor controller) and it wont work.
It wont responding corectly to my triger signal from arduino (flickering and some noise at default level).

Please check my wiering and code and help me out…

#include  <TimerOne.h>          // Avaiable from
volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 3;                // Output to Opto Triac
int dim = 0;                    // Dimming level (0-128)  0 = on, 128 = 0ff
int inc=1;                      // counting up or down, 1=up, -1=down

int val = 0;  // variable to store the value read
int freqStep = 75;    //75 This is the delay-per-brightness step in microseconds.
                      // For 60 Hz it should be 65

void setup() {                                      // Begin setup
  pinMode(AC_pin, OUTPUT); 
  attachInterrupt(digitalPinToInterrupt(2), zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  Timer1.attachInterrupt(dim_check, freqStep);      
  // Use the TimerOne Library to attach an interrupt
  // to the function we use to check to see if it is 
  // the right time to fire the triac.  This function 
  // will now run every freqStep in microseconds.                                            

void zero_cross_detect() {    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)

// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=dim) {                     
      digitalWrite(AC_pin, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    else {
      i++; // increment time step counter                     

void loop() {                        
  if((dim>=128) || (dim<=0))
  if (Serial.available() > 0) {
    int a = Serial.parseInt();
    if(a>0){dim = a;Serial.println(dim);}

Thank you


Ops diag, both files the same image.

Tom... :slight_smile:

Changed. Sorry :confused:

Your zero crossing circuit has problems.

You need to swap the two connections between the diode bridge/4N35. As shown, the polarity is backwards. Also, the choice of 330k resistors is awfully high, something closer to 47k each would make more sense for 230 v mains (about 3.5 ma through the diode with a 100% CTR) Make sure the resistors are rated for the voltage and power applied as power is approximately 1 watt. Use series/parallel resistors to increase the ratings if required.

Thanks for answer. Yes, this mistake with polarity is solved allready and it wont work. I have make a meassurment with oscyloscope on MOC3021 + pin and is working (trigger time is changing as expected)…