AC PWM Library--Using Library to Control Multiple Sources

Hi,

I am using an Arduino Uno with a custom PCB shield that uses a zero cross-detection circuit as well as triacs and opto-isolaters to control two resistive loads using PWM.

I am using the library found here: http://playground.arduino.cc/Code/ACPWM

I have the code working great to control one of the triac circuits.

Is there a simple way to use the library to control both triac circuits independently?

For example can I do something like take:

 ACpwm.initialize(50,ZERO_PIN,CHANGE,PWM_PIN,255);

and add:

 ACpwm.initialize2(50,ZERO_PIN,CHANGE,PWM_PIN,255);

throughout the code?

Your help is greatly appreciated. Please let me know if I can provide any further information.

Thank you,

Danny

Have you tried using different pins? :slight_smile: Oh, wait, you can only use pin 9 or 10. Not sure about both.

It appears that when the library was created, he didn’t take into account the fact that two instances of the ACpwm object might be needed.
Still, you could try creating two ACpwm objects like this:-

#define ZERO_PIN1 <pin number>
#define ZERO_PIN2 <pin number>
#define PWM_PIN1 9
#define PWM_PIN2 10

ACPWM acPWM1;
ACPWM acPWM2;

Then initialise each:-

acPWM1.initialize(50,ZERO_PIN1,CHANGE,PWM_PIN1,255);
acPWM2.initialize(50,ZERO_PIN2,CHANGE,PWM_PIN2,255);

According to the documentation, either pin 9 or pin 10 must be used for the PWM pin:-
Edit: I noticed that aarg had already pointed this out, but left it anyway. :slight_smile:

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.

I tested the above and it compiles OK, but you’ll need to see if it actually works.

#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);
    ACpwm.initialize(50,ZERO_PIN,CHANGE,PWM_PIN,255)

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

I get that I could use the ACpwm.initialize function for a second PWM Pin(say PWM_PIN2), which I would have previously defined.

However, when I go to use the function ACpwm.setDutyCycle(), the program will not know which pin to set the duty cycle for, since I initialized two different pins.

ACpwm.initialize(50,ZERO_PIN,CHANGE,PWM_PIN,255)
ACpwm.initialize(50,ZERO_PIN,CHANGE,PWM_PIN2,255)

Does this clarify my problem?

Thanks!

What about:-

#define SET_PIN1 A0
#define SET_PIN2 A1

acPWM1.setDutyCycle(analogRead(SET_PIN1));
acPWM2.setDutyCycle(analogRead(SET_PIN2));

But does the library expect a value from 0 to 1023 for duty-cycle, or does it expect 0 to 255?
If 0-255, you'll need to use 'map()' to convert the pot's 0-1023 to 0-255 before passing it to 'setDutyCycle()'.
(Or divide the pot value by 4.)

Edit: I should perhaps mention that I've never used this library, I just quickly downloaded a copy to see if I could help. I haven't studied it in depth, so don't really know what will and won't work with it. If you study it's code you'll get a better idea.
As I said, the documentation wasn't written with multiple instances in mind.

So I tried running the code:

#define ZERO_PIN1 <pin number>
#define ZERO_PIN2 <pin number>
#define PWM_PIN1 9
#define PWM_PIN2 10

ACPWM acPWM1;
ACPWM acPWM2;

void setup() 
{
acPWM1.initialize(50,ZERO_PIN1,CHANGE,PWM_PIN1,255);
acPWM2.initialize(50,ZERO_PIN2,CHANGE,PWM_PIN2,255);
}

void loop() 
{
acPWM1.setDutyCycle(analogRead(SET_PIN1));
acPWM2.setDutyCycle(analogRead(SET_PIN2));
}

The Pin 9, which is associated with acPWM1 works and produces a PWM signal and triggers the Triac. The Pin 10 associated with the acPWM2 does nothing.

The library does expect something from 0 to 255, which I have mapped.

Thanks!

hoffmadk:
So I tried running the code:

#define ZERO_PIN1 <pin number>

#define ZERO_PIN2
#define PWM_PIN1 9
#define PWM_PIN2 10

ACPWM acPWM1;
ACPWM acPWM2;

void setup()
{
acPWM1.initialize(50,ZERO_PIN1,CHANGE,PWM_PIN1,255);
acPWM2.initialize(50,ZERO_PIN2,CHANGE,PWM_PIN2,255);
}

void loop()
{
acPWM1.setDutyCycle(analogRead(SET_PIN1));
acPWM2.setDutyCycle(analogRead(SET_PIN2));
}




The Pin 9, which is associated with acPWM1 works and produces a PWM signal and triggers the Triac. The Pin 10 associated with the acPWM2 does nothing. 

The library does expect something from 0 to 255, which I have mapped. 

Thanks!

Oh well, it was worth a try.

You might be able to modify the library, but chances are with a bit of searching you can find another library that does support multiple outputs.

Otherwise, depending on your level of experience, you could always write your own code to do it. You even have the ACPWM library as a guide.

Edit: First, though, can you show us your complete code, (the code that you actually tested, rather than what you just posted), just in case there’s a minor error causing the problem?

hoffmadk:
Hi,

I am using an Arduino Uno with a custom PCB shield that uses a zero cross-detection circuit as well as triacs and opto-isolaters to control two resistive loads using PWM.

I am using the library found here: http://playground.arduino.cc/Code/ACPWM

I have the code working great to control one of the triac circuits.

Is there a simple way to use the library to control both triac circuits independently?

For example can I do something like take:

 ACpwm.initialize(50,ZERO_PIN,CHANGE,PWM_PIN,255);

and add:

 ACpwm.initialize2(50,ZERO_PIN,CHANGE,PWM_PIN,255);

throughout the code?

Your help is greatly appreciated. Please let me know if I can provide any further information.

Thank you,

Danny

Hi, i know some years have passed, but do you still have the schematics for the circuit you used with that program? I'm struggling to have one that works for AC phase control on Arduino (not even the one suggested by playground.arduino itself seems to work properly). Thanks in advance.