[Solved] Trying to port an AC Dimmer circuit from Arduino to SparkCore

I am trying to port an AC Dimmer Circuit to SparkCore… I made several changes to code which i felt was necessary including changing usage of timer as you can see below…
Its working perfectly fine with Arduino Uno but when try with spark core using modified code, I run into trouble…It does’t work. I made no changes to circuit…only in program and I/O pins…

This is the link to project…http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/

This is the circuit…

The code for ARDUINO UNO WAS
CODE::

  #include  <TimerOne.h>          
    volatile int i=0;               // Variable to use as a counter
    volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
    int AC_pin = 11;                // 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 freqStep = 75;    // This is the delay-per-brightness step in microseconds.
                                // For 60 Hz it should be 65
    // It is calculated based on the frequency of your voltage supply (50Hz or 60Hz)
    // and the number of brightness steps you want. 
    // 
    // Realize that there are 2 zerocrossing per cycle. This means
    // zero crossing happens at 120Hz for a 60Hz supply or 100Hz for a 50Hz supply.  
    // To calculate freqStep divide the length of one full half-wave of the power
    // cycle (in microseconds) by the number of brightness steps. 
    // (120 Hz=8333uS) / 128 brightness steps = 65 uS / brightness step
    // (100Hz=10000uS) / 128 steps = 75uS/step
    
    void setup() {                                      // Begin setup
      pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
      attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 
//(interrupt 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 occurred
      i=0;
      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() {                        
    
    }

Instead of TimerOne Library i used SparkIntervalTimer Library
Here is the modified PORTED code…

#include "SparkIntervalTimer/SparkIntervalTimer.h"

volatile int i=0;               // Variable to use as a counter
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = D7;                // 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 freqStep = 75;    // This is the delay-per-brightness step in microseconds.
void zero_cross_detect(void);
void dim_check(void);
                      // For 60 Hz it should be 65
// It is calculated based on the frequency of your voltage supply (50Hz or 60Hz)
// and the number of brightness steps you want. 
// 
// Realize that there are 2 zerocrossing per cycle. This means
// zero crossing happens at 120Hz for a 60Hz supply or 100Hz for a 50Hz supply. 

// To calculate freqStep divide the length of one full half-wave of the power
// cycle (in microseconds) by the number of brightness steps. 
//
// (120 Hz=8333uS) / 128 brightness steps = 65 uS / brightness step
// (100Hz=10000uS) / 128 steps = 75uS/step
IntervalTimer myTimer;

void setup() {                                      // Begin setup
  Serial.begin(9600);
  pinMode(D2,INPUT);
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  attachInterrupt(D0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 0
          // for Zero Cross Detection
  myTimer.begin(dim_check, freqStep, uSec);    
   
}

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

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() { 
    Serial.print(digitalRead(D2));
 

}

I added the serial commands to check input to D0…
PLEASE HELP…
Also let me know if need more information…

@jiyojolly, if +Vcc in the diagram is 5V, you cannot use D2 as the zero-cross input since it is NOT 5V tolerant! The GPIO pin should be OK. Instead you can use these pins:

D0, D1, D3, D4, D5, D6 and D7

Second, I don’t suggest you use D7 as the triac control since it already has the small blue onboard LED connected to it. The dimmer circuit should work at 3.3V but the 220 ohm current limiting resistor and LED may be too much voltage drop for the triac opto LED to turn on. The rest of the code looks fine. Make the recommended changes and let me know how it goes. :smiley:

2 Likes

ok let me make the changes…thanks for the quick reply… :smile:

1 Like

I remember others having issues with this a few months ago, i think they got it sorted

have a look here

1 Like

Hey got everything to work…The problem was with the code…i messed up pins…I initialized the wrong i/o port…Thanks for the heads-up…

1 Like

Hi jiyojolly!

Could you share with us what was wrong in your code in order to be able to use it?
Is it the following line the one that need to be changed (i.e. the pin D0 to be changed to D2) or are you using other ports?

attachInterrupt(D0, zero_cross_detect, RISING);

Would be nice seeing the final working version of your project code!
Many thanks in advance…

M.-

Is it necessary to use bridge rectifier in the circuit.

@imPawan, yes otherwise you will only get half the zero crossings! The full wave bridge creates two “above zero” waves for every cycle, turning on the LED twice. Without the bridge, you would only one “above zero” wave per cycle with the LED only turning on once. The LED on the opto-isolator will also suffer a large reverse voltage without the bridge, which will most likely damage it.

is it safe to use the bridge rectifier without a stepping down transformer ?