AC PWM Library for Atmel Atmega8/168/328 AVR microcontrollers and Arduino

This library was created for tuning AC power with Pulse Width Modulation (PWM technique) and zero cross detection using Atmel® Atmega8/168/328 AVR microcontrollers and the Arduino® prototyping board.

Download the library and start using it, or see below for more details.            download

 

Instructions of use

Documentation

Example of use

Latching adjustment

 

Background information 

What is AC power control and what do we need to achieve it? 

What is PWM and how can we control AC power with that? 

What is zero cross detection? 

Why do I need the microcontroller?

Useful links 

 

What is AC power control and what do we need to achieve it?

Mains power is an alternative current (AC) with a sinusoidal wave of a specific frequency (50 or 60Hz), and a specific amplitude (from 110 to 240V). Parameters are specific for each country (see here). Of course, we are talking about one phase power. 3 phase power is a different case, not considered here.

Suppose we have a load (e.g. a lighting bulb) connected to mains power. How do we make the bulb light less? The answer is that we must feed the bulb with less power than the maximum it gets. And that is exactly what AC power control means; The means of regulating (tuning) AC mains power with an efficient manner in order to limit the energy provided to a load connected (e.g. lights, thermal units, audio devices, motors  e.t.c.).

In order to build a circuit that controls the AC power with PWM, your will need:

 

What is PWM and how can we control AC power with that?

The first way to do that is to shrink the amplitude of the power. In other words, lower the voltage. This can be done by adding a variable resistor in series for example. But this has the disadvantage that the power consumed is actually the same (the sum of the load power and the variable resistor power).

The second, and most effective way is to tune the frequency. Suppose we add a switch between the light and the mains power socket, in order to close the switch (light on) for 5 seconds and open the switch (light off) for another 5 seconds sequentially. Then, the power we consume is 50% of the maximum power consumed. If we do that with a frequency as high as the human eye cannot understand, then we have the bulb light with 50% luminance. Believe it or not, this is how dimmers work.

The rapid switching of the light is achieved with the use of a driving, periodic pulse. When the pulse is OFF, the switch is open and the light is OFF. When the pulse is ON, the switch is closed, and the light is ON. Depending on the width of the pulse, we can tune the percentage of time the switch is OFF and ON state. And this is exactly what AC power control with Pulse Width Modulation is. In the following figures, we can see three different ways to limit the power to 1/3 (33%) of its maximum.

 

In all cases, the percentage of OFF period is 2/3, and the percentage of ON period is 1/3. What is different is the period of the pulse. In the first figure, this is 6 AC waves. In the second figure it is 3 AC waves, and in the last figure it is 1/2 AC wave. Respectively, the duration of the ON time is 2 AC waves in the first case, 1 AC wave in the second case, and 1/6 AC wave in the third case. The duration of the ON period of a PWM pulse is called duty cycle, and it is a vital parameter for the power control.

The only thing we need now is a switching element that can stand enough power and can be switched fast enough, in order to get what we expect. These elements are called solid state devices. Among them, opto-TRIACs and Solid State Relays (SSRs) are the most popular for one phase AC power control. These devices have a contact which makes the switching of the load, and a gate, which closes / opens the contact, depending if voltage is applied or not, respectively. Well, things are a little more complicated, so we recommend some background reading on what TRIACs and SSRs are and how they work.

 

What is zero cross detection?

Considering the above, one would say that the faster we switch a load, the better the results. So, we can switch the mains many times in an AC wave period. Well, this is generally true, but there are some limitations. When we switch off AC mains at a point in time where the amplitude of the sinus wave is high, then spikes and EMI (electromagnetic interference) are created. Hence, the optimum point to switch off an element is when the amplitude of the mains sine wave is near zero. This point is called zero crossing point, and happens twice in every AC cycle. This limits us to switch the mains only once every half sine wave. Furthermore, this limits the frequency of the driving pulse to 2 times the frequency of the AC mains. This is the third case of the figure above.

However, another problem no rises; How do we know when the amplitude of the sine wave is zero (zero crossing point)? This is exactly the meaning of a zero crossing detection circuit. It is a circuit that is fed with mains power, and low voltage (e.g. 5V) DC pulse at every zero crossing point. The pulse can be momentary (e.g. delta pulse) or square wave pulse. Note that, in most cases, zero cross detection is necessary for avoiding unexpected situations. There are many patterns available for building a zero cross detection circuits easily, with low cost, using diodes and opto-couplers. Some examples can be found herehere and  here. There are also ICs for this purpose, like the Atmel® AVR182 zero cross detector.

 

Why do I need the microcontroller?

The microcontroller is the ‘smart’ element that performs the whole operation. It takes an input from the zero crossing detector and produces an output pulse which drives the gate of the switching element. Usually, an intermediate element is necessary between the AC switching unit and the microcontroller.

Moreover, the microcontroller provides the interface with the user, through LED displays or touch screens, push buttons, rotary tuners, switches e.t.c. After all, mains control may not be the only operation of your project, so the microcontroller may be doing other staff as well.

 

The library

