will be read-only starting December 31st, 2018. For more info please look at this Forum Post

AC Power Control Library with PWM Technique and Zero Cross Detection

This is a library for performing AC power control with Pulse Code Modulation (PWM) and zero cross detection. With the use of power electronics (TRIACs, SSRs, e.t.c.) you can control the power of an AC load, like lights (dimming), audio, fans, motors, heating elements, ovens, kettles e.t.c. The library was designed with the maximum of simplicity and minimality in order to barely affect the size of your sketch.

Reviewed: 2013 - Dimitris El. Vassis

License GPLv.3

Download: ACPWM Library

Installation - Use
Unzip and place the extracted folder inside the libraries folder of your Arduino setup. At the start of your code, use the following statement to include the library in your sketch: #include <ACPWM.h> Use the methods provided with the following statement: ACpwm.method(); where method() is the name of the method you want to use.

Supported methods
void initialize(int frequency, int zeroControlPin, int zeroPulseMode, int pwmPin, int scale);
Initializes the power control operation by setting the mains frequency, the pin of the zero cross detection pulse, the mode of the pulse (LOW, CHANGE, RISING, FALLING), the pin that makes the pwm control, and the scaling of the set point.

void setLatch(int onLatch, int offLatch);
Sets the latching due to response of electronic devices, in the HIGH and LOW state of the zero control pulse respectively.

void setDutyCycle(int dutyCycle);
Sets the duty cycle (ON period) of the PWM operation.

Example program
The following program uses a potentiometer, a zero crossing detector and a switching unit (e.g. TRIAC) to dim a light with the use of Arduino.

  3.  /* AUTHOR: Dimitris El. Vassis - 2013 */
  5.  #include <ACPWM.h>
  6.  #define ZERO_PIN 2 //Input pin from zero cross detector
  7.  #define PWM_PIN 9 //Output pin to TRIAC / SSR
  8.  int SET_PIN = A0; //Analog pin for setting the dutyCycle value with a pontentiometer
  10.  void setup()
  11.  {
  12.     pinMode(ZERO_PIN, INPUT);
  13.     pinMode(PWM_PIN, OUTPUT);
  14.     //Initialize PWM operation.
  15.     //Mains frequency: 50Hz.
  16.     //Zero crossing point reached whenever pulse to PIN2 changes
  17.     //Duty cycle = 0..255. 0:always off. 255: always on. 150: 59% on.
  18.     ACpwm.initialize(50,ZERO_PIN,CHANGE,PWM_PIN,255);
  19.     //Latching on HIGH zero state: 3 microseconds.
  20.     //Latching on LOW zero cross state: 5 microseconds.
  21.     ACpwm.setLatch(3,5);
  22.  }
  24.  void loop() {
  25.     //Adjust the power with the potentiometer
  26.     //Set the duty cycle equal to the value of the potentiometer.
  27.     ACpwm.setDutyCycle(analogRead(SET_PIN));
  28.  }

More info
For full documentation, go HERE.
For background information and other useful links on ac power control with Arduino, go HERE.