I use an Arduino Uno for controlling one DC motor speed with PWM and one dimmer for control heater. The dimmer and PWM control worked standalone, but when I want them to work together, the dimmer doesn't work.

#include  <TimerOne.h>          /
#include <Wire.h> 

 //////////////                    //pin 2 zero kesmesi icin
int isiticiPWM = 9;                //  Opto Triac isitici PWM icin
int fanPWM=11;                     //motor pwm pini

int hiz=0;

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

char   data ;
int dim = 255;                    // Dimming level (0-128)  0 = on, 128 = 0ff
int inc=5;    // counting up or down, 1=up, -1=down
int freqStep = 75;    // This is the delay-per-brightness step 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(isiticiPWM, 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(isiticiPWM, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    else {
      i++; // increment time step counter                     
void setup() {  

  pinMode(isiticiPWM, OUTPUT);

    attachInterrupt(0, 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);   


void loop()

for(int k=0;k<=255;k++)

  • hiz=450;analogWrite(fanPWM,hiz); -- PWM goes from 0 to 255. 450 is too high. – Majenko Mar 11 '19 at 14:47
  • Your loop function doesn't make sense to me. You are ramping dim from 0 to 255 in full speed. I guess you want dim to be the currently set time to turn on the triac. Most likely dim will change so fast, that the Timer interrupt does not have time to actually apply the value. Somewhat a racing condition. – chrisl Mar 11 '19 at 15:10
  • Zero crossing is used for AC to detect when the alternating current is as close to zero as possible so when a TRIAC/MOSFET doesn't generate a lot of transients! en.wikipedia.org/wiki/Zero_cross_circuit – MatsK Mar 11 '19 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.