There are many libraries available for AC power control with PWM, each one having its own advantages. Towards this direction, our purpose was to build an integrated library for the ATMega Atmega8/168/328 chipset, with support of zero cross detection. The library allows the definition of many parameters, in order to be easily used with a large scale of electronic devices. The use of the library requires minimum to zero knowledge of programming, and probably, the user will not need to modify the source code of the library.

 

Instructions of use

Use of the library is just a three-step process:

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

See below for a list of supported methods.

 

Methods

The library is constituted of the following methods:

 

void initialize(int frequency, int zeroControlPin, int zeroPulseMode, int pwmPin, int scale);

This method is used once, at the setup() function of your sketch. It defines the parameters of your PWM operation.

frequency: The AC mains power frequency, e.g. 50 for 50Hz.

zeroCrossPin: The Arduino pin that accepts the zero crossing pulse. e.g. 2 for pin 2.

zeroPulseMode: Four modes are supported, that describe the zero pulse:

LOW: Zero cross happens whenever the pulse is LOW.

CHANGE: Zero cross happens whenever the pulse changes.

RISING: Zero cross happens whenever the pulse goes from LOW to HIGH.

FALLING: Zero cross happens whenever the pulse goes from HIGH to LOW.

These modes are the same exactly as the modes of the Arduino’s mainInterrupt() function (see specs).

Example of use: CHANGE: When the pulse is square wave. RISING: When the pulse is Delta.

pwmPin: The Arduino pin that produces the PWM pulse. E.g. 9 for pin 9. Attention: This pin must be either pin 9 or pin 10. If other pin is defined, pin 9 will be used.

Scale: The scaling of the duty cycle. For example, if scale=255, the duty cycle can have 256 distinct values (0-255), expressed as a percentage of the PWM pulse.

 

void setLatch(int basicLatch, int onLatch);

Some times electronics don’t work as we expect. In our case, there may be some delay to the response of the zero cross detection circuit or the ON and OFF switching of the switching element. This is called latching. If this delay is small, there is no problem. However, there are cases where unexpected problems occur. In order not to have you change your circuit, this library provides the setLatch() method, which absorbs the latching of your circuit. This method is (optionally) used once, right after the use of the initialize() function.

The use of this function is optional. If not used, zero latching is assumed. For more information on using this function, see latch adjustment, at the end of this page.

 

void setDutyCycle(int dutyCycle);

This method can be used dynamically, whenever and whenever you want, in your sketch. It sets the duty cycle of your PWM operation, in respect to the scaling you set on the initialize() function. Example: If you set scale=255 and dutyCycle=80, then you provide a mains power of 80/255 = 31.3% of the maximum power the load consumes. Obviously: 0 <= dutyCycle <= scale.

 

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. 

 

 

 /* AC POWER CONTROL WITH PWM AND ZERO CROSS DETECTION */
 /* AUTHOR: Dimitris El. Vassis - 2013 */

 

 #include <ACPWM.h>

 #define ZERO_PIN 2 //Input pin from zero cross detector
 #define PWM_PIN 9 //Output pin to TRIAC / SSR

 int SET_PIN = A0; //Analog pin for setting the dutyCycle value with a pontentiometer

 

 void setup()
 {
    pinMode(ZERO_PIN, INPUT);
    pinMode(PWM_PIN, OUTPUT);
    //Initialize PWM operation.
    //Mains frequency: 50Hz.
    //Zero crossing point reached whenever pulse to PIN2 changes
    //Duty cycle = 0..255. 0:always off. 255: always on. 150: 59% on.
    ACpwm.initialize(50,ZERO_PIN,CHANGE,PWM_PIN,255);
    //Latching when voltage is positive: 3 microseconds.
    //Latching when voltage is negative: 5 microseconds.
    ACpwm.setLatch(3,5);
 }

 

 void loop() {
    //Adjust the power with the potentiometer
    //Set the duty cycle equal to the value of the potentiometer.
    ACpwm.setDutyCycle(analogRead(SET_PIN));
 }

 

  

Latching adjustment

 As explained above, latching is the delay faced by electronic devices (zero cross detector, switching unit) to respond. If you face latching problems in your circuit, use the setLatch() function to absorb them. You will need an oscilloscope with two channels in order to see what’s going on. Connect channel1 of the oscilloscope to the AC mains, and the other one to the pwm pin of the Arduino board.

(a) Observe the delay between the zero crossing point and the fall of PWM pulse from ON to OFF state. If the falling of the PWM pulse is before the zero crossing point of the AC mains, set the basicLatch parameter to a positive value, in MICROSECONDS. If the falling of the PWM pulse comes after the zero crossing point of the AC mains, set the basicLatch parameter to a negative value, in MICROSECONDS. Obviously, if the two points coincide, set the onLatch parameter to zero. Load the script and test. Readjust if necessary.

(b) Set the duty cycle to 50%. Observe the PWM pulse on the oscilloscope and calculate the difference d = onPeriod - offPeriod. Set the onLatch parameter to onLatch = d, in MICROSECONDS (may be negative as well).

 

Links